none
Word 2010 VBA code using EVAL() RRS feed

  • Question

  • I'm trying to dynamically access controls (text boxes) that I've added to a Word 2010 document.  I added the Access Object Library to expose the EVAL function and it seems to be present.  Assume my texbox control is named "test1", here are the lines of code I'm trying to run:

      x = MsgBox("test1=" & ThisDocument.test1.Text, vbOKOnly)
      x = MsgBox("eval=" & Eval("ThisDocument.test1.Text"), vbOKOnly)

    The first line works, the second line fails with "run-time error '2766', The object doesn't contain the automation object 'thisdocument.'

    I suspect I have to fully qualify the control test1 name but no matter how high up the object tree I go the 1st portion fails.  Can someone just tell me what the fully qualified name would be?


    Rick E.

    Friday, August 9, 2013 3:33 AM

Answers

  • Eval can evaluate objects within the Access application. It doesn't recognize objects within the Word application, so you can't use it for your purpose.

    Regards, Hans Vogelaar

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:42 PM
    Friday, August 9, 2013 9:22 AM
  • Hi Rick

    Mmm. Eval can only work on string equations. My assumption is that the ActiveX object you're referencing should return such an equestion from its Text property? So if I did Debug.Print ThisDocument.test1.Text the result would be an equation, something like:

      3*4

    Is that a correct assumption? If it is, please try as I suggested and remove the QUOTES from around ThisDocument.test1.Text. What you're doing with the quotes is turning the object reference into a literal string. You're telling Eval you want it to run an evaluation on the literal string "ThisDocument.test1.Text" when what (I think) you really want is to evaluate what ThisDocument.test1.Text returns as the content of the control.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:10 PM
    Moderator
  • Ah, sorry, now I'm following what you're saying.

    Access EVAL can see the Word objects, but you can't pass the object as a string to the function. That won't work anywhere. If you want dynamic, look at the code sample I used in an earlier reply that addresses an InlineShape over an index value. Then you could do something like

    Sub XYZ(controlIndexInDocument)
      Dim ctl as Object
      Set ctl = ActiveDocument.InlineShapes(controlIndexInDocument).OLEFormat.Object
      Debug.Print Eval(ctl.Text)
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:14 PM
    Moderator
  • How did you add the text boxes? Did you click "Rich Text Context Control" or "Plain Text Content Control"? Or did you click Legacy Tools > Text Box (under ActiveX Controls)?

    Regards, Hans Vogelaar

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:51 PM
  • I added using the texbox under ActiveX controls.

    Rick E.

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 5:48 PM
  • FYI, I think I got it and you coached my in the right direction.  It was the string of

    ActiveDocument.InLineSHapes.OLEFormat.(property) but you have to test ProgID to ensure it's a textbox first.

    I'm over the hump and sailing now.  Thanks for the nudging.  I was really stuck.


    Rick E.

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 10:56 PM

All replies

  • Eval can evaluate objects within the Access application. It doesn't recognize objects within the Word application, so you can't use it for your purpose.

    Regards, Hans Vogelaar

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:42 PM
    Friday, August 9, 2013 9:22 AM
  • Hi Rick

    Try first assigning the text to a string variable, then pass the variable to Eval instead of the reference to the control. Eval does work while running in Word when I give it a string to work with.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 9, 2013 11:40 AM
    Moderator
  • Thanks Cindy, I tried the code below but it fails the exact same way?  Is this how you meant the code to be?  It still can't 'see' the 'thisdocument' object.

    x = MsgBox("test1=" & ThisDocument.test1.Text, vbOKOnly)

    streval="ThisDocument.test1.Text" x = MsgBox("eval=" & Eval(streval), vbOKOnly)



    Rick E.

    Friday, August 9, 2013 1:11 PM
  • Hi Hans, I should have asked a sharp fellow like you 8 hours ago!  I was trying to develop 'the other way' and loop through controls, checking their .name and .text values but i can't get that to work either.  I've tried sample code from across the Internet and i don't know if it's a Word 2010 issue but none of it works.  Most of the code I see centers around a "tag" property but my TextBox control doesn't have a tag property.  None of the sample code references the "name" property.

    Do you have sample code to loop through all controls (or is a textbox a contentcontrol?) and list their .name and .text?

    Thanks for your help.


    Rick E.

    Friday, August 9, 2013 1:14 PM
  • Hi Rick

    Works fine for me, here, when I test. It shouldn't make a difference, but my test code isn't in a ThisDocument class, just in a normal module, so I used this:

        Dim s As String
       
        s = ActiveDocument.InlineShapes(1).OLEFormat.Object.Text ' "1+2"
        MsgBox ActiveDocument.InlineShapes(1).OLEFormat.Object.Text
        MsgBox Eval(s), vbOKOnly

    I also tested assigning a static string (commented out) to the string variable. Everything worked as expected...

    And I am running Word 2010, same version of Access.

    Ah, I think I see the problem. You're putting the reference to the control in QUOTES, turning it into a string. Eval can't work with such a string - it needs an equation. Take away those quotes:streval=ThisDocument.test1.Text


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 9, 2013 2:32 PM
    Moderator
  • Hi Cindy, I think we're trying to accomplish two different things.  I believe YOU'RE trying to EVAL the contents of a static object property or the .Text value and have it perform the equation.  For ME I do NOT have a static object name but rather need to build the name of the object dynamically.

    I'm seeing a lot of evidence that Hans is right that the Access EVAL function can't see the Word objects to process them.


    Rick E.

    Friday, August 9, 2013 2:48 PM
  • Hi Rick

    Mmm. Eval can only work on string equations. My assumption is that the ActiveX object you're referencing should return such an equestion from its Text property? So if I did Debug.Print ThisDocument.test1.Text the result would be an equation, something like:

      3*4

    Is that a correct assumption? If it is, please try as I suggested and remove the QUOTES from around ThisDocument.test1.Text. What you're doing with the quotes is turning the object reference into a literal string. You're telling Eval you want it to run an evaluation on the literal string "ThisDocument.test1.Text" when what (I think) you really want is to evaluate what ThisDocument.test1.Text returns as the content of the control.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:10 PM
    Moderator
  • Ah, sorry, now I'm following what you're saying.

    Access EVAL can see the Word objects, but you can't pass the object as a string to the function. That won't work anywhere. If you want dynamic, look at the code sample I used in an earlier reply that addresses an InlineShape over an index value. Then you could do something like

    Sub XYZ(controlIndexInDocument)
      Dim ctl as Object
      Set ctl = ActiveDocument.InlineShapes(controlIndexInDocument).OLEFormat.Object
      Debug.Print Eval(ctl.Text)
    End Sub


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:14 PM
    Moderator
  • Thx, we're on the same track now.

    Yes, in my reply to Hans I was generally explaining I was attempting to do the concept you're showing above but can't hit the hit object to access the textbox value?  All the properties I hit don't have a .name or .text property and most sample code discusses the .tag property which my textbox doesn't have.  i've tried so many paths I'm not sure which one to post but something like:

    dim ctl as contentcontrol

    set ctl = thisdocument.contentcontrols(1)

    x=msgbox(ctl.name)

    -or -

    set ctl = thisdocument.contentcontrols.item(1)

    x=msgbox(ctl.name)

    It's clear I'm a novice at the Work Object heirarchy.  i need to retrieve the .name and .text values for textboxes I've added.

    


    Rick E.

    Friday, August 9, 2013 3:36 PM
  • How did you add the text boxes? Did you click "Rich Text Context Control" or "Plain Text Content Control"? Or did you click Legacy Tools > Text Box (under ActiveX Controls)?

    Regards, Hans Vogelaar

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 3:51 PM
  • Hi Rick

    Aha! As Hans asks: how did you insert these textboxes into the Word document? It makes a big difference, exactly what you're using...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 9, 2013 4:31 PM
    Moderator
  • I added using the texbox under ActiveX controls.

    Rick E.

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 5:48 PM
  • FYI, I think I got it and you coached my in the right direction.  It was the string of

    ActiveDocument.InLineSHapes.OLEFormat.(property) but you have to test ProgID to ensure it's a textbox first.

    I'm over the hump and sailing now.  Thanks for the nudging.  I was really stuck.


    Rick E.

    • Marked as answer by Rick D E Sunday, August 11, 2013 2:43 PM
    Friday, August 9, 2013 10:56 PM
  • Hi Rick

    Glad you're up and running, now :-)

    If you wouldn't mind taking a minute to mark the replie(s) that you consider answers as such, we from the admin corner of things would be grateful.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, August 10, 2013 3:13 PM
    Moderator