VBA: find, select, insert external file: pictures, Excel, etc

Started by wapperdude, July 14, 2017, 06:45:55 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


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, 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.


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.Add "Picture Files", "*.jpg, *.png"
        .InitialFileName = docPath
        Application.ActiveWindow.Page.Import .SelectedItems(1)
    End With
End Sub

Visio 2019 Pro