VB: Connecting to SW Clarification

Discussion in 'SolidWorks' started by Corey Scheich, Nov 17, 2003.

  1. 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
     
    Corey Scheich, Nov 17, 2003
    #1
  2. Dim swApp as Sldworks.SldWorks
    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...)
     
    Philippe Guglielmetti, Nov 17, 2003
    #2
  3. 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, Nov 17, 2003
    #3
  4. 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.
     
    Sean Phillips, Nov 17, 2003
    #4
  5. Corey Scheich

    rocheey Guest

    Set swApp = CreatObject ("SldWorks.Application")

    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.
     
    rocheey, Nov 17, 2003
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.