Consulting

Results 1 to 5 of 5

Thread: [HELP] Outlook Form File Search Dialog

  1. #1
    VBAX Newbie Gawl's Avatar
    Joined
    Apr 2005
    Location
    Lisbon
    Posts
    2
    Location

    Question [HELP] Outlook Form File Search Dialog

    I need to click in a button and open the file search dialog to add attachment's in a form anyone can help me??? i make the attachment code but to attach the file i need to write the path manually i want this dialog to automate the path!!! Help me plz

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi Gawl and welcome to VBAX

    First the bad news... Outlook doesn't support Application.FileDialog the same way that Word Excel and PowerPoint do even though it's part of the Office common control library. For some reason it can't be accessed from within the OL object model which seems very strange, especially when you click the Attach Files button and it appears, but that's how it is and we just have to deal with it.

    The good news is that there are at least a couple of ways to get round it...
    First option: build your own userform file picker
    Second option: Use the file dialog available in another app (lik Word)
    To do this you'll need to add a reference to the MS Word Object Model in the VBEditor|Tools|References, then add and adapt the code below[VBA]Sub GetPathsWithWordDialog()

    Dim wdApp As Word.Application
    Dim fd As FileDialog
    Dim olWin As Object
    Dim s
    Dim Kill_Word As Boolean

    Set olWin = Application.ActiveWindow

    On Error Resume Next
    Kill_Word = False
    Set wdApp = GetObject(, "Word.Application") 'if Word already open
    If wdApp Is Nothing Then
    Set wdApp = CreateObject("Word.Application")
    Kill_Word = True
    End If

    'switch to Word
    wdApp.Visible = True
    wdApp.Activate

    Set fd = wdApp.FileDialog(msoFileDialogFilePicker)
    With fd
    .AllowMultiSelect = True
    .Filters.Add "All Files", "*.*", 1
    .Show
    olWin.Activate
    For Each s In .SelectedItems
    's is a string variant that provides the full
    'file path for attaching the selected files
    'so do attachments here
    Next
    End With

    Set fd = Nothing
    If Kill_Word = True Then wdApp.Quit
    Set wdApp = Nothing

    End Sub[/VBA]Third option: use a function call to the WIN API to use the Windows common dialog.[VBA]'Declare the required function at the top of the module
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    'declare the type required for the function paramater
    Private 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

    'procedure to display the dialog
    Sub GetPathWithSystemDialog()

    Dim fName As OPENFILENAME
    fName.lStructSize = Len(fName)
    'Select a filter
    fName.lpstrFilter = "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    'create a buffer for the file
    fName.lpstrFile = Space$(254)
    'set the maximum length of a returned file
    fName.nMaxFile = 255
    'Create a buffer for the file title
    fName.lpstrFileTitle = Space$(254)
    'Set the maximum length of a returned file title
    fName.nMaxFileTitle = 255
    'Set the initial directory
    'fName.lpstrInitialDir = "C:\"
    'Set the title
    fName.lpstrTitle = "Attach File..."
    'No flags
    fName.flags = 0

    'Show the 'Open File'-dialog
    If GetOpenFileName(fName) Then
    MsgBox "File to Attach: " + Trim$(fName.lpstrFile)
    Else
    MsgBox "Cancel was pressed"
    End If

    End Sub[/VBA] That should get you started
    K :-)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Killian
    Hi Gawl and welcome to VBAX

    First the bad news... Outlook doesn't support Application.FileDialog the same way that Word Excel and PowerPoint do even though it's part of the Office common control library. For some reason it can't be accessed from within the OL object model which seems very strange, especially when you click the Attach Files button and it appears, but that's how it is and we just have to deal with it.
    @ Gawl,

    Welcome to VBAX!

    Hi K,

    Indead that's mighty strange and it frustrated the hell out off me in the past!

    Why...the thing is that there is even a sample code in the HelpFile and if you type FileDialog in the VBE en press F1 you can see that the help file writers even thought it was going to be in here! The whole FileDialog Object is in the damn puppy!

    This must be one off those things that got mixed up with al those separate code teams that Microsoft uses per Application. (A lot of difference between Office apps are caused by this lack of communication)

    In that time I found a simular sollution like yours from Outlook MVP Sue Mosher...and ussualy if she says the object isn't there...it ussualy is:
    http://www.outlookcode.com/codedetail.aspx?id=294

    Guess we will never trully know why...
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  4. #4
    VBAX Newbie Gawl's Avatar
    Joined
    Apr 2005
    Location
    Lisbon
    Posts
    2
    Location
    tkx for the help!!! OL2000 don't supports the word file dialog :S:S:S

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Gawl
    tkx for the help!!! OL2000 don't supports the word file dialog :S:S:S
    Hi,

    No problem..

    The API Class sub from Killian will work in 2000 so that fixes you're problem.

    The FileDialog object was introduced in 2002 so you're right it won't work in 2000, but you can still use the Dialogs object in Word (Look it up in Word VBA Help)

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •