Word is hanging when I hide deleted revisions programatically

C

Carlos Chalhoub

Hi listmates,

I created a macro that counts the number of revised words in a document. The
problem is that I don't want the macro to count the deleted text, just the
new text. What is happening is that when I hide the deleted text
(Options.DeletedTextMark = wdDeletedTextMarkHidden), Word is bombing and I
have to shut it down through task manager. When I omit the
"Options.DeletedTextMark = wdDeletedTextMarkHidden" line, the macro works,
but the deleted text is counted, which is not what I want.

I'm at my wits end. Can somebody show me what I'm doing wrong?

Thanks
Carlos

----------- Start of macro -----------------
Sub UnitFileCompare()

Dim oRev As Revision
Dim sallrev
Dim sAllRevWdCount
Dim numWords

'set revision mark options here
Options.DeletedTextMark = wdDeletedTextMarkHidden
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdNoHighlight

If ActiveDocument.Revisions.Count <> 0 Then
'make a revisions collection here
For Each oRev In ActiveDocument.Revisions
sallrev = sallrev & oRev.Range.Text & vbCrLf
Next oRev
Else
MsgBox "There are no revisions in this document."
Exit Sub
End If
'count the number of words in the revisions collection here
Documents.Add
ActiveDocument.Range.InsertAfter sallrev
Set sAllRevWdCount = Dialogs(wdDialogToolsWordCount)
sAllRevWdCount.Execute
numWords = sAllRevWdCount.Words
MsgBox "Number of revised words:" & numWords
End Sub
----------- End of macro -----------------
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Carlos Chalhoub > écrivait :
In this message, < Carlos Chalhoub > wrote:

|| Hi listmates,
||
|| I created a macro that counts the number of revised words in a document.
The
|| problem is that I don't want the macro to count the deleted text, just
the
|| new text. What is happening is that when I hide the deleted text
|| (Options.DeletedTextMark = wdDeletedTextMarkHidden), Word is bombing and
I
|| have to shut it down through task manager. When I omit the
|| "Options.DeletedTextMark = wdDeletedTextMarkHidden" line, the macro
works,
|| but the deleted text is counted, which is not what I want.
||
|| I'm at my wits end. Can somebody show me what I'm doing wrong?
||

Try this. Instead of hiding deleted word, I just process inserted text.

'_______________________________________
Sub UnitFileCompare()

Dim oRev As Revision
Dim sallrev
Dim sAllRevWdCount
Dim numWords
Dim TempDoc As Document

If ActiveDocument.Revisions.Count <> 0 Then
'make a revisions collection here
For Each oRev In ActiveDocument.Revisions
If oRev.Type = wdRevisionInsert Then
sallrev = sallrev & oRev.Range.Text & vbCrLf
End If
Next oRev
Else
MsgBox "There are no revisions in this document."
Exit Sub
End If

'count the number of words in the revisions collection here
Set TempDoc = Documents.Add
TempDoc.Range.InsertAfter sallrev

Set sAllRevWdCount = Dialogs(wdDialogToolsWordCount)
sAllRevWdCount.Execute
numWords = sAllRevWdCount.Words
MsgBox "Number of revised words:" & numWords

TempDoc.Close wddonotsaechanges

End Sub
'_______________________________________

Also, beware that "word" is a funny concept in Word. If the user adds a
paragraph marks,it will be counted as a word...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Carlos Chalhoub

Merci Jean-Guy.

It works well. However, it is bypassing the footnotes. I tried to use the
..CountFootnotes argument but the syntax does not seem to be working. I tried
this:
Set sAllRevWdCount = Dialogs(wdDialogToolsWordCount).CountFootnotes
I tried this:
sAllRevWdCount.CountFootnotes
But the macro does not like it. Any ideas?

By the way, MS-Word counts paragraph marks and the like if I use
..Words.Count. The dialog does not do that.

Best regards
Carlos
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Carlos Chalhoub > écrivait :
In this message, < Carlos Chalhoub > wrote:

|| Merci Jean-Guy.
||
|| It works well. However, it is bypassing the footnotes. I tried to use the
|| .CountFootnotes argument but the syntax does not seem to be working. I
tried
|| this:
|| Set sAllRevWdCount = Dialogs(wdDialogToolsWordCount).CountFootnotes
|| I tried this:
|| sAllRevWdCount.CountFootnotes
|| But the macro does not like it. Any ideas?

I am not sure I follow. The tracked changes are not being pickup in
footnotes or the word counting is not accurate?

||
|| By the way, MS-Word counts paragraph marks and the like if I use
|| .Words.Count. The dialog does not do that.

Right, forgot about that!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Carlos Chalhoub

Salut Jean-Guy,

The revisions in the footnotes are not being picked up. I tried the
following as well, but the result is the same, ie Word is dumping all the
revisions in tempDoc, except the ones in the footnotes.

sAllRevWdCount = TempDoc.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)

Carlos
 
C

Carlos Chalhoub

Stupid me. tempDoc only has straight text, so the ComputeStatistics line
does not advance me one bit.

OK, what I need is for the revisions in the footnotes to be picked up. I
know that I have to change the range to include all stories, but I'm not
sure how to.

Thanks for your help.
Carlos
 
C

Carlos Chalhoub

I got it. I modified the code under
http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm and came up
with the following. It works perfectly, but it is rather slow.


Sub UnitFileCompare()

Dim oRev As Revision
Dim oRevf As Revision
Dim sallrev
Dim sallrevf
Dim sAllRevWdCount
Dim TempDoc As Document

If ActiveDocument.Revisions.Count <> 0 Then
'make a revisions collection from the main story here
For Each oRev In ActiveDocument.Revisions
If oRev.Type = wdRevisionInsert Then
sallrev = sallrev & oRev.Range.Text & vbCrLf
End If
Next oRev

'make a revisions collection from the first story of each non-main type here
For Each myStoryRange In ActiveDocument.StoryRanges
If myStoryRange.StoryType <> wdMainTextStory Then
For Each oRevf In myStoryRange.Revisions
If oRevf.Type = wdRevisionInsert Then
sallrevf = sallrevf & oRevf.Range.Text & vbCrLf
End If
Next oRevf
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
'make a revisions collection from the remaining stories of each non-main
type here
For Each oRevf In myStoryRange.Revisions
If oRevf.Type = wdRevisionInsert Then
sallrevf = sallrevf & oRevf.Range.Text & vbCrLf
End If
Next oRevf
Loop
End If
Next myStoryRange
Else
MsgBox "There are no revisions in this document."
Exit Sub
End If

'count the number of words in the revisions collection here
Set TempDoc = Documents.Add
TempDoc.Range.InsertAfter sallrev
TempDoc.Range.InsertAfter sallrevf

sAllRevWdCount = TempDoc.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)
MsgBox "Number of revised words:" & sAllRevWdCount

TempDoc.Close wdDoNotSaveChanges

End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Carlos Chalhoub > écrivait :
In this message, < Carlos Chalhoub > wrote:

|| I got it. I modified the code under
|| http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm and came up
|| with the following. It works perfectly, but it is rather slow.

Glad you found it. I was going to suggest:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
It has a slightly more robust approach (see the last "version").

If all you are after are changes in the main text and the footnotes, it
might run faster if you used:

If myStoryRange.StoryType = wdFootnotesStory Then
instead of
If myStoryRange.StoryType <> wdMainTextStory Then


You could even try to remove the first "For Each ... Next" block and use

If myStoryRange.StoryType = wdMainTextStory _
Or myStoryRange.StoryType = wdFootnotesStory Then

Just a tought!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top