使用VBA读取.xlsx附件数据。

huangapple go评论73阅读模式
英文:

Reading .xlsx attachment data with VBA

问题

在Outlook宏中,我有一个包含.xlsx附件的电子邮件。我想要获取其中一个列中字符串的总长度。是否可以在Outlook宏中动态读取附件数据,或者有什么建议的方法?我是否需要先保存附件,然后将其作为工作簿打开并从那里处理?

英文:

I have an .xlsx attachment in an e-mail in Outlook. I want to get summed length of strings in one of the columns using Outlook macro. Is it possible to read attachements data on the fly in Outlook macro or what is the advised approach? Do I need to save the attachment first, then open it as a workbook and work from there?

答案1

得分: 2

你可以直接打开文件,如果你正在使用Open XML API,但在VBA中无法访问。

否则,你可以使用Excel对象模型来读取该文件:

Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False
Set Workbook = ExcelApp.Workbooks.Open("C:\Path\To\Your\File.xlsx")
英文:

You can open the file directly if you are using Open XML API, but it is not accessible in VBA.

Otherwise you can use Excel Object Model to read that file:

Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = false
Set Workbook = ExcelApp.Workbooks.Open("C:\Path\To\Your\File.xlsx")

答案2

得分: 1

Outlook对象模型不提供处理附件的方法。使用低级别API(扩展MAPI),您可以获取表示附件文件的字节数组。

您需要将附件文件保存到磁盘,然后自动化Excel来完成工作。Attachment.SaveAsFile方法将附件保存到指定路径。以下示例代码摘自MSDN,展示了如何使用该方法:

Sub SaveAttachment()
 Dim myInspector As Outlook.Inspector
 Dim myItem As Outlook.MailItem
 Dim myAttachments As Outlook.Attachments

 Set myInspector = Application.ActiveInspector
 If Not TypeName(myInspector) = "Nothing" Then
   If TypeName(myInspector.CurrentItem) = "MailItem" Then
     Set myItem = myInspector.CurrentItem
     Set myAttachments = myItem.Attachments
     'Prompt the user for confirmation
     Dim strPrompt As String
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file."
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
       myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _
       myAttachments.Item(1).DisplayName
     End If
   Else
     MsgBox "The item is of the wrong type."
   End If
 End If
End Sub

要从Outlook VBA环境中访问Excel类型,您需要添加相应的COM引用:

' 在项目|引用对话框(或VB4或VBA的工具|引用)中设置对 'Microsoft Excel 16.0 Object Library' 的引用。

' 声明对象为早期绑定对象
Dim oExcel As Excel.Application

Set oExcel = CreateObject("Excel.Application")

' 通过v-table调用Visible属性
oExcel.Visible = True

有关更多信息,请参阅在自动化中使用早期绑定和后期绑定

英文:

The Outlook object model doesn't provide anything for dealing with attached files on the fly. The best what you could do using a low-level API (Extended MAPI) is to get the byte array which represents the attached file.

You need to save the attached file on the disk and then automate Excel for getting the job done. The Attachment.SaveAsFile method saves the attachment to the specified path. The following sample code taken from MSDN shows how to use that method:

Sub SaveAttachment() 
 Dim myInspector As Outlook.Inspector 
 Dim myItem As Outlook.MailItem 
 Dim myAttachments As Outlook.Attachments 
 
 Set myInspector = Application.ActiveInspector 
 If Not TypeName(myInspector) = "Nothing" Then 
   If TypeName(myInspector.CurrentItem) = "MailItem" Then 
     Set myItem = myInspector.CurrentItem 
     Set myAttachments = myItem.Attachments 
     'Prompt the user for confirmation 
     Dim strPrompt As String 
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file." 
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then 
       myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _ 
       myAttachments.Item(1).DisplayName 
     End If 
   Else 
     MsgBox "The item is of the wrong type." 
   End If 
 End If 
End Sub

To get access to Excel types from the Outlook VBA environment you need to add a corresponding COM reference:

' Set reference to 'Microsoft Excel 16.0 Object Library' in
' the Project|References dialog (or Tools|References for VB4 or VBA).

' Declare the object as an early-bound object
  Dim oExcel As Excel.Application

  Set oExcel = CreateObject("Excel.Application")

' The Visible property is called via the v-table
  oExcel.Visible = True

See Using early binding and late binding in Automation for more information.

huangapple
  • 本文由 发表于 2023年6月16日 00:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定