是否有另存为对话框?

发布于 2024-10-17 20:17:50 字数 56 浏览 4 评论 0原文

我想使用“另存为”文件对话框保存邮件附件。是否可以使用 VBA 和 Outlook 来完成此操作?

I want to save a mail attachment with a SaveAs file dialog. Is it possible to do this with VBA and Outlook?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城烟雨 2024-10-24 20:17:50

我认为 Outlook 不会让您打开文件对话框!

我使用的一个丑陋但快速且实用的解决方法是临时打开 Excel 实例并使用其 GetSaveAsFilename 方法。

Set xlApp = CreateObject("Excel.application")
xlApp.Visible = False
strSaveAsFilename = xlApp.GetSaveAsFilename
xlApp.Quit
Set xlApp = Nothing

然后你可以说MyAttachment.SaveAsFile(strSaveAsFilename)

如果不一定安装 Excel,那么您可以使用 Word 和 FileDialog 方法执行类似的操作(Word 没有 GetSaveAsFilename)。有关示例,请参阅 FileDialog 上的 VBA 帮助。

可能有一个更优雅的解决方案,但上面的方法可以工作......

I don't think Outlook will let you open a file dialog!

An ugly but quick and functional workaround that I have used is to temporarily open an instance of Excel and use its GetSaveAsFilename method.

Set xlApp = CreateObject("Excel.application")
xlApp.Visible = False
strSaveAsFilename = xlApp.GetSaveAsFilename
xlApp.Quit
Set xlApp = Nothing

Then you can say MyAttachment.SaveAsFile(strSaveAsFilename).

If Excel is not necessarily installed, then you can do a similar trick using Word and the FileDialog method (Word doesn't have GetSaveAsFilename). See VBA help on FileDialog for an example.

There is probably a more elegant solution out there, but the above will work...

自此以后,行同陌路 2024-10-24 20:17:50

不要忘记 BrowseForFolder 函数:

Function BrowseForFolder(Optional OpenAt As String) As String 

Dim ShellApp As Object 

Set ShellApp = CreateObject("Shell.Application"). _ 
BrowseForFolder(0, "Please choose a folder", 0, OpenAt) 

On Error Resume Next 
BrowseForFolder = ShellApp.self.Path 
On Error GoTo 0 

Select Case Mid(BrowseForFolder, 2, 1) 
Case Is = ":" 
    If Left(BrowseForFolder, 1) = ":" Then 
        BrowseForFolder = "" 
    End If 
Case Is = "\" 
    If Not Left(BrowseForFolder, 1) = "\" Then 
        BrowseForFolder = "" 
    End If 
Case Else 
    BrowseForFolder = "" 
End Select 

ExitFunction: 

Set ShellApp = Nothing 

End Function

Don't forget the BrowseForFolder function:

Function BrowseForFolder(Optional OpenAt As String) As String 

Dim ShellApp As Object 

Set ShellApp = CreateObject("Shell.Application"). _ 
BrowseForFolder(0, "Please choose a folder", 0, OpenAt) 

On Error Resume Next 
BrowseForFolder = ShellApp.self.Path 
On Error GoTo 0 

Select Case Mid(BrowseForFolder, 2, 1) 
Case Is = ":" 
    If Left(BrowseForFolder, 1) = ":" Then 
        BrowseForFolder = "" 
    End If 
Case Is = "\" 
    If Not Left(BrowseForFolder, 1) = "\" Then 
        BrowseForFolder = "" 
    End If 
Case Else 
    BrowseForFolder = "" 
End Select 

ExitFunction: 

Set ShellApp = Nothing 

End Function
青芜 2024-10-24 20:17:50

有两种方法可以模拟此行为(此处假设为 Outlook 2003):

使用文件 » 保存附件

此代码将以编程方式调用“文件”菜单上的“保存附件”菜单项。以下三个辅助功能是必需的,应粘贴到同一个项目中。选择或打开带有附件的电子邮件,然后运行 ​​SaveAttachments 过程。

Sub SaveAttachments()

Dim obj As Object
Dim msg As Outlook.mailItem
Dim insp As Outlook.Inspector

Set obj = GetCurrentItem
If TypeName(obj) = "MailItem" Then
  Set msg = obj
  Set insp = msg.GetInspector
  With insp
    .Display
    ' execute the File >> Save Attachments control
    .CommandBars.FindControl(, 3167).Execute
    .Close olDiscard ' or olPromptForSave, or olSave
  End With
End If

End Sub

Function GetCurrentItem() As Object
  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set GetCurrentItem = ActiveExplorer.Selection.item(1)
  Case IsInspector(Application.ActiveWindow)
    Set GetCurrentItem = ActiveInspector.CurrentItem
  End Select
End Function
Function IsExplorer(itm As Object) As Boolean
  IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
  IsInspector = (TypeName(itm) = "Inspector")
End Function

请注意,如果有多个附件,系统将提示您选择要保存的附件,然后再显示保存对话框:

用多个文件保存附件

使用 BrowseForFolder

我使用 上找到的 BrowseForFolder 函数VBAX。这将显示 Shell.Application 的 BrowseForFolder 对话框:

shell 应用程序浏览文件夹

选择或打开带有附件的电子邮件并运行 < code>SaveAttachments 过程。在对话框中选择文件夹后,电子邮件的所有附件都将保存到所选文件夹中。

Sub SaveAttachments()

  Dim folderToSave As String
  Dim obj As Object
  Dim msg As Outlook.mailItem
  Dim msgAttachs As Outlook.attachments
  Dim msgAttach As Outlook.Attachment

  folderToSave = BrowseForFolder

  If folderToSave <> "False" Then

    Set obj = GetCurrentItem
    If TypeName(obj) = "MailItem" Then
      Set msg = obj
      Set msgAttachs = msg.attachments

      For Each msgAttach In msgAttachs
        msgAttach.SaveAsFile folderToSave & "\" & msgAttach.FileName
      Next msgAttach
    End If

  End If

End Sub

Function GetCurrentItem() As Object
  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set GetCurrentItem = ActiveExplorer.Selection.item(1)
  Case IsInspector(Application.ActiveWindow)
    Set GetCurrentItem = ActiveInspector.CurrentItem
  End Select
End Function
Function IsExplorer(itm As Object) As Boolean
  IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
  IsInspector = (TypeName(itm) = "Inspector")
End Function

There are two ways to simulate this behavior (I assume Outlook 2003 here):

Use File » Save Attachments

This code will programmatically call the "Save Attachments" menu item on the File Menu. The three ancillary functions below are necessary and should be pasted into the same project. Select or open an email with attachments and run the SaveAttachments procedure.

Sub SaveAttachments()

Dim obj As Object
Dim msg As Outlook.mailItem
Dim insp As Outlook.Inspector

Set obj = GetCurrentItem
If TypeName(obj) = "MailItem" Then
  Set msg = obj
  Set insp = msg.GetInspector
  With insp
    .Display
    ' execute the File >> Save Attachments control
    .CommandBars.FindControl(, 3167).Execute
    .Close olDiscard ' or olPromptForSave, or olSave
  End With
End If

End Sub

Function GetCurrentItem() As Object
  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set GetCurrentItem = ActiveExplorer.Selection.item(1)
  Case IsInspector(Application.ActiveWindow)
    Set GetCurrentItem = ActiveInspector.CurrentItem
  End Select
End Function
Function IsExplorer(itm As Object) As Boolean
  IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
  IsInspector = (TypeName(itm) = "Inspector")
End Function

Note that if there are multiple attachments, you will be prompted to choose which one(s) you want to save before being shown the save dialog:

save attachments with multiple files

Use BrowseForFolder

I use the BrowseForFolder function found on VBAX. This will show the Shell.Application's BrowseForFolder dialog:

shell app browse for folder

Select or open an email with attachments and run the SaveAttachments procedure. After selecting a folder in the dialog, all attachments to the email will be saved to the selected folder.

Sub SaveAttachments()

  Dim folderToSave As String
  Dim obj As Object
  Dim msg As Outlook.mailItem
  Dim msgAttachs As Outlook.attachments
  Dim msgAttach As Outlook.Attachment

  folderToSave = BrowseForFolder

  If folderToSave <> "False" Then

    Set obj = GetCurrentItem
    If TypeName(obj) = "MailItem" Then
      Set msg = obj
      Set msgAttachs = msg.attachments

      For Each msgAttach In msgAttachs
        msgAttach.SaveAsFile folderToSave & "\" & msgAttach.FileName
      Next msgAttach
    End If

  End If

End Sub

Function GetCurrentItem() As Object
  Select Case True
  Case IsExplorer(Application.ActiveWindow)
    Set GetCurrentItem = ActiveExplorer.Selection.item(1)
  Case IsInspector(Application.ActiveWindow)
    Set GetCurrentItem = ActiveInspector.CurrentItem
  End Select
End Function
Function IsExplorer(itm As Object) As Boolean
  IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
  IsInspector = (TypeName(itm) = "Inspector")
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文