Link to home
Start Free TrialLog in
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

Using VBA, how can I display an Insert Picture dialog in PPT 2007, pointing to a directory of my choice


Trying to build a macro in VBA, for use in Powerpoint, that presents an InsertPicture dialog pointing to a pre-determined folder.  I understand PPT does not give the same access to built-in dialog boxes I have in Word, but is there a workaround?

Here's the code I used to accomplished this in Word:

    Dim defpath As String
'Get the present default filepath for pictures
    defpath = Options.DefaultFilePath(wdPicturesPath)
'Change it to desired folder
    Options.DefaultFilePath(wdPicturesPath) = "C:\TemplateDemo\Graphics\"
    With Dialogs(wdDialogInsertPicture)
    End With
'Change the default filepath back to the original default
    Options.DefaultFilePath(wdPicturesPath) = defpath

I already tried substituting "pp.." for "wd..." but no such luck.

I also found online the following snippet which does display an insert picture dialog (pointing to the default "my pictures" location) in PPT 2007, but it was clearly coded for legacy versions.

Application.CommandBars("Menu bar").Controls("&Insert").Controls("&Picture").Controls("&From File...").Execute

I'd be happy to use this if I can control where it points.  

Any suggestions?

Thanks in advance
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello versatilebb,

With appropriate editing to point to your directory and file types then ...


Option Explicit

    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 Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenFileName As OPENFILENAME) As Long

Public Const OFN_EXPLORER = &H80000
Public Const OFN_FILEMUSTEXIST = &H1000&
Public Const OFN_HIDEREADONLY = &H4&
Public Const OFN_PATHMUSTEXIST = &H800&

Sub ShowFileOpenDialog(ByRef FileList As Collection, Optional strRootDir As String)
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim FileDir As String
    Dim FilePos As Long
    Dim PrevFilePos As Long

    With OpenFile
        .lStructSize = Len(OpenFile)
        .hwndOwner = 0
        .hInstance = 0
        .lpstrFilter = "Image Files" + Chr(0) + "*.bmp;*.jpg;*.jpeg;*.jpe" + _
            Chr(0) + "All Files (*.*)" + Chr(0) + "*.*" + Chr(0) + Chr(0)
        .nFilterIndex = 1
        .lpstrFile = String(4096, 0)
        .nMaxFile = Len(.lpstrFile) - 1
        .lpstrFileTitle = .lpstrFile
        .nMaxFileTitle = .nMaxFile
        If strRootDir = "" Then
            .lpstrInitialDir = "C:\"
            .lpstrInitialDir = strRootDir
        End If
        .lpstrTitle = "Load Images"
        .flags = OFN_HIDEREADONLY + _
            OFN_PATHMUSTEXIST + _
            OFN_FILEMUSTEXIST + _
        lReturn = GetOpenFileName(OpenFile)
        If lReturn <> 0 Then
            FilePos = InStr(1, .lpstrFile, Chr(0))
            If Mid(.lpstrFile, FilePos + 1, 1) = Chr(0) Then
                FileList.Add .lpstrFile
                FileDir = Mid(.lpstrFile, 1, FilePos - 1)
                Do While True
                    PrevFilePos = FilePos
                    FilePos = InStr(PrevFilePos + 1, .lpstrFile, Chr(0))
                    If FilePos - PrevFilePos > 1 Then
                        FileList.Add FileDir + "\" + _
                            Mid(.lpstrFile, PrevFilePos + 1, _
                                FilePos - PrevFilePos - 1)
                        Exit Do
                    End If
            End If
        End If
    End With
End Sub

Sub SelectFiles()
    Dim FileList As New Collection
    Dim I As Long
    Dim S As String

    ShowFileOpenDialog FileList, "C:\TemplateDemo\Graphics\"
    With FileList
        If .Count > 0 Then
            S = "The following files were selected:" + vbCrLf
            For I = 1 To .Count
                S = S + .Item(I) + vbCrLf
            MsgBox S
            MsgBox "No files were selected!"
        End If
    End With
End Sub

Open in new window

Avatar of Anthony2oo5

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

With apologies to the source:

The above works fine ... for example place it in a code module on it's own and then you don't need to doi much other than call it when needed and take the string value which is the file(s) list in the collectioon S.

Avatar of Bryce Bassett


This is my kind of solution:  simple but effective.  This does everything I need it to do
Thanks for the code, Chris, but all I really needed was Anothony2oo5's simpler solution.   I may look into this if I ever need to do anything fancier.

Thanks, Anthony.