Consulting

Results 1 to 4 of 4

Thread: String Compare Stumper

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location

    String Compare Stumper

    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/Fo...?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
    Last edited by gmaxey; 11-09-2014 at 09:28 AM. Reason: Announce cross posting
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Issue resolved. It was the "rsid" attributes. Functioning code is available here:
    https://dl.dropboxusercontent.com/u/...e%20Event.docm
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •