Syntax for late binding to MSFORMS.DataObject

L

Legacy 98055

Guest
I cannot figure out the syntax for the life of me.

This should work...

Dim DatObj as Object
Set DatObj = CreateObject("MSFORMS.DataObject")

...but it does not.

Also, if it is not possible to late bind to this class, could you please explain why?

Thanks! :)
 
Very creative Dave. :)

Here is an example of creating a reference on the fly. The advantage of using the correct type (Dim da As New dataobject) is simply: If the reference is already set, you get early binding which is 2 to 3 times faster than late binding. If the reference is not set, it will be, and, if the user saves the changes, the code will be early bound from thence onward.

<table border="1" bgcolor="White"><caption ALIGN=left><font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New>  <font color="#0000A0">Sub</font> Main()
       <font color="#0000A0">If</font> HasReference <font color="#0000A0">Then</font>
           RunUsualCode
       <font color="#0000A0">Else</font>
          <font color="#008000"> 'plan b (whatever that is) :)</font>
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
  <font color="#0000A0">Function</font> HasReference() <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font>
       <font color="#0000A0">Dim</font> Ref <font color="#0000A0">As</font> Object, fso <font color="#0000A0">As</font> Object, SysFolder <font color="#0000A0">As</font> <font color="#0000A0">String</font>
       <font color="#0000A0">Const</font> SystemFolder <font color="#0000A0">As</font> <font color="#0000A0">Integer</font> = 1
      
       <font color="#0000A0">Set</font> fso = CreateObject("Scripting.FileSystemObject")
       SysFolder = fso.GetSpecialFolder(SystemFolder)
      
      <font color="#008000"> 'try and load from file first</font>
       <font color="#0000A0">Set</font> Ref = ThisWorkbook.VBProject.References.AddFromFile(SysFolder & "\FM20.DLL")
      
      <font color="#008000"> 'if loading from file failed, load using id</font>
       <font color="#0000A0">If</font> Ref <font color="#0000A0">Is</font> <font color="#0000A0">Nothing</font> <font color="#0000A0">Then</font>
           <font color="#0000A0">Set</font> Ref = ThisWorkbook.VBProject.References.AddFromGuid("{0D452EE1-E08F-101A-852E-02608C4D0BB4}", 2, 0)
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
      
       HasReference = <font color="#0000A0">Not</font> (Ref <font color="#0000A0">Is</font> Nothing)
  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>
  
  <font color="#0000A0">Sub</font> RunUsualCode()
       <font color="#0000A0">Dim</font> da <font color="#0000A0">As</font> <font color="#0000A0">New</font> dataobject
      
       da.Clear
       da.SetText "SomeText"
       da.PutInClipboard
      
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table>
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Thanks Tom that's great stuff. Your posted code creates an error if the library is already referenced ie. on second use of the function. The line "Set Ref = etc" errors. Anyways, I've learned quite abit and thanks for the post. Dave
 
Upvote 0
I cannot figure out the syntax for the life of me.

This should work...

Dim DatObj as Object
Set DatObj = CreateObject("MSFORMS.DataObject")

...but it does not.

I also have spent a while trying to determine how to late bind with MSForms 2.0 DataObjects - like Tom, I expected the above to work (which it doesn't).

Is anyone able to shed light on the reason why this doesn't work (I'm not after any workarounds, I just want to know if anyone is able to show how to use late-binding for this object, or if not, why not).
 
Upvote 0
Hi

The CreateOject function creates a reference to an ActiveX object. I've never read that the DataObject is an Active X component and so I'd not expect it to work.

Although I don't see at this moment any such case, if by any reason I'd want to use late binding with the DataObject, I'd use New, like:

Code:
Sub DObjLB()
Dim dObj As Object
 
Set dObj = New MSForms.DataObject
 
dObj.SetText "Hi!"
MsgBox dObj.GetText
End Sub

It goes without saying that the fact that I've never read that the DataObject is an Active X component means just that.
 
Upvote 0
Hi PGC

But that code still requires a reference to the MSForms2.0 lib file, which is what I would like to avoid with late binding.
 
Upvote 0
Hi Richard

Please confirm.

I don't think you can do that. Late binding, nowadays, means that the type of the object is dinamically defined during the execution of the program. But you still have to get the information necessary to define the type.

You get that from the Active X components, not from the late binding, as an Active X component implements the "IUnknown interface" that allows you to gain access to its inner interfaces.

In the case of the DataObject, since it's not an Active X component, and since it belongs to an Object Model independent from the application, you have to provide the information by including the MSForms reference. That's how the code knows what's a DataObject.

Pedro
 
Upvote 0
OK, perhaps I should have watched my words more carefully: I wanted to create a DataObject without creating an explicit reference to the MSForms2.0 library in the VBA project (I realise I could incorporate code that added such a reference, but that wasn't the point of my question).

PGC you have given me an explanation as to why I have been unable to use CreateObject to achieve this - and I thank you for this. Unless I hear otherwise, I am going to assume you are correct.

(y)
 
Upvote 0

Forum statistics

Threads
1,216,080
Messages
6,128,692
Members
449,464
Latest member
againofsoul

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top