0

In Solidworks I record two macros.

Macro 1 is empty:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

'added code
Dim distance_of_second_plane
'end of added code

Sub main()

    Set swApp = _
    Application.SldWorks

    Set Part = swApp.ActiveDoc
    'added code:  here I want to call the second macro and send it distance_of_second_plane, and have it use that value
     distance_of_second_plane = .05
     '.. now what?

    'end of added code, don't know what to add.

End Sub

Macro 2 does something that requires data from macro 1:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

    Set swApp = _
    Application.SldWorks

    Set Part = swApp.ActiveDoc
    boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Dim myRefPlane As Object
    Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.05334, 0, 0, 0, 0)
    Part.ClearSelection2 True

End Sub

these macros are of course saved in different files. How do I call the second from the first, passing in the data from the first, and using it in the second?

things I`ve tried: http://support.microsoft.com/kb/140033, http://www.cadsharp.com/macros/run-macro-from-another-macro-vba/, VBA module that runs other modules, Call a Subroutine from a different Module in VBA

all of them are problematic. I'll give details of the errors I get if asked for them.

Community
  • 1
  • 1
juggler
  • 319
  • 1
  • 5
  • 16

2 Answers2

1

You could create a separate module to encapsulate your common methods. Then add reference to that module and call it from both your Macro1 and Macro2 sub routines.

So for example in your Macro1:

  1. In the project explorer, right click on the Modules folder and Insert-Module, name in Module1 (or whatever you want)

enter image description here

  1. Create a subroutine in Module1, name it InsertPlane (or whatever name makes sense), and construct the subroutine with parameters that will be needed to accomplish what needs to be done

    Sub InsertPlane(ByRef swApp As Object, ByVal distance As Double)
    
        'enter your code here, the parameters passed to InsertPlane can be whatever you need
        'to do what the method needs to do.
    
    End Sub
    
  2. In your main() method of your macro, call the InsertPlane() method when you need it

    Sub main()
    
    Set swApp = _
    Application.SldWorks
    
    Set Part = swApp.ActiveDoc
    'added code:  here I want to call the second macro and send it distance_of_second_plane, and have     it use that value
     distance_of_second_plane = 0.05
    
     'example of calling the subroutine
     boolstatus = Module1.InsertPlane(swApp, distance_of_second_plane)
    
    End Sub
    
  3. The modules can be exported and imported, so you can reuse them in other macros, to do that right click in the project tree on Module1 and export file. Similarly, you can right click on the Modules folder and Import modules.

Hope this helps.

Jeff Anderson
  • 799
  • 7
  • 18
  • I appreciate your effort in answering. I can give it a try, but don`t have the time any more to do the research to track down how. other priorities. if you can give me a simple but complete example of how to do this, that would be much appreciated. – juggler Aug 26 '14 at 01:46
0

While it is possible to run a macro from another macro using ISldWorks::RunMacro2, it is not possible to have this method return any values that you can use in your first macro. Even if this was possible, I would not recommend it.

Everything you need to accomplish can be accomplished in a single macro, you simply need to learn how to use the SolidWorks API to reach that end. Could you please explain what you are trying to accomplish with your macro? Then I could show you what code you need.

Please also note that the macro recorder is really not a good tool for creating macros of any significance. If you plan on using the SolidWorks API seriously, then you really need these three skills under your belt:

  1. How to do basic programming with VBA (variables, arrays, conditionals, loops, etc)
  2. How to navigate and read the SolidWorks API Help (the offline version)
  3. How API objects are related to one another (SolidWorks API Object Model)

I have some videos at my web site (in my profile) that can help you get started. Again, if you want me to help here with your current problem, please explain what you're trying to automate.

cadsharp
  • 397
  • 3
  • 4
  • 16
  • "Everything you need to accomplish can be accomplished in a single macro, you simply need to learn how to use the SolidWorks API to reach that end. Could you please explain what you are trying to accomplish with your macro? Then I could show you what code you need." I have no doubt of this. I now have a macro that builds a fairly substantial fraction of an airplane wing for me. The objective of calling one macro from another is to split up the code. it's hard to find and edit individual parts of it when it's, like, 16 screens long (I haven't counted how many screens it is..) – juggler Sep 20 '14 at 15:17
  • you will have noticed that yours is one of the sites I referenced in my search for solutions. thank's to it, I do currently have loops in my program. :-) I'll be getting back to this in a big way after oct. 17. we'll see how things go then :-) – juggler Sep 20 '14 at 15:22
  • Glad you found some of my tutorials helpful. Are you familiar with creating sub-procedures, functions, and modules in VBA? Because these are typically what you would use to modularize your code. By the way, my "you simply need to learn..." comment was not intended to be condescending, I apologize if it came across that way. – cadsharp Sep 20 '14 at 22:14