![]() |
|
#1
|
|||
|
|||
|
I have been programming with VBA for years in MS Acces, but I have to admit that I have not used VBA in MS Word very much.
I have a situation where I am reading a text value from an enclosed bookmark and also reading a text value from a textbox. I am storing each of these values in a text type variable for each. I am then comparing the two values. This sounded simple enough, however, the two values never match, even when I have reduced these two values to just an "A" in the bookmark and in the textbox. I would like to know if anyone has had any experience with something like this and if so how you managed to get the comparison to work. Thanks in advance for any assistance. Byron |
|
#2
|
||||
|
||||
|
What is the code you're using to read the bookmark text and compare it against the textbox value? If the bookmarked text was added programmatically, please post the code for that too.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
macropod,
Thanks for your response. I going to attach a document that will demo what code I am using to read the values. In this document I have created the bookmark and text box manually and am only reading and attempting to compare the values through code. I was not able to upload the .docm docment. so here is the code from Macro1: Code:
Dim shp As Shape
Dim strKeyInfo As String
Dim strText As String
strKeyInfo = ActiveDocument.Bookmarks("Record").Range.Text
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
shp.Select
strText = shp.TextFrame.TextRange.Text
If strKeyInfo = strText Then
MsgBox "Values Match!"
Else
MsgBox "Values Do Not Match!"
End If
End If
Next shp
Below is the code that I am using to update the bookmark: Code:
Dim strKey As String
strChar1 = Me.cboKeyList
If Me.cboKeyModifier > "" Then
strChar2 = Me.cboKeyModifier
Else
strChar2 = ""
End If
strChar3 = Me.cboMajMin
strKey = strChar1 & strChar2 & " " & strChar3
UpdateBookmark "Key", strKey
Code:
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Code:
strKeyInfo = ActiveDocument.Bookmarks("Key").Range.Text
If CheckForDesignatedKey(strKeyInfo) = False Then
Code:
Public Function CheckForDesignatedKey(sKeyToFind As String) As Boolean
'returns true if key is found in any textbox
Dim shp As Shape, strText As String
CheckForDesignatedKey = False
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
shp.Select
strText = shp.TextFrame.TextRange.Text
If sKeyToFind = strText Then
CheckForDesignatedKey = True
End If
End If
Next shp
End Function
When I run my code and display the returned values from my variables in the immediate code window they both appear to have exactly the same values. I keep thinking that I am just missing something. I appreciate your attempting to help. Please take a look at the attached document and the code in Macro1. Hopefully you will see where the problem is. Byron |
|
#4
|
||||
|
||||
|
As coded, unless your 'Record' bookmarked range includes a terminating paragraph break, the match will fail because the textbox text necessarily includes one. On the assumption that terminating paragraph breaks in both the bookmarked range and the textbox should be excluded from the comparison, you could use:
Code:
Dim shp As Shape, strKeyInfo As String, strText As String, Rng As Range
With ActiveDocument
Set Rng = .Bookmarks("Record").Range
With Rng
Do While .Characters.Last = vbCr
.End = .End - 1
If .Start = .End Then Exit Do
Loop
strKeyInfo = .Text
End With
For Each shp In .Shapes
With shp
If .Type = msoTextBox Then
Set Rng = .TextFrame.TextRange
With Rng
Do While .Characters.Last = vbCr
.End = .End - 1
If .Start = .End Then Exit Do
Loop
strText = .Text
End With
If strKeyInfo = strText Then
MsgBox "Values Match!"
Else
MsgBox "Values Do Not Match!"
End If
End If
End With
Next
End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Thank you so much for you explanation of this situation. I really appreciate it. I had already come to the conclusion that there had to be something in one or the other of the two source that was causing the code to evaluate the two value as not being equal but I had no idea which one was causing the issue.
I will take a look at the code and implement it to make things work. Thanks again. Byron |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
VBA to input / clear text from a Bookmark
|
micko1 | Word VBA | 10 | 11-05-2013 04:42 PM |
Open header to insert text into bookmark
|
Amapola188 | Word VBA | 3 | 07-12-2012 05:16 PM |
| Repeating Bookmark Text | BECKTHOMO | Word | 1 | 03-27-2012 08:34 PM |
Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control
|
skarden | Word | 1 | 12-12-2011 10:39 PM |
delete all bookmark text
|
hklein | Word VBA | 4 | 08-10-2011 04:33 AM |