0

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.

EuanM28
  • 258
  • 3
  • 14
Maisam
  • 3
  • 3
  • 1
    Will placing the macro file in the same folder of the `SLDASM` file and using `ThisWorkbook.Path` in place of drive or path letter would work like `Set Part = swApp.OpenDoc6(ThisWorkbook.Path & "\RSO_Regal.SLDASM", 2, 0, "", longstatus, longwarnings)`? – Dhay Dec 30 '22 at 11:21
  • I changed the code and got the following error message. `Set Part = swApp.OpenDoc6(ThisWorkbook.Path & "\RSO_Regal.SLDASM", 2, 0, "", longstatus, longwarnings)` Error: **Run-time error '424'** **Object required** Note: VBA macro is for Solidworks CAD software. I don't want open an Excel Workbook. Thanks – Maisam Dec 31 '22 at 13:04

1 Answers1

0

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)
  • Thank you very much. This code Work. There is only one problem. The output of this code is **F:\RSO_Regal.swpRSO_Regal.SLDASM**. But I need only the file path without the macro name and extension: **F:\RSO_Regal.SLDASM**. Note: **RSO_Regal.swp** is the name and extension of macro data – Maisam Jan 02 '23 at 17:26
  • It seems that SO didn't show the \ between the quotes. So add a \ between the quotes in this line **swApp.GetCurrentMacroPathName, ""** I've also updated the above answer. – Deepak Gupta Jan 03 '23 at 06:30