Extensible CAD Technologies
SolidWorks and PDMWorks Enterprise Development Blog

GetObject( ) or CreateObject( )? Is there a difference? – Part III

Posted by at 11 October, 2007, 1:24 am

At the end of my last post on this topic, I promised you that I would share a “dirty secret” so I guess I now have to come clean. FYI – “Dirty secret” may have been a bit strong but it sounded cool (at least to me). Anyways, there is a difference between how SolidWorks handles CreateObject( ) and how MS Office products handle CreateObject( ). This is something to be aware of when automating across applications.

What is the difference? In a nutshell, calling CreateObject( ) on an MS Office application interface will always create a new instance of the application and then return a handle to that instance. Calling CreateObject( ) on the SldWorks.Application interface will only create a new instance of SolidWorks if one isn’t already running. This basically ensures that only one instance of SolidWorks at a time is ever automated by your Macro. This is a basic coding pattern called “the Singleton” and it prevents untold suffering and countless tears. SolidWorks is so kind to us and many of us didn’t even know it.

Below are two Macros to illustrate the difference between SolidWorks and MS Office applications with regard to CreateObject( ). The first is an Excel Macro that automates SolidWorks and the second is a SolidWorks Macro that automates Excel. If you copy/paste the code, be sure you add references in each Macro project to the object library of the application you are automating. If you don’t know what the heck I am talking about, then just download the complete files from here.

  • Excel Macro to automate SolidWorks:

‘Copyright 2007 by Extensible CAD Technologies, LLC – All rights reserved
‘Written 10/10/2007 by Jeff Cope
‘User assumes all risk for using the code below.

Sub AutomateSolidWorks()

‘Declare three handles to SolidWorks instances.
Dim swx1 As SldWorks.SldWorks
Dim swx2 As SldWorks.SldWorks
Dim swx3 As SldWorks.SldWorks

‘Use CreateObject( ) for the first instance. A new instance of SolidWorks IS NOT created.
‘Note: As we showed previously, using GetObject( ) without a filepath argument here would
‘return an error since no instances of SolidWorks are available to attach to yet.

Set swx1 = CreateObject(“SldWorks.Application”)
‘Use CreateObject( ) for the first instance. A new instance of SolidWorks IS NOT created.
Set swx2 = CreateObject(“SldWorks.Application”)
‘Use GetObject( ) for the first instance. A new instance of SolidWorks IS NOT created.
Set swx3 = GetObject(, ”SldWorks.Application”)
‘At this point, we should only have one SolidWorks window open and one SolidWorks process running.
‘Add new parts using each of the three handles

swx1.NewPart
swx2.NewPart
swx3.NewPart
‘We should now have one SolidWorks window open with three new SolidWorks parts loaded.

End Sub

  • SolidWorks Macro to automate Excel:

‘Copyright 2007 by Extensible CAD Technologies, LLC – All rights reserved
‘Written 10/10/2007 by Jeff Cope
‘User assumes all risk for using the code below.

‘Declare three instances of the Excel.Application interface
Dim xl1 As Excel.Application
Dim xl2 As Excel.Application
Dim xl3 As Excel.Application

Sub main()

‘Use CreateObject( ) for the first instance. A new instance of Excel is created.
Set xl1 = CreateObject(“Excel.Application”)
‘A new Workbook gets added to this instance
xl1.Workbooks.Add
‘Make the instance visible so we can see what is happening
xl1.Visible = True

‘Use CreateObject( )for the first instance. A new instance of Excel is created.
Set xl2 = CreateObject(“Excel.Application”)
‘A new Workbook gets added to this instance
xl2.Workbooks.Add
‘Make the instance visible so we can see what is happening
xl2.Visible = True
‘At this point, we should be able to see two Excel windows running and each should have a
‘new Workbook added.

‘Use GetObject( ) for the first instance. A new instance of Excel IS NOT created. A handle
‘is simply returned to one of the existing instances (usually xl1).

Set xl3 = GetObject(, “Excel.Application”)
‘A new Workbook gets added to this instance
xl3.Workbooks.Add
‘Make the instance visible so we can see what is happening
xl3.Visible = True
‘At this point in the Macro’s execution, we should see two Excel windows (and two Excel processes)
‘running. However, we have three handles to Excel instances so two of the handles are pointing
‘to the same Excel instance.

‘Release handles to COM objects
Set xl1 = Nothing
Set xl2 = Nothing
Set xl3 = Nothing

‘One of the Excel windows will have two Workbooks in it instead of one. Note: If you
‘want to know which one, then just leave one of the instances invisible.
End Sub

If you enjoyed this post, make sure you subscribe to my RSS feed!

Category : CAD | Macros | MS Office | SolidWorks | SolidWorks API

2 Responses to “GetObject( ) or CreateObject( )? Is there a difference? – Part III”


Michelle Gabele October 13, 2007

Excellent article. Thanks for the in-depth treatment of the subject and keep these coming.

Maarten October 20, 2010

Hello,
I’m glad I stumbled onto your site. It’s really great!
Maybe you can help me with the following:
I have a workstation with multiple versions of Solidworks installed.
How can I select a specific version using the createobject method?



Generate First-Article Inspection Reports From TIF, PDF and CAD Drawing Files With InspectionXpert. Generate First-Article Inspection Reports From SolidWorks Drawing Files With InspectionXpert.