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
Didn't find your answer? Ask the community — no account required.
P
Philippe Guglielmetti
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...)
B
Brenda D. Bosley
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
S
Sean Phillips
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.
R
rocheey
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.
Join the Discussion
Have something to add? Share your thoughts — no account required.
Didn't find your answer?
Ask the community — no account required
Report Content
You are reporting this content to the moderators. They will look at it
ASAP.