CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2001
    Posts
    2

    How can I create run-time object references for objects of Office Object Model?

    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"
    ...





  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How can I create run-time object references for objects of Office Object Model?

    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Sep 2001
    Posts
    2

    Re: How can I create run-time object references for objects of Office Object Model?

    This is great!
    And how can I evaluate the constants?
    (vbAlignLeft or wdAlignParagraphCenter)


  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How can I create run-time object references for objects of Office Object Model?

    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured