Author Topic: VBA: find, select, insert external file: pictures, Excel, etc  (Read 1088 times)

0 Members and 1 Guest are viewing this topic.

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3457
  • Ideas Visio-lized into solutions
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.

Enjoy!

Code: [Select]
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
Visio 2019 Pro