How to Determine Object Methods and Properties
Post date: Dec 3, 2013 1:09:26 PM
Windows Operating systems come with many small apps such as Windows Media Player. Some of those apps are known as COM objects1. COM objects can be manipulated by VBA. To manipulate an object, we need to know what methods it exposes and what properties it has.
MSDN
The best place to learn about object properties is to Google “MSDN” object’s-name “Object Model” (Example "MSDN WIndows Media Player Object Model"). Unfortunately, MSDN is not complete. In such cases we can Google just the object’s name and hope someone else has written something. When this fails us ....
Object Browser
VBE’s object browser2 is often helpful but it too is not complete.
IntelliSense and References:
We can add an object reference and create a variable over an instance of the object which we then use to explore its properties. To do this we:
Use VBE’s menu option “Tools” > “References”
Find the app we want and select/check it to add the reference
Write a small routine like this:
Sub Temp()
Dim WMP As WindowsMediaPlayer
Stop
End Sub
Run it.
In VBE’s Immediate window (menu option “View” > “Immediate Window” or CTRL-G) use Intellisense3 to display the object’s (WMP) various methods and properties and to play with it
Locals Window and CLSID/GUID
Sometimes the app has no entry in VBE’s references, or the reference can’t be used to create the object. In this case we can use the app’s CLSID (aka GUID)4 to create the object and discover its properties.
A. Find the app’s CLSID:
Run REGEDIT5
Search HKEY_LOCAL_MACHINE/SOFTWARE/Classes for the app’s name. Using the Windows Media Player as an example, I found WMPlayer.OCX.
Double click the app’s name. Underneath is CLSID.
Click CLSID to show its properties.
Double click the CLSID’s (Default) property (Windows Media Player’s is {6BF52A52-394A-11d3-B153-00C04F79FAA6} as found under WMPlayer.OCX), select it, copy it, and cancel out of REGEDIT.
B. Discover the app’s properties:
Write a temporary routine like below (pasting the CLSID in the CreateObject function):
Sub Temp()
Dim WMP As WindowsMediaPlayer
Set WMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")
Stop
End Sub
Run it.
When it stops, display the Locals window using VBE’s menu option “View” > “Locals Window”
WMP will be listed with a plus sign. Click the plus sign to reveal its properties and settings.
Use VBE’s Immediate window to set and play with the properties
NOTE! Unfortunately, the Locals Window does not display an object’s methods. At this point I usually guess based on similar objects’ methods. Guessing works often.
References:
What are COM Objects: http://en.wikipedia.org/wiki/COM_object
How to use Object Browser: http://msdn.microsoft.com/en-us/library/office/aa164805(v=office.10).aspx
What is Intellisense: http://en.wikipedia.org/wiki/Intelligent_code_completion
What is a CLSID/GUID: http://en.wikipedia.org/wiki/Clsid
How to Run REGEDIT: http://www.wikihow.com/Open-Regedit