Click to See Complete Forum and Search --> : How can I create run-time object references for objects of Office Object Model?
swifty
September 25th, 2001, 09:47 AM
My problem is, that I don't know, how to create a reference at run-time to a word application object.
For example:
Dim myWordApp As Object
Set myWordApp = CreateObject("Word.Application")
Than I have a reference to a word application.
But, how can I check the value of myWordApp.Activedocument.Paragraphs(1).Alignment dynamically, if at compile-time I don't know yet, whose value I want to check, because it is generated from a string?
"Activedocument.Paragraphs(1).Alignment"
or
"ActiveDocument.Paragraphs(1).LineSpacing"
or
"ActiveDocument.CodeName"
...
Cakkie
September 25th, 2001, 11:07 AM
You can do this using the scriptcontrol. You will need to add a scriptcontrol to the form.
Dim obj as Object
set obj = CreateObject("Word.Application")
obj.Visible = true
ScriptControl1.Language = "VBScript"
ScriptControl1.AddObject "WordObject", obj
MsgBox ScriptControl1.Eval("Wordobject.version")
ScriptControl1.AddCode "public Sub NewDocument()" & vbCrLf & _
" Wordobject.Documents.Add" & vbCrLf & _
"End Sub"
ScriptControl1.Run "NewDocument"
What we do is pass the word object to the scriptcontrol. Then we can get if from scripts, which are mearly strings. We can use eval to get a single value (like shown with version), and we can use addcode to add subs/functions to the control. Again, strings are passed, so no need to know in advance what is going to get passed. Thhose subs/functions can then be run using the Run method of the control.
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
swifty
October 2nd, 2001, 03:47 AM
This is great!
And how can I evaluate the constants?
(vbAlignLeft or wdAlignParagraphCenter)
Cakkie
October 2nd, 2001, 04:02 AM
That's another thing, you can't do this directly, because you don't have a refference to it in VBScript. What you can do, is look up the values in the objectbrower, and pass them as numeric values.
You can look up a numeric value of a constant, by createing a new project, add refference to word, and type "?constant" in the immediate window, without the quotes, where constant is the name of the constant.
eg:
?wdAlignParagraphCenter returns 1
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.