Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on 

How to find the class id of an arbitrary object? Example:Set x = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

Please, do not post tangents about whether today's goal is good, bad, fast, slow, brilliant or stupid.  I already had a bunch of those tangents here.  Strangely enough, that long winded discussion never mentioned createobject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")  which I now believe might be a very useful construction.

My goal is to make vba code more portable by switching from early binding to late binding. I wanted to do this for ARBITRARY object types while keeping the object's methods and properties unchanged.  

For instance let us say I want to switch the following code to late binding.

Dim x as dataobject ' reference to ms forms 2.0 object library  which on my machine is c:\windows\syswow64\fm20.dll
set x = new dataobject

I accidentally found a webpage that showed the following alternative.
   dim x as object
  Set x = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

How can intentionally find similar classids? For instance, can I transform the following
    dim x as new SHDocVw.ShellWindows
VBAMicrosoft ExcelVisual Basic ClassicMicrosoft Access

Avatar of undefined
Last Comment
Robert Berke
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Robert Berke
Robert Berke
Flag of United States of America image

ASKER

I'll be a son of a gun -- that was really easy !!
 
To replace MSForms.dataobject I search for "dataobject" and found just what I needed:

HKEY_CLASSES_ROOT\CLSID\{1C3B4210-F441-11CE-B9EA-00AA006B1A69}\InprocServer32
        Class   Reg_SZ   Microsoft.Vbe.Interop.Forms.DataObjectClass

Next, I wanted to replace < set x =  New SHDocVw.ShellWindows > so I searched for "ShellWindows"

HKEY_CLASSES_ROOT\CLSID\{9BA05972-F6A8-11CF-A442-00A0C90A8F39
     (default)  REG_SZ   Shellwindows
     Appid      Reg_SZ   {9BA05972-F6A8-11CF-A442-00A0C90A8F39}

That actually worked in the following (except SHDocVw.internetExplorer does not need a createobject because the For Each loop creates them.)

Sub ShowIE()
' early binding Dim shellWins as ShdocVw.Shellwindows  creates typename IShellWindows
'                dim shdocvw.InternetExplorer creates typename IWebBrowser2
Dim shellWins As Object, explorer As Object '  SHDocVw.ShellWindows & SHDocVw.InternetExplorer
    Set shellWins = CreateObject("New:{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")
    For Each explorer In shellWins
        Debug.Print explorer.LocationURL
    Next
End Sub

Open in new window

But, I still have one more question.  What if I had been try to replace   MsForms.Page?  Search for "Page" gives me a dozen false positives before I finally got to
      Class   Reg_SZ   Microsoft.Vbe.Interop.Forms.PageClass

I recognized that as the correct key because I had previously seen the same structure for the DataObject.  That is cheating, I want a methodology that finds an arbitrary object (without relying on previous success searching for a similar object).

Is there some methodology that will quickly get to the correct classId having just the library.class (E.G. SHDocVw.ShellWindows )?
Avatar of Qlemo
Qlemo
Flag of Germany image

Sorry, but I don't think you will succeed in that. Some classes are translated into something more easily to use. "MsForms.Page" certainly is a VB alias for "Microsoft.Vbe.Interop.Forms.PageClass".
But in general, the classes are following the same scheme. "MsForms" translates to "Microsoft.Vbe.Interop.Forms" - always.
"Excel.Application" is a COM reference, and as such has an entry below HKCR\Excel.Application with CLSID pointing to HKCR\CLSID, and that again contains the real class name "Microsoft.Office.Interop.Excel.ApplicationClass". This shows the complexity of your request.
Avatar of Robert Berke
Robert Berke
Flag of United States of America image

ASKER

I am still making more progress than I expected.  I will leave this question open for while, just in case someone has a better idea.  Then I will award you points.  

Thanks for you help.

rberke
Avatar of Robert Berke
Robert Berke
Flag of United States of America image

ASKER

To replace  set  var = new xxxxx.yyyy
With  Set var = CreateObject("new:{3F4DACA4-160D-11D2-A8E9-00104B365C9F}")

You must first find the “magic numbers”. The following procedure seems to work pretty well.
Open Notepad to be used as workspace.

Open regedit and manuver to HKEY_CLASSES_ROOT\CLSID\
Ctrl f to find in yyyyy.  (remove the checkbox next to Keys and Values, and keep Data)

Edit > Copy Key Name > then paste into notepad.
Edit > Modify > Ctrl C > then paste into notepad. The first entry might look like this
    HKEY_CLASSES_ROOT\CLSID\{1C3B4210-F441-11CE-B9EA-00AA006B1A69}\InprocServer32
    Class REG_SZ Microsoft.Vbe.Interop.Forms.DataObjectClass

Use F3 to repeat the find and record the next occurances of yyyyy.
If the search “hangs” for over a minute, you are done.
You are also done if the key name does not start with HKEY_CLASSES_ROOT\CLSID

For instance yyyyy = “InternetExplorer” might look like this
HKEY_CLASSES_ROOT\CLSID\{0002DF01-0000-0000-C000-000000000046}\ProgID
InternetExplorer.Application.1
HKEY_CLASSES_ROOT\CLSID\{0002DF01-0000-0000-C000-000000000046}\VersionIndependentProgID
InternetExplorer.Application

Summarize all the class ids and ignore duplicates.  
Eliminate incorrect class ids using code like this.

Sub test711()  Sub testCLSID() s a better version.  See a later post in this thread
msgbox "there is a better version"
On Error Resume Next
Dim x1 As SHDocVw.InternetExplorer ' your original early binding class
Dim x2 As Object
Dim x3 As Object
Set x2 = CreateObject("new:{0002DF01-0000-0000-C000-000000000046}") ' possible replacement
Set x3 = CreateObject("new:{xxxxxxx.....xxxx}") ' another possible replacement

Debug.Print TypeName(x1) & vbCrLf & TypeName(x2) & vbCrLf & TypeName(x3)

End Sub
Avatar of Robert Berke
Robert Berke
Flag of United States of America image

ASKER

I still believe there might be a more general answer and (when I have time) I am going to open a new question to pursue a general resolution to my question.
For instance, I am very intrigued by Fanpage's code which shows the GuiId associated with a referenced library.    Perhaps there is some way of diving lower into the reference and pull out all class names and their associated clsid?

But, for right now, I agree the question should be closed.

Qlemo should get best answer

300 Points should go to Qlemo, and 200 to Fanpages.
Avatar of Qlemo
Qlemo
Flag of Germany image

OP stated a different point distribution, so stopping the closing process.
Avatar of Robert Berke
Robert Berke
Flag of United States of America image

ASKER

Slightly improved version for testing potential CLSID's in CreateObject
Sub testCLSID()
Dim desired As New SHDocVw.ShellWindows  ' your original early binding class
Dim Magic As Variant, fullMagic As String, msg As String
Dim x2 As Object, x3 As Object

On Error Resume Next
For Each Magic In Array("9BA05972-F6A8-11CF-A442-00A0C90A8F39", "d92bd3b9-99a0-4334-a497-11bcb093e9d2")
    fullMagic = "new:{" & Magic & "}"
    Set x2 = Nothing
    Set x2 = CreateObject(fullMagic)
    msg = msg & vbCrLf & fullMagic & "=" & TypeName(x2) & IIf(TypeName(x2) = TypeName(desired), "This magic number works,", " This number doesn't Work")
Next
MsgBox msg
End Sub
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo