PDA

View Full Version : Examining Word Paragraph for Change Attributes



MWE
06-25-2008, 09:45 AM
I am using Word2000. I have a VBA-based application that does a fair amount of "behind the scenes" work on Word documents. I wish to handle things differently if a paragraph (or some text in a paragraph) has been "tagged" for deletion (Track Changes While Editing is ON, text block has been deleted, change has not yet been accepted). Given a range of text, how do I determine if it has been tagged for deletion? Can I get down to the character level?

Thanks

MOS MASTER
06-27-2008, 03:33 PM
Hi Mark, :hi:

I think you should take a look at the Revision object.

What you need is something like this:

Sub FindMarkedForDeletion()
Dim mForDel As Revision
For Each mForDel In ActiveDocument.Range.Revisions
If mForDel.Type = wdRevisionDelete Then
Debug.Print mForDel.Range.Text
End If
Next
End Sub


HTH

MWE
07-19-2008, 04:27 PM
Thanks for the reply. I have fiddled around with various approaches and have encountered a problem that I do not understand. It appers that revisions belong to the document and not to anything else. For example, if I select a block of text and run a vba proc to display the # or revisions and then info on each revision in the selection/range: Sub Revisions_Display()
'
' test procedure to display info on revs for a particular selection
'

Dim Num As Long
Dim rev As Revision

MsgBox "# revisions in this documentn = " & ActiveDocument.Revisions.Count
MsgBox "# revisions in this selection = " & Selection.Range.Revisions.Count
Num = 0
For Each rev In Selection.Range.Revisions
Num = Num + 1
MsgBox Num & vbTab & rev.Type & vbCrLf & rev.Range.Text
Next rev

End Subthe # of revisions is correct, but the info on revisions then displayed is for every revision in the document.

What am I doing wrong?

Thanks

macropod
07-21-2008, 03:21 PM
Hi MWE,

How about something based on:
Sub TestRevisions()
Dim oRev As Revision
Dim i As Integer
Dim j As Integer
With ActiveDocument
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Revisions.Count > 0 Then
With .Paragraphs(i).Range
For j = 1 To .Revisions.Count
If .Revisions(j).Type = wdRevisionDelete Then MsgBox .Revisions(j).Range.Text
Next
End With
End If
Next
End With
End Sub

MWE
07-21-2008, 07:37 PM
Thanks for your reply, but your code seems to do the same thing mine does, i.e., spits out all revisions. Perhaps I was not clear in what my code does. It seems to ignore the fact that the loop is for the selection. It does output each revision separately, but the loop indexes through every revision in the document rather than each revision in the selection. I want to "operate" only on the revisions associated with a given selection. If I could figure out a way to know the "parent" paragraph for each revision, I could probably develop some bandaid code that would work. But although each revision has a parent, the properties of that parent are a mystery.

I did a bit more testing with Word 2003 and found exactly the same problems; so it is not a Word2K anomoly.

I also played around with Comments and found that they too are tied to the document and not to specific paragraphs even though Selection.Range.Comments.Count displays the correct number of comments for that selection.

What I am trying to do with comments is pretty simple. I wish to copy comments from one version of a doc to another (almost the same) version and retain the comment's author and color. I could copy the relevant text; that seems to copy the comment's author and color, but that also screws up formating, bookmarks and several other things associated with the target paragraph. It would be so easy if I could just click on the commment and change the author (and color)

macropod
07-21-2008, 10:16 PM
Hi MWE,

The only reason the code in my previous post outputs all the deletions is that it loops through all the paragraphs. If, for example, you changed 'For i = 1 To .Paragraphs.Count' to 'i = 2' and delete the last 'Next' statement, then only the deletions for paragraph 2 will be reported.

Since you appear only to want the revisions for a selected paragraph, try:
Sub TestRevisions()
Dim oRev As Revision
Dim i As Integer
Dim j As Integer
For i = 1 To Selection.Paragraphs.Count
With Selection.Paragraphs(i)
If .Range.Revisions.Count > 0 Then
With .Range
For j = 1 To .Revisions.Count
If .Revisions(j).Type = wdRevisionDelete Then MsgBox .Revisions(j).Range.Text
Next
End With
End If
End With
Next
End SubAll you need to do is position the cursor anywhere in the subject paragraph, then run the code. If the selection spans more than one paragraph, the deletions from all selected paragraphs will be reported.

The code was developed and tested with Word 2000.

The same approach would work with Comments.

MWE
07-22-2008, 11:26 AM
Your code works as you have written it, but if you display every revision in the inner loop regardless of the "type", you get funny results. And that may be a hint regarding what is wrong. For a particular target selection, there should be two revisions, but .range.revisions.count states 3. The last one is a type = 11 which according to the ojbect browser is not a legitimate rev type.

I created some test docs and this strange phenomena seems to be independent of the doc or the version of word I am using. I need to do some additional testing, but you have gotten me on the right track and if I simply test for rev type and ignore any objects that I do not want to operate on anyway, I should be OK.

Thanks

macropod
07-22-2008, 03:52 PM
Hi MWE,

Interesting. What gets selected if you change:
If .Revisions(j).Type = wdRevisionDelete Then MsgBox .Revisions(j).Range.Text
to:
If .Revisions(j).Type = 11 Then
.Revisions(j).Range.Select
Exit For
End If

MOS MASTER
07-23-2008, 06:02 PM
Mark,

Do you have a testdocument which gives you funny results with the code you are using?

And specify the expected result.

MWE
07-24-2008, 10:00 AM
Mark,

Do you have a testdocument which gives you funny results with the code you are using?

And specify the expected result.I did a little more testing and have concluded:

the revised approach, i.e., only processing valid rev types seems to work in the two small test docs I composed.
the revised approach does NOT work in the original document I was working with when I started playing with revisions. What seems to happen in the orig document is quite strange. The display of # paras in the selection is correct but after that ??? Based on what is displayed, paragraph processing starts at the first para containing a revision after the selection and either just keeps spitting out data on that revision OR sequences through the rest of the paras in the document OR processes "something" until I kill word (cntl break does not stop it). Makes no sense to me!:banghead: