excel - 如何打开我刚刚在 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 宏?
提前非常感谢!
解决方案
此代码似乎仅在我实际在其自己的窗口中打开目标电子邮件时才有效。
那是因为你依赖ActiveInspector
窗口。如果要处理在资源管理器窗口中选择的项目,则需要检查Selection
对象(请参阅相应的属性)。
要打开 Excel 文件,您可以:
- 使用Shell.ShellExecute方法。此方法等效于启动与文件快捷菜单关联的命令之一。每个命令都由一个动词字符串表示。支持的动词集因文件而异。最常支持的动词是“open”,通常也是默认动词。其他动词可能仅受某些类型的文件支持。
- 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"
推荐阅读
- graph - ArangoDB谱系图遍历
- c# - 使用 C# 中的代码渲染图像按钮
- saml-2.0 - xmlsec1 saml 签名:找不到名称为“Signature”的默认节点
- swift - 表视图滚动导致意外结果
- symfony - 如何在引用多对一关系symfony 3的sel中获取类别和子类别
- typescript - 将字符串查询转换为可执行语句
- c# - 列表视图框中未显示最小编号
- android - 如何使用android中的get方法在url中设置json数组?
- kubernetes - kubernetes有没有可以同时创建多种资源的api
- python - 为简单的基于文本的 Python 程序定义函数的递归