Visio Guy

Visio Guy Website & General Stuff => User-submitted Stuff => Topic started by: wapperdude on July 14, 2017, 06:45:55 PM

Title: VBA: find, select, insert external file: pictures, Excel, etc
Post by: wapperdude on July 14, 2017, 06:45:55 PM
The following code can be used to open Windows Explorer and then search, select, and insert a file into Visio, without having to leave or exit the VBA macro.  It uses the FileDialog(msoFileDialogFilePicker).  However, this cannot be run directly from Visio...as far as I can tell.  The trick is to invoke Excel (or Word, Power Point), find and select file, and then use Visio to insert the file. 

This very similar to the topic, http://visguy.com/vgforum/index.php?topic=7450.0 (http://visguy.com/vgforum/index.php?topic=7450.0), which focused on finding an Excel file, and then selecting the desired range of cells to be imported into Visio.

The focus of this code is picture files.  These are set up by the add filter line in the code.

Enjoy!


Sub selFile()
    Dim XlApp As Object
    Dim docPath As Variant
   
    docPath = ActiveDocument.Path                   'This is the default search path
    Set XlApp = CreateObject("Excel.Application")   'This is dummy.  FileDialog doesn't work with Visio directly.

' Find, select, and insert file:
    With XlApp.FileDialog(msoFileDialogFilePicker)
        .Filters.Clear
        .Filters.Add "Picture Files", "*.jpg, *.png"
        .InitialFileName = docPath
        .Show
        Application.ActiveWindow.Page.Import .SelectedItems(1)
    End With
    XlApp.Quit
End Sub


Wapperdude