如何使用 Outlook VBA 实施 Application.FileDialog?

How to implement Application.FileDialog using Outlook VBA?

我想在我的邮件中附加一个文件。我要选择路径

我试过了Application.FileDialog

我明白了

Runtime Error 438.

我在网上找到这段代码:

Sub Main()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
 
    Dim vrtSelectedItem As Variant
 
    With fd
 
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems

            Next vrtSelectedItem

        Else

        End If

    End With
 
    Set fd = Nothing
End Sub

你用windowsAPI调用怎么样

Option Explicit
Public Declare Function GetOpenFileNameB Lib "comdlg32.dll" _
                                Alias "GetOpenFileNameA" _
                (pOpenfilename As OPENFILENAME) As Long

Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type


Public Sub Example()
    Dim Email As Outlook.MailItem
    Set Email = Application.CreateItem(0)

    Dim File As String
        File = GetOpenFileName()

    With Email
        .To = ""
        .Attachments.Add (File)
        .Display
    End With

End Sub


Public Function GetOpenFileName(Optional ByVal vFileFilter As String, _
                                Optional ByVal vWindowTitle As String, _
                                Optional ByVal vInitialDir As String, _
                                Optional ByVal vInitialFileName As String) As String

Dim OFN As OPENFILENAME, retVal As Long
    OFN.lStructSize = Len(OFN)
    OFN.hwndOwner = 0
    OFN.hInstance = 0
    OFN.lpstrFile = IIf(vInitialDir = "", Space$(254), vInitialDir & Space$(254 - Len(vInitialDir)))
    OFN.lpstrInitialDir = IIf(vWindowTitle = "", CurDir, vInitialDir)
    OFN.lpstrTitle = IIf(vWindowTitle = "", "Select File", vWindowTitle)
    OFN.lpstrFilter = IIf(vFileFilter = "", "All Files (*.*)" & Chr(0) & "*.*", _
                                Replace(vFileFilter, ",", Chr$(0)))

    OFN.nMaxFile = 255
    OFN.lpstrFileTitle = Space$(254)
    OFN.nMaxFileTitle = 255
    OFN.flags = 0

    retVal = GetOpenFileNameB(OFN)

    If retVal Then GetOpenFileName = Trim$(OFN.lpstrFile)
End Function

运行 Public Sub Example()

我找到了以下解决方案。它有效,但 FileDialog 在后台启动。有没有意见在Userform和Outlookwindow

前启动FileWindow
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = False

Dim fd As Office.FileDialog
Set fd = xlApp.Application.FileDialog(msoFileDialogFilePicker)

Dim selectedItem As Variant

If fd.Show = -1 Then
    For Each selectedItem In fd.SelectedItems
        Debug.Print selectedItem
    Next
End If

Set fd = Nothing
    xlApp.Quit
Set xlApp = Nothing