在使用VBA在Outlook中添加文件作为附件时,如何将打开文件夹对话框窗口设置为活动窗口?

时间:2018-06-08 16:37:36

标签: vba excel-vba outlook-vba excel

好的,所以我有工作代码,但是添加文件的浏览器会在后台显示。这不会是那么糟糕,但是当这种情况发生时,很难快速达成目标。

我正在试图弄清楚如何让弹出式浏览器显示在所有内容之上,这样它就不会被埋没。它也非常慢,所以如果有更有效的方式,那也会很棒。这是我到目前为止所拥有的。

Option Explicit
Private Sub AttachmentFile()
Dim oLook As Object
Dim oMail As Object
Dim FD As Object
Dim vrtSelectedItem As Variant

    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.CreateItem(0)
    Set FD = Excel.Application.FileDialog(3)

    With oMail

            FD.AllowMultiSelect = True
            FD.Filters.Clear
            FD.Filters.Add "All Files", "*.*"
            FD.InitialFileName = "\\ad\dfs\Shared Data\"

            If FD.Show = True Then
                  For Each vrtSelectedItem In FD.SelectedItems
                  .Attachments.Add vrtSelectedItem
                    Next
            End If
            .Display
    End With
    Set FD = Nothing
    Set oMail = Nothing
    Set oLook = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

尽管Outlook中没有FileDialog对象,但是您可以模拟使用ExecuteMso按下按钮的情况。

要打开“ Outlook插入文件”对话框,请按AttachFile按钮:

Private Sub ExecuteMso_AttachFile()

    Dim currItem As Object

    On Error Resume Next
    Set currItem = ActiveInspector.currentItem
    On Error GoTo 0

    If currItem Is Nothing Then Set currItem = CreateItem(olMailItem)

    currItem.Display

    ' Hover over the icon that you would add as a button
    ' The IdMso to use in ExecuteMso is the last part of the text displayed.
    ActiveInspector.CommandBars.ExecuteMso ("AttachFile")

End Sub