'Testing a Textbox value failure
So here goes (VBA Word): I'm picking a specific Text Box and testing to see if it's contents are equal to a specific string. All of this works - except I always get a false response. When I post the value in Debug.print is shows "Something Important" which should have been a true response. triggering a change in the text. Never does.
(VBA Word macro follows:)
Sub testit()
Dim WD as document
Dim StringOne as string
set WD = Application.ActiveDocument
StringOne = "Something Important"
'this line will always come back false - why?*
If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne Then
WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something More"
Else
' This will say "Something Important"
Debug.Print WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text
' And Just in case I typed something wrong let's enter it again into the textbox
WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something Important"
End If
End Sub
This is a simplification of a more complex program. I will be tests several Text Boxes, Which I will call by name and change accordingly in the WORD document for presentation reasons. IE: Change the .fontsize, .bold. etc. Accordingly. Some textboxes will have different settings according to the layout of the WORD doc. Regardless, if I can't test the value of the text in a textbox... it will never change.
Solution 1:[1]
Surprisingly, it is a character mapping problem.
If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne then
needs to be corrected as a length issue
If Left(Wd.Shapes.Range(ShapeName).TextFrame.TextRange.Text, Len(StringOne)) = StringOne Then
There seems to be a few blank or formatting characters that the graphic needs at the end of the string which you can trim off.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KingBob |
