简体   繁体   English

如何使用 Outlook VBA 浏览 Windows 文件夹并在其中保存 Outlook 附件

[英]How Do I Browse Windows Folders And Save An Outlook Attachment There Using Outlook VBA

I would like to save an Outlook attachment using Outlook VBA with the ability to browse for a destination folder in windows prior to the save.我想使用 Outlook VBA 保存 Outlook 附件,并能够在保存之前在 Windows 中浏览目标文件夹。 I have found the thread "Save attachments to a folder and rename them" as well as several other threads through google.我通过谷歌找到了主题“将附件保存到文件夹并重命名”以及其他几个主题。

All of the solutions I have found so far include the folder path as text within the code like folderStr = "C:\Users\ME\Documents".到目前为止,我发现的所有解决方案都将文件夹路径作为文本包含在代码中,例如 folderStr = "C:\Users\ME\Documents"。 The file path then has the specific attachment name attached to the string and possibly a sub folder that needs to already exist on the computer.文件路径然后将特定的附件名称附加到字符串,并且可能需要计算机上已经存在的子文件夹。

I am using the following pair of functions in Excel VBA but when I tried to use them in Outlook it failed.我在 Excel VBA 中使用了以下一对函数,但是当我尝试在 Outlook 中使用它们时失败了。 I double checked the reference libraries but maybe I missed something else in the implementation.我仔细检查了参考库,但也许我在实现中遗漏了其他东西。

Function SHGetPathFromIDList Lib "shell32.dll"
Function SHBrowseForFolder Lib "shell32.dll"

I am beginning to believe that this is not possible from within the Outlook VBA module.我开始相信这在 Outlook VBA 模块中是不可能的。 Any help is appreciated.任何帮助表示赞赏。

You are free to use any Windows API functions from Outlook VBA, just need to declare them correctly according to the host/OS bitness, for example, see How to use Windows SHBrowseforFolder function on 32 bit or 64 bit Excel VBA .您可以自由使用 Outlook VBA 中的任何 Windows API 函数,只需根据主机/操作系统的位数正确声明它们,例如,请参阅如何在 32 位或 64 位 Excel VBA 上使用 Windows SHBrowseforFolder 函数

The Outlook object model provides the SaveAsFile method which saves the attachment to the specified path. Outlook 对象模型提供了将附件保存到指定路径的SaveAsFile方法。 The location at which to save the attachment is represented by the parameter of type string.保存附件的位置由字符串类型的参数表示。

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

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

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