Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-17-2021, 03:43 AM
Downunder Dave Downunder Dave is offline Use VBA to compare Text Box contents between documents. Windows 7 64bit Use VBA to compare Text Box contents between documents. Office 2010
Novice
Use VBA to compare Text Box contents between documents.
 
Join Date: Jul 2021
Location: New Zealand
Posts: 3
Downunder Dave is on a distinguished road
Default Use VBA to compare Text Box contents between documents.

Hi,


I am using Word 2010 for this exercise.


I currently have a piece of VBA that compares two documents (using either the CompareDocuments or Compare methods) and this produces a quick summary of the number of differences, their type (Insert/Delete) and the number of characters involved.



Each of the CompareDocuments or Compare methods produce an interim Results document, just as you get when doing this process manually via the Word toolbar/menu. The Results document I get with VBA clearly shows the text of each difference and whether that difference is within the:
Main Document
Header/Footer
Text Boxes
Header/Footer Text Boxes
Footnotes
Endnotes


Once I create an object of the Results document, I can easily iterate through the differences that are in the Main Document, like this:
For Each dsRevision In oResDoc.Revisions
If dsRevision.Type = wdRevisionInsert Then
sngCharacters = sngCharacters + dsRevision.Range.Characters.Count
End If
Next


My problem is that the VBA Revisions collection only seems to include differences that are in the Main Document and nowhere else.



These differences in other locations are clearly being found, I just cannot inspect them with VBA.


I may be missing something fundamental here. Does anyone know how I can use VBA to iterate across the Revisions that appear in the Footnote/Header and Text Box sections of the Results document ?





Many thanks,


Downunder Dave
Reply With Quote
  #2  
Old 07-20-2021, 10:26 PM
Peterson Peterson is offline Use VBA to compare Text Box contents between documents. Windows 10 Use VBA to compare Text Box contents between documents. Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

I'm not exactly sure what you're trying to accomplish (it looks like you're trying to add a count to a string??), but if you have a combined document with redlines in which you need to locate all insertions in all of what in Word are referred to as "stories," e.g., main body, headers, footers, text boxes, etc., then the following code should do that:
Code:
Sub Revisions_FindAllInAllStories()
' Find insertions in all stories

' Code to loop all stories by Macropod:
' http://www.vbaexpress.com/forum/archive/index.php/t-27391.html

    Dim pRange As Range
    
    Dim dsRevision As Revision
    Dim oResDoc As Document
    Dim sngCharacters As Long

    Set oResDoc = ActiveDocument
    
    For Each pRange In ActiveDocument.StoryRanges
        Do
            For Each dsRevision In pRange.Revisions

                ' I'm just guessing what you're trying to do here.
                ' Replace this If/Then with your code:
                If dsRevision.Type = wdRevisionInsert Then
                    sngCharacters = sngCharacters + dsRevision.Range.Characters.Count
                    Debug.Print sngCharacters
                End If
            Next
            Set pRange = pRange.NextStoryRange
        Loop Until pRange Is Nothing
    Next
End Sub
Reply With Quote
  #3  
Old 07-21-2021, 02:16 AM
Downunder Dave Downunder Dave is offline Use VBA to compare Text Box contents between documents. Windows 7 64bit Use VBA to compare Text Box contents between documents. Office 2010
Novice
Use VBA to compare Text Box contents between documents.
 
Join Date: Jul 2021
Location: New Zealand
Posts: 3
Downunder Dave is on a distinguished road
Default

Hi Peterson,
Thank you for your reply. You have handed me the missing link. I did not know about the StoryRanges object. It all makes a bit more sense now.


By the way, in the actual project I am trying to do, I am comparing a template to hundreds of documents that have been created by users from that template. I am trying to determine how much text has been added to that that was already in the template. The code that I displayed was finding the number of characters in each revision (that was the subject of an insert action) with:



dsRevision.Range.Characters.Count


and adding that number to the value currently in: sngCharacters


It works like a charm, it is just that without knowing about the StoryRanges object, I could not account for text boxes that a user may have added and inserted text into.


Cheers,


Downunder Dave
Reply With Quote
Reply

Tags
compare documents, revisions, text boxes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use VBA to compare Text Box contents between documents. Macro to read cell contents and compare data jrfoley3 Excel Programming 7 07-09-2016 12:25 PM
Compare 2007 documents with 2010 documents wardw Word 1 06-09-2016 02:29 PM
compare, match and count cell contents between sheets bobsone1 Excel 11 08-07-2014 10:34 PM
Use VBA to compare Text Box contents between documents. Can you compare two word documents? (Automatically) admin4ever Word 2 05-17-2011 09:44 AM
Compare two word documents gives FORMATT changes for ListItems Jagadeesh Word 0 09-30-2009 07:49 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft