News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

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.

wapperdude

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!


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

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 143 (show)
Files included: 32 - 1207KB. (show)
Memory used: 1003KB.
Tokens: post-login.
Cache hits: 13: 0.00180s for 26,739 bytes (show)
Cache misses: 2: (show)
Queries used: 15.

[Show Queries]