0

I want to attach a file to my mail. I want to choose the path.

I tried Application.FileDialog.

I get

Runtime Error 438.

I found this code on the Internet:

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
Community
  • 1
  • 1
Jocasso
  • 465
  • 1
  • 7
  • 20

2 Answers2

4

How about you use windows API calls

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

Run Public Sub Example()

0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Thanks for your answer. I tried this code and it shows me an Runtime Error on the Attachments.Add(file) line., because the path doesn't exist. The path doesn't exist because there didn't open a file window. – Jocasso Apr 19 '19 at 06:20
  • Sorry after "declare" I added "PtrSafe", beacause it showed me a compiler Error, which said that the code must be refresh for 64-Bit-Syystems and that I should use the PtrSafe attribut – Jocasso Apr 19 '19 at 06:36
  • It works on my computer- Okay I will run on different computer tomorrow and let you know. Which office are running? – 0m3r Apr 19 '19 at 06:43
  • 1
    I'm running office 365 – Jocasso Apr 19 '19 at 06:46
  • Hey, i still get following error code "compile Error The Code in this Project must be updated for use on 64-Bit-systems. Please review and update Declare statements and then mark them with the PtrSafe attribute" – Jocasso Apr 23 '19 at 05:18
1

I found following solution. It works, but the FileDialog starts in the Backgorund. Is there an opinion to start the File Window in front of the Userform and Outlook window

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
Jocasso
  • 465
  • 1
  • 7
  • 20
  • After much ado - there is an option to open the `FileDialog` in front of the other windows. See my comment on the [related answer](https://stackoverflow.com/a/43921567/15243610). If you're ok with it - I can also edit your answer with the tweaks I made that are working on my side – immobile2 Oct 08 '21 at 14:52