首页 > 解决方案 > 如何打开我刚刚在 Outlook 中解压缩的工作表

问题描述

我正在尝试在 Excel 和 Outlook 中编写一些宏,它们最终会自动解压缩并打开 CSV,处理数据,并在新电子邮件到达特定文件夹时将其发送到需要去的地方。我已经在 Excel 方面解决了所有问题,但我在使用 Outlook 时遇到了困难。下面的代码解压缩文件。我将如何打开解压缩的文件并触发 Excel 宏(始终在另一个工作簿中打开)?

我遇到的另一个问题:此代码似乎仅在我实际在其自己的窗口中打开目标电子邮件时才有效。

Public Sub OpenZippedSheet()
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objAttachment As Outlook.Attachment
    Dim objShell As Object
    Dim objFileSystem As Object
    Dim strTempFolder As String
    Dim strFilePath As String
    Dim strFileName As String

    Set objMail = Outlook.Application.ActiveInspector.CurrentItem
    Set objAttachments = objMail.Attachments

    'Save & Unzip the zip file in local drive
    Set objShell = CreateObject("Shell.Application")
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp" & Format(Now, "yyyy-mm-dd-hh-mm-ss")
    MkDir (strTempFolder)

    For Each objAttachment In objAttachments
        If Right(objAttachment.FileName, 3) = "zip" Then
           strFilePath = strTempFolder & "\" & objAttachment.FileName
           objAttachment.SaveAsFile (strFilePath)
           objShell.NameSpace((strTempFolder)).CopyHere objShell.NameSpace((strFilePath)).Items
        End If
    Next

End Sub

我假设我会做某种 object.open 但我不知道让它在 Excel 中实际打开的语法是什么。那么有没有办法从 Outlook 触发 Excel 宏?

提前非常感谢!

标签: excelvbaoutlook

解决方案


此代码似乎仅在我实际在其自己的窗口中打开目标电子邮件时才有效。

那是因为你依赖ActiveInspector窗口。如果要处理在资源管理器窗口中选择的项目,则需要检查Selection对象(请参阅相应的属性)。

要打开 Excel 文件,您可以:

  1. 使用Shell.ShellExecute方法。此方法等效于启动与文件快捷菜单关联的命令之一。每个命令都由一个动词字符串表示。支持的动词集因文件而异。最常支持的动词是“open”,通常也是默认动词。其他动词可能仅受某些类型的文件支持。
  2. Automate Excel from your VBA macro to do the required actions. See How to automate Microsoft Excel from Visual Basic for more information.

To run your VBA macro code from other applications you can use the Application.Run method. Read more about that in the How do I use Application.Run in Excel article.

Application.Run "'" & TestWkbk.Name & "'!MacroNameHere", "parm1", "parm2"

推荐阅读