gmaxey
11-08-2014, 03:22 PM
I'm stumped with a string compare problem. The following code should illustrate. I am trying to detect a change in a RichText type content control. I want to detect any changes e.g., format etc. and not just a text change. I've attempted to set a string variable equal to the WordOpenXML string value of the CC range. I then attempted to run a loop to compare the string variable to the WordOpenXML string. When they are no longer equal represents a change.
For some reason (and the number 3500 may just be on my PC), the strings match up to the 3500th character and then they no longer match. Can anyone replicate/explain this weird behavior?
Note: This board seemed to be OOC earlier this morning so I've cross posted at:
https://social.msdn.microsoft.com/Forums/office/en-US/b8123050-a719-4c4b-bc1a-d67fa2fcaf2a/string-compare-stumber?forum=worddev
Option Explicit
Option Compare Text
Sub Test()
With ActiveDocument
.Range.Delete
.Range.InsertBefore vbCr + vbCr
.ContentControls.Add wdContentControlText, .Paragraphs(1).Range
.ContentControls.Add wdContentControlRichText, .Paragraphs(2).Range
.ContentControls(1).Range.Text = "A"
.ContentControls(2).Range.Text = "A"
MsgBox CCChange(.ContentControls(1))
MsgBox CCChange(.ContentControls(2))
MsgBox CCChange(.ContentControls(2), False)
End With
End Sub
Function CCChange(oCC As ContentControl, Optional bNormal = True) As Boolean
Dim lngIndex As Long 'For demo/testing only.
Dim strCC_Text As String
CCChange = False
Select Case oCC.Type
Case 1, 3, 4, 5, 8
strCC_Text = oCC.Range.Text
Do While strCC_Text = oCC.Range.Text
DoEvents
On Error GoTo Err_Object
lngIndex = lngIndex + 1 'For demo/testing only to trigger a simulated change.
If strCC_Text <> oCC.Range.Text Or lngIndex = 3 Then
CCChange = True
Exit Function
End If
Loop
Case 0, 2
strCC_Text = vbNullString
strCC_Text = CStr(oCC.Range.WordOpenXML)
If Len(strCC_Text) = Len(oCC.Range.WordOpenXML) Then
MsgBox Len(strCC_Text) & " " & Len(oCC.Range.WordOpenXML) & " They appear to be the same at least by characters count."
End If
Select Case True
Case bNormal
Do While strCC_Text = oCC.Range.WordOpenXML
'So why does't this code execute?
DoEvents
If strCC_Text <> oCC.Range.WordOpenXML Then
DoEvents
CCChange = True
Exit Function
End If
Loop
Case Else
'It seems there is a difference with the 3501 character :-(
Debug.Print Mid(strCC_Text, 3051, 1) & " " & Mid(oCC.Range.WordOpenXML, 3051, 1)
'Why would that be since the first string is set to = the second string?
Do While Mid(strCC_Text, 1, 3050) = Mid(oCC.Range.WordOpenXML, 1, 3050)
DoEvents
If strCC_Text <> oCC.Range.WordOpenXML Then
DoEvents
CCChange = True
Exit Function
End If
Loop
End Select
End Select
lbl_Exit:
DoEvents
Exit Function
Err_Object:
Resume lbl_Exit
End Function
For some reason (and the number 3500 may just be on my PC), the strings match up to the 3500th character and then they no longer match. Can anyone replicate/explain this weird behavior?
Note: This board seemed to be OOC earlier this morning so I've cross posted at:
https://social.msdn.microsoft.com/Forums/office/en-US/b8123050-a719-4c4b-bc1a-d67fa2fcaf2a/string-compare-stumber?forum=worddev
Option Explicit
Option Compare Text
Sub Test()
With ActiveDocument
.Range.Delete
.Range.InsertBefore vbCr + vbCr
.ContentControls.Add wdContentControlText, .Paragraphs(1).Range
.ContentControls.Add wdContentControlRichText, .Paragraphs(2).Range
.ContentControls(1).Range.Text = "A"
.ContentControls(2).Range.Text = "A"
MsgBox CCChange(.ContentControls(1))
MsgBox CCChange(.ContentControls(2))
MsgBox CCChange(.ContentControls(2), False)
End With
End Sub
Function CCChange(oCC As ContentControl, Optional bNormal = True) As Boolean
Dim lngIndex As Long 'For demo/testing only.
Dim strCC_Text As String
CCChange = False
Select Case oCC.Type
Case 1, 3, 4, 5, 8
strCC_Text = oCC.Range.Text
Do While strCC_Text = oCC.Range.Text
DoEvents
On Error GoTo Err_Object
lngIndex = lngIndex + 1 'For demo/testing only to trigger a simulated change.
If strCC_Text <> oCC.Range.Text Or lngIndex = 3 Then
CCChange = True
Exit Function
End If
Loop
Case 0, 2
strCC_Text = vbNullString
strCC_Text = CStr(oCC.Range.WordOpenXML)
If Len(strCC_Text) = Len(oCC.Range.WordOpenXML) Then
MsgBox Len(strCC_Text) & " " & Len(oCC.Range.WordOpenXML) & " They appear to be the same at least by characters count."
End If
Select Case True
Case bNormal
Do While strCC_Text = oCC.Range.WordOpenXML
'So why does't this code execute?
DoEvents
If strCC_Text <> oCC.Range.WordOpenXML Then
DoEvents
CCChange = True
Exit Function
End If
Loop
Case Else
'It seems there is a difference with the 3501 character :-(
Debug.Print Mid(strCC_Text, 3051, 1) & " " & Mid(oCC.Range.WordOpenXML, 3051, 1)
'Why would that be since the first string is set to = the second string?
Do While Mid(strCC_Text, 1, 3050) = Mid(oCC.Range.WordOpenXML, 1, 3050)
DoEvents
If strCC_Text <> oCC.Range.WordOpenXML Then
DoEvents
CCChange = True
Exit Function
End If
Loop
End Select
End Select
lbl_Exit:
DoEvents
Exit Function
Err_Object:
Resume lbl_Exit
End Function