VBA Outlook打开Excel文件并在打开时导入数据

时间:2019-01-22 21:31:45

标签: vba outlook outlook-vba

我正在尝试设置以下过程。 当我早上打开Outlook时,它将在网络文件夹中查找。如果该文件夹中的文件没有今天的日期作为保存日期,则它将打开该文件并从选定的路径导入数据,然后将其保存。

我现在遇到的问题是让它执行导入过程。它将打开文件,但导入时出错。给我“对象不支持此属性或方法”

任何帮助将不胜感激!

 Sub Application_Startup()

   Dim xlApp As Excel.Application
   Dim sourceWB As Excel.Workbook
   Dim sourceSH As Excel.Worksheet
   Dim oXL As Excel.Application
   Dim oWB As Excel.Workbook

 'Check if file doesn't have today's date (need to write code)


  Set xlApp = New Excel.Application
      With xlApp
      .Visible = True
      .EnableEvents = True
      '.UserControl = True
      '.DisplayAlerts = True
      '.AskToUpdateLinks = True
    End With

 strFile = "S:\NFInventory\groups\CID\CID Database\BigPic Files\BigPic 2019.xlsx"

'Import process (Error starts next line)
 strFile = Application.GetOpenFilename("Text Files (*.PRN),*.PRN", , "Please select text file...")

With wb.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
    .TextFileParseType = xlDelimited
    .TextFileCommaDelimiter = True
    .Refresh
End With

 'open File
  Set sourceWB = xlApp.Workbooks.Open(strFile, , , , , , , , , True)
  Set sourceSH = sourceWB.Worksheets("Sheet1")
  sourceWB.Activate

 'Save and Close
     'Workbooks("BigPic 2019.xlsx").Close SaveChanges:=True

 End Sub

1 个答案:

答案 0 :(得分:1)

您的主机应用程序是Outlook,而GetOpenFilename()是Excel应用程序功能

因此通过Excel应用程序对象调用它:

strFile = xlApp.GetOpenFilename("Text Files (*.PRN),*.PRN", , "Please select text file...")