简体   繁体   English

打开 Excel 文件,使用 Outlook VBA 将数据导入该文件

[英]Open Excel file, to import data to that file, using Outlook VBA

I'm trying to set up the following process.我正在尝试设置以下过程。

When I open Outlook in the morning it will look in a network folder.当我早上打开 Outlook 时,它会在网络文件夹中查找。 If the file in that folder does not have today's date as a save date open the file and import data from a chosen path then save it.如果该文件夹中的文件没有今天的日期作为保存日期,请打开文件并从所选路径导入数据,然后保存。

It will open Excel but it errors on strFile = Application.GetOpenFilename("Text Files (*.PRN),*.PRN", , "Please select text file...") .它将打开 Excel,但在strFile = Application.GetOpenFilename("Text Files (*.PRN),*.PRN", , "Please select text file...")上出错。

"Object doesn't Support this property or Method" “对象不支持此属性或方法”

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

Your host application is Outlook while GetOpenFilename() is an Excel Application function您的主机应用程序是 Outlook 而 GetOpenFilename() 是 Excel 应用程序函数

So call it via Excel application object:所以通过 Excel 应用程序对象调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM