• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

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
0
rberke
Asked:
rberke
  • 4
  • 3
2 Solutions
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You can search in the registry below HKCR\CLSID for the value containing your class name to replace.
0
 
[ fanpages ]IT Services ConsultantCommented:
I added the "Microsoft Internet Controls" as the fifth Reference in my VB(A)Project.

In the "Immediate" Window:

?ThisWorkbook.VBProject.References(5).Guid
{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}
0
 
rberkeConsultantAuthor Commented:
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

Sub ShowIE()
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 )?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
 
rberkeConsultantAuthor Commented:
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
0
 
rberkeConsultantAuthor Commented:
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()
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
0
 
rberkeConsultantAuthor Commented:
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.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
OP stated a different point distribution, so stopping the closing process.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now