Application.SldWorks or CreateObject("SldWorks.Application") ?

Hi there,

Can someone please explain the difference between:

Set swApp = Application.SldWorks

or

Set swApp = CreateObject("SldWorks.Application")

Why should you use one or another ?

Greetings !

SolidworksApi macros
Answers 7
AT
AT 2014-06-12 Best Answer

First line is a static variable set in macro. You can only use it within SolidWorks macro while Create/GetObject is VB6 function you can use it in any VB6 compatible projects.

______________________________________________

Regards, Artem Taturevych | Snr. Developer | IC3D ANZ

IC3DSteel – New Steel Solution for SolidWorks

LinkedIn - SolidWorks API Group

SF
SF 2014-06-12

In addition to what Artem has mentioned,

Application.SldWorks gives you current running instance of SolidWorks whereas CreateObject("SldWorks.Application") gets and creates an instance of the default version from the registry, if you search for HKEY_CLASSES_ROOT\SldWorks.Application\CLSID you can find which version is "Solidworks.Application" using by default.

This might lead to issues if multiple versions are installed on the machine, for instance if you are running SW 2014, when you use CreateObject("SldWorks.Application") it creates an instance of SW 2013 if that is the default version in registry.

If you want to use CreateObject or GetObject then include the version that you want to use, in the class string.
e.g.
for SW 2012
CreateObject("SldWorks.Application.20")
for SW 2013
CreateObject("SldWorks.Application.21")
for SW 2014
CreateObject("SldWorks.Application.22")

CM
CM 2014-06-12

Thanks Santosh for this detailed information.

Where can I find these things, in the help section ?

Greetings !

SF
SF 2014-06-12

This information is actually generic information which I presented in SolidWorks context, the same goes for other apps as well e.g. Excel

AJ
AJ 2018-01-01

The difference is that the first the compiler binds the variables' type/method calls at compile time. This basically is like replacing method names with memory addresses for the compiler. The compiler has knowledge of which types/methods to bind thanks to the libs you have set from your Tools > References. The downside of this is that if you mess up your references, then your code will not compile. You are not the compiler anything to bind to!

Consequently, your code can compile without setting any references. Here, you are giving the compiler the task of doing the binding during the runtime of the macro. The problem is here is all of your variables during compile time must be defined as objects (since you have no references set). The good thing about late binding is that excellent for resolving version conflicts. 

Unless you memorize the solidworks api method calls by heart for every version, you're probably better off using early/static binding.

Cheers,
Amen
SolidWorks API Training & Services 

SA
SA 2018-01-03

If you want to get the running SolidWorks session use

Set swApp = GetObject(, "SldWorks.Application")

JK
JK 2024-05-23

Using Application.sldworks instead of the create object command i resolved an issue 'Run Time Error -2147221164(80040154) Class Not Registered' Only some users of my macro experienced this problem due to old SW-version installations messing up registry keys.