Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
This tutorial describes how to create or get an object (useful when you want to link to another Microsoft application). What I'll do is first show how to do this, then consider the pros and cons of the approach.
This tutorial gives a summary of an alternative way to create references. If you want to know more about the subject - including why you might want to create a reference in the first place, and what this means - read my earlier tutorial on how to create references in VBA.
If you are already running an application like Word, you can get a reference to this running application using GetObject:
Sub PutCellInWord()
Dim WordApp As Object
'get reference to running copy of Word
Set WordApp = GetObject(Class:="Word.Application")
WordApp.Visible = True
'add a new document, and show contents of cell A1 in it
WordApp.Documents.Add
WordApp.Selection.TypeText (Range("A1").Value)
WordApp.Selection.TypeParagraph
End Sub
Notice that in the above code you can't declare WordApp as a Word application - you have to be vaguer, and just say it's an object.
What happens if the application you're trying to link to isn't running (or worse, if it might be, and you're not sure)? The answer is to try getting a reference to a running copy of the application, and if this fails create a new one. If this sounds complicated, the code is fairly standard and easy:
Option Explicit
'variable to hold reference to Word
Dim WordApp As Object
Sub PutCellInWord()
'get a reference to a copy of Word (new or existing)
GetWordReference
'make copy of Word visible
WordApp.Visible = True
'add a new document, and show contents of cell A1 in it
WordApp.Documents.Add
WordApp.Selection.TypeText (Range("A1").Value)
WordApp.Selection.TypeParagraph
End Sub
Sub GetWordReference()
'if Word isn't open, jump to this label
On Error GoTo NoCurrentWord
'try to get a reference to running copy of Word
Set WordApp = GetObject(Class:="Word.Application")
'if we get here, Word was open ==> can leave
Exit Sub
NoCurrentWord:
'if we get here, Word wasn't open - so create a new copy
Set WordApp = CreateObject(Class:="Word.Application")
End Sub
The above code calls the GetWordReference procedure to get a reference to Word (whether to an open copy, or failing this to a newly created one).
Why would you use CreateObject/GetObject rather than references? The disadvantages include:
The above code uses something called late binding. What this means is that VBA doesn't know that the variable WordApp refers to a Word application until run time, which means it can't optimise the code when it's compiled and will run slightly more slowly.
Another consequence of this late binding is that you can't use autocompletion (intellisense) when writing the above macro, since VBA doesn't know what sort of thing WordApp is at this point.
The advantages include:
The references above aren't version specific (they'll work whichever version of Word you have on your computer).
The code above uses an existing copy of Word where one is available, avoiding the unnecessary overhead of creating a new application.
As implied above, my recommendation for most people would be to ignore this tutorial!
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.