Page 1 of 1

Running into trouble when converting vba code

Posted: 07 May 2021, 10:33
by colt
I am converting this vba code to run in ahk.

Code: Select all

Set swApp = Application.SldWorks
'Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
Set pg = swModel.Extension.GetPackAndGo
So far I've been able to convert most of my macros over to ahk, but this one is giving me trouble. It seems that the line "Set pg = swModel.Extension.GetPackAndGo" fails unless swModel is defined ahead of time by "Dim swModel As SldWorks.ModelDoc2". This is the first time I've ran across this in their api, usually you can skip the definition but in this case it seems like it is required. Am I SOL or is there a way to initialize as that type in autohotkey?

Re: Running into trouble when converting vba code  Topic is solved

Posted: 08 May 2021, 09:47
by swagfag
u dont have to declare types in ahk. ur problem is caused due to something else

Re: Running into trouble when converting vba code

Posted: 07 Sep 2021, 15:44
by colt
For anyone else interested in this, I found that you have to pass comvar element to this function. It is not documented this way, and the returned object is empty. But it does not crash ahk now. Probably need to pass it a specific object...

Code: Select all

swModel := swApp.activeDoc
pgOut := comVar(varType:=9)
swPackAndGo := swModel.Extension.GetPackAndGo(pgOut.ref) ;seems to run correctly with long delay but return value is empty
swPackAndGo.GetDocumentNamesCount ;empty

Re: Running into trouble when converting vba code

Posted: 15 Jul 2022, 00:42
by colt
Solidworks support told me that some of their functions don't work when using late binding. This read helped me understand better https://nolongerset.com/early-binding-vs-late-binding/. I bet their object does not have the IDispatch, causing it to fail. I guess this function is unusable until they update their code?

Re: Running into trouble when converting vba code

Posted: 15 Jul 2022, 04:18
by swagfag
no, the function is usable. u just have to find a way to call it. if they didnt bother implementing IDispatch, then ud have to call it by the method's address/position in the object's vtable(ie use ComCall if ure using v2, or check out this example if u arent https://www.autohotkey.com/docs/commands/ComObjQuery.htm#ExClassName). of course, to do that ud have to know the internal structure of the object ure working with(IModelDocExtension or IPackAndGo, depending on which call u figured out was erroring out) and to know that, ud need to have either:
  • some .header files
  • a .tbl of the object
  • or figure the object's layout by trial & error, gl doing that