Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-29-2014, 08:56 PM
Byron Polk Byron Polk is offline Compare text from Bookmark with text from Text box Windows 7 64bit Compare text from Bookmark with text from Text box Office 2010 32bit
Novice
Compare text from Bookmark with text from Text box
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default Compare text from Bookmark with text from Text box


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
Reply With Quote
  #2  
Old 07-29-2014, 11:40 PM
macropod's Avatar
macropod macropod is offline Compare text from Bookmark with text from Text box Windows 7 32bit Compare text from Bookmark with text from Text box Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 07-30-2014, 04:01 AM
Byron Polk Byron Polk is offline Compare text from Bookmark with text from Text box Windows 7 64bit Compare text from Bookmark with text from Text box Office 2010 32bit
Novice
Compare text from Bookmark with text from Text box
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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
I my actual project, I have an existing bookmark and I have a userform where the user is prompted to provide the information that is to be programatically used to update the text at the bookmark. In the actual project I am creating the text box and populating the text based on user selections from the same userform.

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
This code calls the "UpdateBookmark" function:
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
Later on I use the code below to acquire the text from the bookmark
Code:
strKeyInfo = ActiveDocument.Bookmarks("Key").Range.Text
If CheckForDesignatedKey(strKeyInfo) = False Then
This code calls the "CheckForDesignatedKey" function below to read the text from the text box and attempt to compare that value with the valaue from the bookmark

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
For what it is worth, the bookmark is located in the document header, but that does not seem to have any thing to do with the issue.

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
Reply With Quote
  #4  
Old 07-30-2014, 05:41 AM
macropod's Avatar
macropod macropod is offline Compare text from Bookmark with text from Text box Windows 7 32bit Compare text from Bookmark with text from Text box Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 07-30-2014, 06:18 AM
Byron Polk Byron Polk is offline Compare text from Bookmark with text from Text box Windows 7 64bit Compare text from Bookmark with text from Text box Office 2010 32bit
Novice
Compare text from Bookmark with text from Text box
 
Join Date: Jul 2014
Location: Whitehouse, TX
Posts: 14
Byron Polk is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare text from Bookmark with text from Text box VBA to input / clear text from a Bookmark micko1 Word VBA 10 11-05-2013 04:42 PM
Compare text from Bookmark with text from Text box 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
Compare text from Bookmark with text from Text box Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM
Compare text from Bookmark with text from Text box delete all bookmark text hklein Word VBA 4 08-10-2011 04:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft