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! :)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This is a guess which may not be helpful but at least it will give this thread a bump. Have a nice day. Dave
Code:
Dim MyData As DataObject
Set MyData = New MSFORMS.DataObject
 
Upvote 0
Tom I'm not at all familiar with the use of MSFORMS as a data object but I am interested in learning a bit more about them and the use of the data object. It's a slow day so I messed around with a bit of code. This examples seems to show dataobject code syntax which is different from the use of binding syntax altogether. The following code shows the content of the clipboard in a message box using a dataobect then places the content in a word document (C\test...which must exist) using late binding. Maybe useful? Anyways, here's another bump. Maybe someone will have the syntax to do what you want to do. Dave
edit: fix ercode
reedit: missed the top part
argh:
edit...one last attempt

Code:
Sub DataObj()
'shows&places clipboard in C/Test.doc

'syntax for dataobject use
Dim MyData As DataObject
Dim Astring As String
Set MyData = New DataObject
    MyData.GetFromClipboard
    Astring = MyData.GetText(1)
    MsgBox Astring
Set MyData = Nothing
Application.CutCopyMode = False

'eg of late binding use
Dim Wapp2 As Object, Test As String
Test = "C:\Test.doc" 'this location has to exist
Set Wapp2 = CreateObject("Word.Application")
On Error GoTo FixErr
Wapp2.documents.Open Filename:=Test, ReadOnly:=False
Wapp2.activedocument.Select
With Wapp2.activedocument
    .Range(0, .Characters.Count).Delete
    .content.insertafter Astring
End With
Wapp2.activedocument.Close savechanges:=True
Wapp2.Quit
Set Wapp2 = Nothing
Exit Sub

FixErr:
On Error GoTo 0
MsgBox "You need a file:  " & Test
Wapp2.Quit
Set Wapp2 = Nothing
End Sub
 
Upvote 0
NdNoviceHlp

I don't think you are using late binding there.:)

The code you posted would still need a reference to the Forms object library.

This line will throw a User-defined type not defined compile error.
Code:
Dim MyData As DataObject

I searched for over an hour to try and find a solution for Right Click and couldn't
find anything.
 
Upvote 0
Norie I've had quite the battle with posting and have had to edit/redit/etc and it's still not right. I had the error code in the wrong place to begin with and so I originally edited that which led to the rest of the changes. The code I posted works quite well you may have tried it during my edit? My notion of late binding is creating the application on the fly (ie. not to Dim originally) so I'm not sure if I understand? I have reference set to the MS Object Library 9.0 if this is what you mean? I will attempt again to fix the code I posted when my frustration subsides. As for the use of "Tom" in my previous post, Right_Click I'm not sure how I arrived at that? Hope that furthering this discussion will solve this thread. Dave
 
Upvote 0
What I think Right Click wants to do is not have to declare MyData as a DataObject.

Your code does use late binding, but that's for the Word object, not the DataObject.

Like I said unless there is a reference to the Forms object library you will
get a compile error here.
Code:
Dim MyData As DataObject
Now if you have added a userform then that automatically creates that reference,
otherwise it's not there.:)
 
Upvote 0
I'm feeling rather guilty that Norie spent that much time just for me! Thanks though! I checked the web as thoroughly as I know before I posted. I searched through the window's registry but could not find anything useful. I'll just have to settle for adding a reference via code. Thanks for all the effort...
 
Upvote 0
I think the light is slowly coming on for me. Did some testing and the object reference I mentioned isn't required. I'm not sure how to test the formObject reference as I find it is auto created when you add a command button to a sheet (which I was using to call the sub). Interesting re. auto create reference to form library if userform, I've just had blind luck previously or... I've always used a command button to call the sub whether userform involved or not. I guess I was suggesting that perhaps late binding isn't possible with a dataobject but of coarse I have no knowlege of this. I've often appreciated Tom's posts and it seemed like this post needed a bump and here we are. Yes it is Tom, as I have re-discovered, and I guess the outcome of this post will be dependent upon his intentions. Dave
 
Upvote 0
Tom I think I now understand... you wanted to avoid setting the reference to the forms library required to use a dataobject hence the latebinding need for the dataobject. Are you going to use the GUID to set the reference? I would be interested in the code syntax. With Norie's enlightenment, I came up with the following bit of code to have XL auto set a persistant forms reference so that the dataobject can be used without error. Maybe completely not useful for you but fixes the error my previous code made when no command button is present. I've learned quite abit and I hope you don't mind my postings to this thread. Have a nice day. Dave
edit: Further testing and this code doesn't work when the clipboard holds sheet contents. :oops: Worked when I had code on the clipboard. It does set the needed reference but then doesn't work. I don't get it?
Code:
Sub Combined()
Call SetFormRef
Call DataObj
End Sub

Sub SetFormRef()
'Add temporary Userform
Dim Uf, Vuf
Set Uf = ThisWorkbook.VBProject.VBComponents.Add(3)
'Include the UF in the Userforms collection
Set Vuf = VBA.UserForms.Add(Uf.Name)
'remove userform
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=Uf
End Sub
 
Upvote 0
This "fixes" my routine. It now copies the selection to the clipboard, displays it, then places in the Word document. The "SetFormRef" is required to run only once in order to run "DataObj" without error. Still probably not helpful but does indicate that the "SetFormRef" sub works to auto set the form reference needed to use the data object. I'm done. Dave
Code:
Sub combined()
Call SetFormRef
Call DataObj
End Sub

Sub SetFormRef()
'Add temporary Userform
Dim Vuf, Uf
Set Uf = ThisWorkbook.VBProject.VBComponents.Add(3)
'Include the UF in the Userforms collection
Set Vuf = VBA.UserForms.Add(Uf.Name)
'remove userform
ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=Uf
End Sub

Sub DataObj()
'shows&places selection in C/Test.doc

'syntax for dataobject use
Dim mydata As DataObject
Dim Astring  As String
Selection.Copy
Set mydata = New DataObject
    mydata.GetFromClipboard
    Astring = mydata.GetText(1)
    MsgBox Astring
Set mydata = Nothing
Application.CutCopyMode = False

'eg of late binding use
Dim Wapp2 As Object, Test As String
Test = "C:\Test.doc" 'this location has to exist
Set Wapp2 = CreateObject("Word.Application")
On Error GoTo FixErr
Wapp2.documents.Open Filename:=Test, ReadOnly:=False
Wapp2.activedocument.Select
With Wapp2.activedocument
    .Range(0, .Characters.Count).Delete
    .content.insertafter Astring
End With
Wapp2.activedocument.Close savechanges:=True
Wapp2.Quit
Set Wapp2 = Nothing
Exit Sub

FixErr:
On Error GoTo 0
MsgBox "You need a file:  " & Test
Wapp2.Quit
Set Wapp2 = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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