VB: Connecting to SW Clarification

I have seen people use 3 different ways

Dim swApp as Sldworks.SldWorks

Set swApp = New SldWorks.SldWorks Set swApp = Application.SldWorks Set swApp = CreatObject ("SldWorks.Application")

Can anyone explain why you would use one instead of another. Or how you choose which one to use. TIA

Corey

Reply to
Corey Scheich
Loading thread data ...

assigns a new object of the SldWorks.SldWorks class. Never used it. Does it work ?

works only in macros, because "Application" is defined as the one which hosts the VBA

you mean CreateObject, with an "e". This one starts a new SW session. It may therefore return an application which is not the one you see on your screen...

I use a 4th one : Set swApp = GetObject (,"SldWorks.Application")

which connects to an existing SW session (usually there is only 1...)

Reply to
Philippe Guglielmetti

The statement "Set swApp = Application.SldWorks" can only be used in SW macros as a means of attaching to the SW session from which the macro was called. This should not be used in an application that automates SW externally.

The last one you mention, CreateObject, has been the standard means of attaching to or starting a SolidWorks session if one was not already active.

Generally in working with object models there are alternate ways of instantiating the root objects off of which the other objects can be derived. For example, with the FileSystemObject:

Dim X as FileSystemObject (declare) Set X = New FileSystemObject (instantiate)

----------OR---------------------------------- Dim X as FileSystemObject (declare) Set X = CreateObject("Scripting.FileSystemObject") (instantiate)

----------OR---------------------------------- Dim X as New FileSystemObject (declare and instantiate all in one line)

I know that originally SolidWorks did not support the other methods of instantiation (using "New"). So I have gotten in the habit of always using the "CreateObject" and "GetObject". I guess I will have to give the other a try and see if it works now.

Regards, Brenda

----------------------------------------------- Brenda D. Bosley, PE CustomSolids

Reply to
Brenda D. Bosley

ive had problems with "Set swApp = Application.SldWorks" or "Set swApp = CreatObject ("SldWorks.Application")". cant remember which one and why it was a problem. i always use "Set swApp = New SldWorks.SldWorks" and never had a problem with it.

Reply to
Sean Phillips

This is the older of the versions, along with using "Getobject". This was before SW added an 'Application' interface. If you are using older versions of SW, then you may want to try "GetObject" before "CreateObject", back then you could start a second instance of SW. This method also does not require early binding.

This is the newer style, using the 'application' interface. Not only wont work on older SW/VBA, but is finicky during newer upgrades as well.

This I havent seen anywhere else, but Ive used similar on some SW objects. The 'NEW' keyword starts a new class instance, whether its a SW app, a Excel WorkBook, or a VB form. My guess is the guy who posted code with this in it is an 'old timer' VB programmer. However, this STILL wont start a second instance of SW, at least using the last few vers.

Reply to
rocheey

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.