PDA

View Full Version : [SOLVED:] String Compare Stumper



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

snb
11-10-2014, 07:47 AM
I can't replicate your findings.


Sub M_snb()
With ActiveDocument
.Content.Delete
.Content = String(2, vbCr)
.ContentControls.Add(1, .Paragraphs(1).Range).Range = "A"
.ContentControls.Add(0, .Paragraphs(2).Range).Range = "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)
Select Case oCC.Type
Case 1, 3, 4, 5, 8
c00 = oCC.Range.Text
For j = 1 To 3
If c00 <> oCC.Range.Text Then Exit For
Next

CCChange = IIf(j = 4, "", "not ") & "Equal"
Case 0, 2
c00 = oCC.Range.WordOpenXML
If Len(c00) = Len(oCC.Range.WordOpenXML) Then MsgBox "Equal size", , Len(c00)

Select Case bNormal
Case True
CCChange = IIf(StrComp(c00, oCC.Range.WordOpenXML) = 0, "", "no ") & "Equal content"
Case Else
For j = 3500 To 3600
If StrComp(Mid(c00, j, 1), Mid(oCC.Range.WordOpenXML, j, 1)) <> 0 Then Exit For
Next
CCChange = IIf(j = 3601, "", "no ") & "Equal content"
End Select
End Select
End Function

gmaxey
11-10-2014, 09:25 AM
snb,

I can replicate them here using your code. However, today the problem character appears to be 3051 and not 3501. The issue is:

I set strCC_Text = to the WordOpenXML string and then begin to compare strText to the WordOpenXML string. They "should" or it "seems the should" remain the same until a change occurs in the CC range (add text, change font size, etc.). However, they don't.

I believe the issue is that each time you query for WordOpenXML is is regenerated and contains a new set of "rsid" numbers. It is these changing numbers that cause the apparent change. This is detailed bit more in the cross-post: Note: This board seemed to be OOC earlier this morning so I've cross posted at:
https://social.msdn.microsoft.com/Fo...?forum=worddev (https://social.msdn.microsoft.com/Forums/office/en-US/b8123050-a719-4c4b-bc1a-d67fa2fcaf2a/string-compare-stumber?forum=worddev)

gmaxey
11-12-2014, 09:24 PM
Issue resolved. It was the "rsid" attributes. Functioning code is available here:
https://dl.dropboxusercontent.com/u/64545773/Class%20CC%20Change%20Event.docm