Consulting

Results 1 to 3 of 3

Thread: Outlook 2016 File / Folder Browser

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location

    Outlook 2016 File / Folder Browser

    I have a OL macro that opens and reads a CSV, but I want to replace the hard-coded path with a dialog box that allows the user ( = Me) to pick a file and return the path to the macro

    Apparently there is no inherent OL VBA capability like Excel's GetOpenFileName

    I found some snippets via Google, but they don't seem to work, at least not with 2016

    Does anyone have OL macros that allow the user to pick a folder and a file?

    Thanks

    Edit:

    The typical approach uses this technique

    http://www.robvanderwoude.com/vbstech_ui_fileopen.php

    but the Set objDialog line throws error 429, ActiveX component can't create object

     ' Dependencies:
     ' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and later.
     ' To use the default "My Documents" WScript.Shell is used, which isn't
     ' available in HTAs.
     '
     ' Written by Rob van der Woude
     ' http://www.robvanderwoude.com
         ' Standard housekeeping
         Dim objDialog
         ' Create a dialog object
         Set objDialog = CreateObject("UserAccounts.CommonDialog")

    I'm assuming that it has something to do with NUSRMGRLib
    Last edited by Paul_Hossler; 09-08-2017 at 05:08 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  2. #2
    It's an unfortunate omission from the Outlook code set, but you can use the one from Excel and if Excel is already running or you have a SSD drive it is quite fast

    Option Explicit
    
    Function BrowseForFolder(Optional sFolder As String) As String
    Dim exApp As Object
    Dim strPath As String: strPath = ""
    Dim fldr As FileDialog
    Dim bStarted As Boolean
        On Error Resume Next
        Set exApp = GetObject(, "Excel.Application")
        On Error GoTo 0
        If exApp Is Nothing Then
            Set exApp = CreateObject("Excel.Application")
            bStarted = True
        End If
        Set fldr = exApp.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder"
            .InitialFileName = sFolder
            .AllowMultiSelect = False
            If .Show <> -1 Then GoTo lbl_Exit
            strPath = .SelectedItems(1) & Chr(92)
        End With
    lbl_Exit:
        BrowseForFolder = strPath
        If bStarted = True Then exApp.Quit 'Optional
        Set exApp = Nothing
        Exit Function
    End Function
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I did find something that I think I made work (at least it hasn't failed in 24 hours) to pick a file to open. Still looking for same for folder

    Option Explicit
    'http://www.vbforums.com/showthread.php?400121-Outlook-2003-How-to-show-a-filedialog-from-Outlook
    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
    Private Declare Function GetOpenFileNameAPI Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    
    Function GetOpenFileName(Optional sFolder As String = vbNullString, _
        Optional sMask As String = "CSV Files (*.csv)|*.csv, All files (*.*)|*.*", _
            Optional sTitle As String = "Select File to Open") As String
        
        Dim OFName As OPENFILENAME
        Dim s As String
        
        With OFName
            .lStructSize = Len(OFName)
            .hwndOwner = 0
            .hInstance = 0
            
            s = sMask
            s = Replace(s, ",", Chr(0))
            s = Replace(s, "|", Chr(0))
            .lpstrFilter = s
            .lpstrFile = Space$(254)
            .nMaxFile = 255
            .lpstrFileTitle = Space$(254)
            .nMaxFileTitle = 255
            
            If Len(sFolder) = 0 Then
                .lpstrInitialDir = CreateObject("Shell.Application").NameSpace(CVar(0)).Self.Path
            Else
                .lpstrInitialDir = sFolder
            End If
            
            .lpstrTitle = sTitle
            .flags = 0
        
            If GetOpenFileNameAPI(OFName) Then
                GetOpenFileName = Trim$(.lpstrFile)
            Else
                GetOpenFileName = vbNullString
            End If
        End With
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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