vbams-officevisio

Export Selection in Microsoft Visio to picture file using VBA


I am having a MS Visio diagram. Often I need to export the part of the drawing to a picture file to use it somewhere else, but I am really tired of using menus.

I want to export some selected elements right to .bmp, .img, png, or other picture file without using menus bu with given filepath and defined resolution in pixels per inches.

Selected elements in Visio

After some trials I have found the working VBA code and I am going to share it here.


Solution

  • My solution to this problem:

    Sub ExportSelectionToBMP()
        Dim vsoSelection As Visio.Selection
        Dim vsoPage As Visio.Page
        Dim exportPath As String
        Dim resolution As Double
      
    
        exportPath = "C:\Users\USERNAME\Desktop\11.bmp"
        resolution = 140
        
        Application.Settings.SetRasterExportResolution visRasterUseCustomResolution, resolution, resolution, visRasterPixelsPerInch
        
        
        
        Set vsoWindow = ActiveWindow
        Set vsoSelection = ActiveWindow.Selection
        
    
        If Not vsoSelection Is Nothing Then
            vsoSelection.Export (exportPath)
            MsgBox "Done.", vbInformation
              Else
            MsgBox "No selection", vbExclamation
        End If
    End Sub