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:

    1. Use VBE’s menu option “Tools” > “References”

    2. Find the app we want and select/check it to add the reference

    3. Write a small routine like this:

      1. Sub Temp()

      2. Dim WMP As WindowsMediaPlayer

      3. Stop

      4. End Sub

    4. Run it.

    5. 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:

    1. Run REGEDIT5

    2. Search HKEY_LOCAL_MACHINE/SOFTWARE/Classes for the app’s name. Using the Windows Media Player as an example, I found WMPlayer.OCX.

    3. Double click the app’s name. Underneath is CLSID.

    4. Click CLSID to show its properties.

    5. 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:

    1. Write a temporary routine like below (pasting the CLSID in the CreateObject function):

      1. Sub Temp()

      2. Dim WMP As WindowsMediaPlayer

        1. Set WMP = CreateObject("new:{6BF52A52-394A-11d3-B153-00C04F79FAA6}")

        2. Stop

      3. End Sub

    1. Run it.

    2. When it stops, display the Locals window using VBE’s menu option “View” > “Locals Window”

    3. WMP will be listed with a plus sign. Click the plus sign to reveal its properties and settings.

    4. 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: