Search code examples
vbamacossolidworks

VBA Flexible file address to open the file


I have a macro on a flash drive that I want to run on different computers. After running, this macro opens a file located in the macro's own folder in the flash drive.

Set Part = swApp.OpenDoc6("F:\RSO_Regal.SLDASM", 2, 0, "", longstatus, longwarnings)
Set Part = swApp.OpenDoc6("RSO_Regal.SLDASM", 2, 0, "", longstatus, longwarnings)

My problem is that every time I connect a flash drive to a new computer or copey the folder in new directory, I have to edit the file address in the macro. Is there a solution to this problem?

There is a solution to this problem for Microsoft Excel in this post. Open File Without Calling Filepath However, my macro is for SolidWorks software and it opens a file with *.SLDASM format.


Solution

  • Get the macro path using GetCurrentMacroPathName and use that to open the file. Check the changed line below.

    Set Part = swApp.OpenDoc6(Left(swApp.GetCurrentMacroPathName, InStrRev(swApp.GetCurrentMacroPathName, "\")) & "RSO_Regal.SLDASM", 2, 0, "", longstatus, longwarnings)