Search code examples
vbasolidworks

Load image in UserForm from same path as the VBA macro file


I would like to keep some images in the same directory as my macro file and then load them into my UserForm. This is for a SolidWorks macro, if that matters. The image loads if I put the full path, but this macro will be deployed onto several workstations, so a path relative to the SWP/macro file is desired.

Couldn't be this lucky:

Private Sub RENTRY_Click()
Image1.Picture = LoadPicture(".\IMAGE.bmp", 552, 358, Default)
End Sub
Private Sub RENTRY_Click()
Image1.Picture = LoadPicture("IMAGE.bmp", 552, 358, Default)
End Sub

Solution

  • You can use GetCurrentMacroPathFolder to find the path to the currrently-executing macro file

    Dim swApp As SldWorks.SldWorks
    Dim pathname As String
    
    Set swApp = Application.SldWorks
    pathname = swApp.GetCurrentMacroPathFolder