View Single Post
 
Old 07-30-2014, 04:01 AM
Byron Polk Byron Polk is offline Windows 7 64bit Office 2010 32bit
Novice
 
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