【问题标题】:Is there a SaveAs dialog?有另存为对话框吗?
【发布时间】:2011-02-16 16:41:30
【问题描述】:

我想用SaveAs 文件对话框保存邮件附件。是否可以使用 VBA 和 Outlook 做到这一点?

【问题讨论】:

    标签: vba outlook save-dialog


    【解决方案1】:

    我认为 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 帮助。

    那里可能有一个更优雅的解决方案,但上面的方法会起作用......

    【讨论】:

    • 谢谢!实际上这会很好用,因为无论如何我的代码最终都会打开一个 excel 文件。有没有为 GetSaveAsFilename 指定一个默认文件夹?
    【解决方案2】:

    别忘了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
    

    【讨论】:

      【解决方案3】:

      有两种方法可以模拟这种行为(我假设这里是 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
      

      请注意,如果有多个附件,系统会在显示保存对话框之前提示您选择要保存的附件:

      使用浏览文件夹

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

      选择或打开带有附件的电子邮件并运行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
      

      【讨论】:

        猜你喜欢
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 2013-06-23
        • 2015-12-28
        相关资源
        最近更新 更多