none
VBA Automation RRS feed

Answers

  • I figured you were using automation from the object references, but your own reference to 'CompareTarget:=wdCompareTargetNew' led me to believe you were using early binding. So that's what I coded for. And, if you're using late binding, you weren't testing with exactly the same code as I posted... Of course, if you use the wrong values, you might expect an error. Instead of:
    wdCompareTargetNew
    you should be using:
    2
    and instead of:
    wdActiveEndPageNumber
    you should be using:
    3

    Also, try running some code from Word itself on the 'compare' document, independently of the above code, such as:

    Sub Test()
    Dim i As Long
    With ActiveDocument
      For i = 1 To .Revisions.Count
        With .Revisions(i)
          .Range.Select
          MsgBox i & .Type & vbCr & .Range.Information(wdActiveEndPageNumber)
        End With
      Next
    End With
    End Sub

    This should help reveal what's causing the error (if it's not your automation code).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 5:58 AM
  • The late binding equivalent of:
    wdCompareTargetNew is 2.
    wdActiveEndPageNumber is 3, not 1.

    So that's another example of your code is not being the same as what I posted...

    I am surprised your late-binding code runs at all with CompareTarget:=wdCompareTargetNew - perhaps that's behind the errors: you should be using '2'.

    Also, using '1' instead of wdActiveEndPageNumber is asking Word to return wdActiveEndAdjustedPageNumber. If you do that, you'll need to change:
    If .Revisions(i).Range.Information(wdActiveEndPageNumber) > j Then
    to:
    If .Revisions(i).Range.Information(1) <> j Then
    as well. That's because the 'adjusted' page # reported by wdActiveEndAdjustedPageNumber returns the values returned by the document's page numbering (which might get re-set at some point), whereas the 'raw' page # reported by wdActiveEndPageNumber ignores the document's page numbering.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 7:38 AM

All replies

  • Aside from the inconsistency in your variable usage (e.g. strtemp, strtemp1 vs If str = str1) this is simply a matter of comparing the two strings, on a word-by word basis. For example:

    Dim objWord As Object, wdDoc As Object
    Dim StrDoc1 As String, StrDoc2 As String
    Dim i As Long, j As Long, StrOut1 As String, StrOut2 As String
    Set objWord = CreateObject("Word.Application")
    Set wdDoc = objWord.Documents.Open(TextBox1.Text, False, True, False)
    objWord.Selection.GoTo What:=1, Which:=1, Name:="2"
    StrDoc1 = Replace(wdDoc.Bookmarks("\page").Range.Text, vbCr, " ")
    wdDoc.Close False
    Set wdDoc = objWord.Documents.Open(TextBox3.Text, False, True, False)
    objWord.Selection.GoTo What:=1, Which:=1, Name:="2"
    StrDoc2 = Replace(wdDoc.Bookmarks("\page").Range.Text, vbCr, " ")
    wdDoc.Close False
    objWord.Quit
    Set wdDoc = Nothing: Set objWord = Nothing
    If StrDoc1 = StrDoc2 Then
      MsgBox "Matched", vbInformation
    Else
      If UBound(Split(StrDoc1, " ")) > UBound(Split(StrDoc2, " ")) Then
        j = UBound(Split(StrDoc2, " "))
      Else
        j = UBound(Split(StrDoc1, " "))
      End If
      For i = 0 To j
        If Split(StrDoc1, " ")(i) <> Split(StrDoc2, " ")(i) Then
          StrOut1 = StrOut1 & Split(StrDoc1, " ")(i) & " "
          StrOut2 = StrOut2 & Split(StrDoc2, " ")(i) & " "
        ElseIf UBound(Split(StrOut1, " ")) > 0 Then
          StrOut1 = StrOut1 & Split(StrDoc1, " ")(i) & " "
          StrOut2 = StrOut2 & Split(StrDoc2, " ")(i) & " "
        End If
        'Limit the output to 10 words
        If UBound(Split(StrOut1, " ")) > 8 Then Exit For
      Next
      If StrOut1 = "" Then StrOut2 = Replace(StrDoc2, StrDoc1, "")
      If StrOut2 = "" Then StrOut1 = Replace(StrDoc1, StrDoc2, "")
      MsgBox "Not Matched. The first mismatch starts with:" & vbCr & vbCr & _
      TextBox1.Text & ":" & vbCr & StrOut1 & vbCr & _
      TextBox3.Text & ":" & vbCr & StrOut2, vbInformation
    End If


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, May 6, 2014 12:44 AM
  • The problem with finding multiple matches/mis-matches is that it requires a lot of extra work to work out whether and where the contents are in synch again once a mismatch has occurred. For example, consider the following two strings:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
    The quick brwon fox jumps oevr the lazy dog. The quick brown fox jumps over the lazy dog.

    Does that count as one mismatch, or two? What about:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
    The quick brown foxes jumped over the lazy dogs. The quick brown fox jumps over the lazy dog.

    or:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
    The quick or slow brown fox jumps over the lazy dog. The brown fox jumps over a lazy dog.

    With the second pair, we the same number of words, but of different lengths. With the last one, however, there a differing numbers of words in the both sentences. So we start getting into issues of how far ahead do we search for the next match and how many matched words would it take before we decide we can re-synchronize. When you start getting into that level of complexity, you might do better to save both pages as documents in their own right and calling Word's document compare function to look for the differences.

    In the code I posted, I simply look for the first difference, then add up to 9 more words (matched or not) for context.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, May 6, 2014 11:59 PM
  • Yes, I am referring to the Document.Compare method. As per the VBA help file, the following example compares the active document with the document named "FirstRev.doc" in the Draft folder and places the comparison differences in a new document.
    Sub CompareDocument()
        ActiveDocument.Compare Name:="C:\Draft\FirstRev.doc", _
            CompareTarget:=wdCompareTargetNew
    End Sub

    So, if you send both of your pages to new documents, save one and make the other one active, you can use this method to output all of the differences in a new document which you can display on screen for the user to review.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, May 8, 2014 2:25 AM
  • As I've already said, you would extract just the two pages (as the code I posted before already does) and, instead of retaining them just as test strings, create a new document for each, save one, make the other one active then compare that against the saved one.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, May 8, 2014 10:03 PM
  • To get a list of the page numbers on which all variations occur, you would use the Document.Compare method, then run code like the following on the output document.

    Sub Demo()
    Dim i As Long, j As Long, StrRev As String
    With ActiveDocument
      For i = 1 To .Revisions.Count
        If .Revisions(i).Range.Information(wdActiveEndPageNumber) > j Then
          j = .Revisions(i).Range.Information(wdActiveEndPageNumber)
          StrRev = StrRev & " " & j
        End If
      Next
      MsgBox "There are " & i - 1 & " variations, occurring on pages:" & StrRev
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, May 9, 2014 10:08 PM
  • I've already provided the code for that here: http://social.msdn.microsoft.com/Forums/office/en-US/2ff9ce1a-602c-4c06-a6cb-af27e5a984ea/vba-automation?forum=worddev

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, May 10, 2014 12:16 PM
  • Your 'With wdDoc' line should refer to 'With objWord.ActiveDocument', since it's the new document you want to get the stats from. Try:

    Sub Test()
    Dim objWord As Object, wdDoc As Object
    Dim i As Long, j As Long, StrRev As String
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False ' Comment-out this line if you want to see the combined document
    Set wdDoc = objWord.Documents.Open("C:\Users\John\Desktop\New folder (7)\a.docx", _
        False, True, False)
    wdDoc.Compare Name:="C:\Users\John\Desktop\New folder (7)\b.docx", _
        CompareTarget:=wdCompareTargetNew
    With objWord.ActiveDocument
      For i = 1 To .Revisions.Count
        If .Revisions(i).Range.Information(wdActiveEndPageNumber) > j Then
          j = .Revisions(i).Range.Information(wdActiveEndPageNumber)
          StrRev = StrRev & " " & j
        End If
      Next
      .Close False ' Comment-out this line if you want to see the combined document
    End With
    wdDoc.Close False
    objWord.Quit ' Comment-out this line if you want to see the combined document
    Set wdDoc = Nothing: Set objWord = Nothing
    MsgBox "There are " & i - 1 & " variations, occurring on pages:" & StrRev
    End Sub

    Note: In Word 2010, at least, but not in Word 2003, when opening the documents for comparison through VBA, the number of variations reported may differ, depending on which document you reference first. It would appear the 'original' should be the one referenced by wdDoc.Compare.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, May 10, 2014 11:03 PM
  • I have tested the code with both Word 2003 & Word 2010 and, in both cases, I get actual page numbers being reported, not 0s. If you're getting 0s, that suggests either you have a faulty Office installation or there's something else about your documents or what you're doing that impacts the result. The code is already written so that each page is reported once, regardless of how many variations occur on that page.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 11, 2014 10:50 PM
  • Are you running the code exactly as I posted it? And is your system fully updated? If so, the errors suggest there may be a fault in your Office installation, which you may be able to fix via Word Options|Resources|Diagnose.

    Cheers
    Paul Edstein
    [MS MVP - Word]


    Monday, May 12, 2014 4:40 AM
  • Paul,

    Yes.I am running the code exactly as you posted and my system is updated also.I have diagnosed everything and it shows my system  does not have any problem.

    But still when I am running the code I am getting the same error.

    Thanks

    John P


    Monday, May 12, 2014 5:36 AM
  • I figured you were using automation from the object references, but your own reference to 'CompareTarget:=wdCompareTargetNew' led me to believe you were using early binding. So that's what I coded for. And, if you're using late binding, you weren't testing with exactly the same code as I posted... Of course, if you use the wrong values, you might expect an error. Instead of:
    wdCompareTargetNew
    you should be using:
    2
    and instead of:
    wdActiveEndPageNumber
    you should be using:
    3

    Also, try running some code from Word itself on the 'compare' document, independently of the above code, such as:

    Sub Test()
    Dim i As Long
    With ActiveDocument
      For i = 1 To .Revisions.Count
        With .Revisions(i)
          .Range.Select
          MsgBox i & .Type & vbCr & .Range.Information(wdActiveEndPageNumber)
        End With
      Next
    End With
    End Sub

    This should help reveal what's causing the error (if it's not your automation code).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 5:58 AM
  • The late binding equivalent of:
    wdCompareTargetNew is 2.
    wdActiveEndPageNumber is 3, not 1.

    So that's another example of your code is not being the same as what I posted...

    I am surprised your late-binding code runs at all with CompareTarget:=wdCompareTargetNew - perhaps that's behind the errors: you should be using '2'.

    Also, using '1' instead of wdActiveEndPageNumber is asking Word to return wdActiveEndAdjustedPageNumber. If you do that, you'll need to change:
    If .Revisions(i).Range.Information(wdActiveEndPageNumber) > j Then
    to:
    If .Revisions(i).Range.Information(1) <> j Then
    as well. That's because the 'adjusted' page # reported by wdActiveEndAdjustedPageNumber returns the values returned by the document's page numbering (which might get re-set at some point), whereas the 'raw' page # reported by wdActiveEndPageNumber ignores the document's page numbering.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, May 12, 2014 7:38 AM