Run a macro from another one

COE Administrator

Run a macro from another one

Hi everyone!

Important question: Is it possible to run, for example a VBScript macro from a VBA macro?

Thanks in advance!

Doraemon

Author: Doraemon

COE Administrator

Run a macro from another one
(in response to COE Administrator)
The answer is... Yes. From within your macro you make a call to the CATIA.SystemServices object. Here is an example of how it is done.

Language="VBSCRIPT"

Sub CATMain()

Set partDocument1 = CATIA.ActiveDocument

Set part1 = partDocument1.Part

Dim params()

CATIA.SystemService.ExecuteScript"path", catScriptLibraryTypeDirectory, "Macro2.catvbs", "CATMain", params

MsgBox "Original Macro resumes"

End Sub

The argument catScriptLibraryTypeDirectory can be a different value if the macro is stored internally to a CATPart. In that case it would be catScriptLibraryTypeDocument. A third type would be if the macro was written in a VBA project. In that case it would be catScriptLibraryTypeVBAProject.

I've tried the above example and it does work. It executes the called macro seemlessly without hesitation. The calling macro starts execution and then executes the called macro. Once the called macro finishes, the calling macro completes its execution. So, you could have an if statement in the middle that calls different macros depending on the tests, continue execution and do different operations.

Regards,
Bob P.

Author: Robert M Petersen

COE Administrator

Run a macro from another one
(in response to COE Administrator)
I was unable to get a VBA project to execute from this method. But, if you have a compiled VBA project (an EXE application) you can execute the VBA application in the following way.

Language="VBSCRIPT"

Sub CATMain()

call CATIA.SystemService.ExecuteBackgroundProcessus("G:\MyProjectsfolder\MyApplication.exe")

End Sub

I'm not sure if this is really what you want. I think you are using the VBA editor that comes with CATIA and not VBA 6.0 that is purchased seperately. I have not used CATIA's VBA editor. I use VBA 6.0 to write my standalone applications. I then execute them with the above method. If I get a chance to look into it further, I'll see what I can find out.

Regards,
Bob P.

Author: Robert M Petersen

COE Administrator

Run a macro from another one
(in response to COE Administrator)
Try this instead of an .exe. In VB 6.0, write your program as an ActiveX DLL, complete with properties and methods. You will use the properties to pass data to and from your VB Script, and the methods to manipulate the data once it's in the .dll.

In your .catvbs program, you would use the CreateObject function to instantiate the class inside your .dll, like so:

Set myDLL = CreateObject("myApp.myClass")
myDLL.OldValue = in
myDLL.DoYourThing
out = myDLL.NewValue


Author: Phil Runninger