I want to get both the revised and original text from a document. I do it this way:
Set wrdDoc = wrdApp.Documents.Open(fileName)
For each sent in wrdDoc.Sentences
if sent.Revisions.Count >=0 then
after=sent.text
sent.Revisions.RejectAll
before=sent.text
SaveRev(before,after)
End if
next
Now that would be fine, except that malformed sentences like
This is one sentence.This is another.
Will get parsed in a weird way. First, there will be this one: "This is one sentence.", then this one with both "This is one sentence.This is another."
What happens when there are revisions there? The first iteration will revert revisions on the first sentence, then the second iteration will not 'see' that revised portion.
Bottom line is, the first iteration will get both versions of the first sentence, and the second iteration will get only the original version of the first sentence (while getting both versions from the second sentence).
Let me clarify:
Let's say I had the original
We started with this sentence.And this sentence.
And it was revised to
We ended with this sentence.And this other sentence.
First iteration will result in
Before: We started with this sentence.
After: We ended with this sentence.
But second iteration will have
Before: We ended with this sentence.And this sentence.
After: We ended with this sentence.And this other sentence.
Well, what I did was alter the logic, undoing the revision reversion:
Set wrdDoc = wrdApp.Documents.Open(fileName)
For each sent in wrdDoc.Sentences
if sent.Revisions.Count >=0 then
wrdDoc.Undo
after=sent.text
sent.Revisions.RejectAll
before=sent.text
SaveRev(before,after)
End if
next
I like this because I end up with an unaltered document (except for the last sentence).
The thing is, doing this puts the macro in an infinite loop at one specific sentence.
I have no idea of the mechanics of the for each, I have no clue what is causing it to hang. Obviously altering the collection is messing up the loop, but I don't understand why.
I could loop for i=0 to wrdDoc.Sentences.Count, but I think that will make me skip sentences for the same reasons I'm repeating one now, and I cannot risk it (even if I test OK, I have to be sure it will never happen).
So the question is (are):
Thank you very much!
PS: I can provide sample documents, let me know if it's needed (maybe what I'm doing wrong is already clear to someone, and I'd have to make the samples as I cannot share the documents I'm working on).
--EDIT--
Ok so this is where it's hanging, only on the 32nd file.
It doesn't hang on a sentence, it actually does a few at the start of the document, then goes back to the beginning.
I previously encountered the same error, but it looped in a single sentence, and didn't go back to the beginning. I think it's the same issue. I'll try to reproduce original and revised versions here.
Originalversion
MAIN TITLE
Measurement of some variable
1 REQUIRED TOOLS
1.1 Special tools
NOTe:
Some note about the procedure (unaltered by revision)
Equipment name (carrier returned line)
(english) assemply with Equipment PN
Kit
Equipment name (carrier returned line)
(english) assemply with (Another) Equipment PN
Kit
Document continues...
There are 2 equipment entries before it restarts the loop.
Revision consisted of inserting the document number, some First Letter of the Word caps, and changing the order between Equipment PN and "Kit".
Revised version
ducument number
MAIN TITLE
Measurement of Some Variable
1 REQUIRED TOOLS
1.1 Special Tools
NOTe:
Some note about the procedure (unaltered by revision)
Equipment name (carrier returned line)
(english) assemply with kit
Equipment PN
Equipment name (carrier returned line)
(english) assemply with kit
(Another) Equipment PN
Document continues...
Recorded original/revison pairs were:
Original..................................Revised
{Empty}...................................Document number
Measurement of some variable..............Measurement of Some Variable
Special tools............................Special Tools
(english) assemply with..................(english) assemply with kit
(english) assemply with..................(english) assemply with kit
Then it starts again, recording the same entries until I break. I don't see the sentences overlapping I talked about, but there was a line break insertion on the revision.
Thanks!
Enumerable objects should not be altered during the enumeration or bad things can happen (what depends on the type of collection).
My guess is that the revision/undo process, combined with the wonky sentence, is causing the Sentences enumerable to change.
You should prepare your own collection first, to see if that makes a difference. Simply try Set sents = New Collection: For Each sent in wrdDoc.Sentences: sents.Add sent: Next
then use sents for your main For Each loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With