Find Replace within whole story except where deleted text

J

Jeremy

Hello.

I wonder if someone could offer me some help as I am not a programmer and
I'm having some trouble achieving what I want (in Word 2003 vba).

I would like to be able to execute a find and replace operation throughout a
storyrange except where the text is marked as deleted (with tracked changes).
That is to say I would like to replace "x" with "y" in the wholestory range
except where text is of the revision type wdrevisiondelete.

I would really appreciate any help. I have written so code, which I can post
if someone would like. However, as I am not a programmer I'm a bit
self-councious about it! I'm sure it's not elegant. I've just spent ages
reading how to program in vba and got the basics.
 
J

Jean-Guy Marcil

Jeremy said:
Hello.

I wonder if someone could offer me some help as I am not a programmer and
I'm having some trouble achieving what I want (in Word 2003 vba).

I would like to be able to execute a find and replace operation throughout a
storyrange except where the text is marked as deleted (with tracked changes).
That is to say I would like to replace "x" with "y" in the wholestory range
except where text is of the revision type wdrevisiondelete.

I would really appreciate any help. I have written so code, which I can post
if someone would like. However, as I am not a programmer I'm a bit
self-councious about it! I'm sure it's not elegant. I've just spent ages
reading how to program in vba and got the basics.

If you change the display to "Final" or "Final Showing Markup" on the
reviewing toolbar, the search will not include the text that was deleted,
provided you use the Selection object. If you use the Range object, the
deleted words will be included. Working around that can get hairy because the
Revision collection is slightly buggy, to say the least. This is a rare case
where, to keep things simple, I would recommend using the Selection object.

By the way, if you do not post any code, nobody can help you improve your
coding and help you get rid of bad habits... So, don't be self-conscious
about it, this a is a great place to learn...
 
J

Jeremy

Hi Jean.

Thanks so much for taking an interest.

Ok, I have set the dispay to "final showing markup" and find/repalce finds
and replaces the text marked as tracked changes as well as the non-tracked
changes text.

If you set the view to "final" the find/repalce won't touch tracked changes
text. However, I want to change stuff that's been inserted, just not stuff
that is a deletion.

I am aware that the revisions collection is buggy. I think you need to use
the Inrange function to test whether the text is in the revisions collection.

Jean, re: your point about the range object, I'm sorry but I don't quite
understand what you mean.

The (awful) code I wrote is as follows. You'll probably tell I'll have to
spend a while testing - I learn as I go! I was trying to iterate through the
document testing whether the text I found was in the revisions collection and
a deletion. If it satisfied these two conditions I thought I'd allow the
macro to move on. If it failed, then the find/replace operation should run.
Undoubtedly I failed miserably!

Sub FindReplace999()

Dim myrange As Range
Set myrange = Selection.Range

Selection.HomeKey Unit:=wdStory

Selection.find.ClearFormatting
With Selection.find
.Text = "x"
.Replacement.Text = "y"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

While Selection.find.Execute

If Selection.InRange(myrange) = False Then
If Selection.Type = wdRevisionDelete Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
counter = counter + 1

Else
Selection.find.Execute Replace:=wdReplaceOne

End If
End If

Wend

End Sub

Best Regards

Jeremy
 
J

Jean-Guy Marcil

Jeremy said:
Hi Jean.

Thanks so much for taking an interest.

Ok, I have set the dispay to "final showing markup" and find/repalce finds
and replaces the text marked as tracked changes as well as the non-tracked
changes text.

I thought you wanted to replace all instances, except for those that were
deleted while track changes was on. If you set "final showing markup" this is
what happens with this code:

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting
With Selection.Find
.Text = "x"
.Replacement.Text = "y"
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute Replace:=wdReplaceAll


I meant to use, as you are doing, the Selection.Find object and not the
Range.Find object.

Your code does not work because your are working with a range object
(myrange)set to the initial selection. As .Execute is performed, the found
text is not connected to this initial range anymore...
Also, Selection.Type does not return a Revision.Type.
It returns:
wdSelectionBlock
wdSelectionFrame
wdSelectionIP
wdSelectionRow
wdNoSelection
wdSelectionColumn
wdSelectionInlineShape
wdSelectionNormal
wdSelectionShape

"wdRevisionDelete" is returned by .Range.Revisions(1).Type, like
Selection.Range.Revisions(1).Type ... But you must first make sure that the
range is part of the revision collection, which can be tricky in your case...
 
J

Jeremy

Hi Jean,

I've been looking at what you said all morning and I'm still having problems.

I wanted to find and replace everywhere except where text was marked for
deletion. If the text is marked as a deletion then it should be ignored. This
is how far I've got. I don't understand how to test whether text is in fact
marked for deletion as part of a find/repalce operation.

Is there any further help you could give me.

I'm stuggling to apply the pointers you gave me. My new code is:

Sub FindReplace3()

'Dim myrange As Range
'Set myrange = Selection.Range

'Selection.HomeKey Unit:=wdStory


While Selection.find.Execute
With Selection.find
If Selection.Range.Revisions(1).Type = wdRevisionDelete And .Text = "x" Then
'then ignore

Else
.Text = "x"
.Replacement.Text = "y"
.Execute Replace:=wdReplaceOne
End If
End With
Wend
End Sub


Best Regards

Jeremy
 
J

Jean-Guy Marcil

Jeremy said:
Hi Jean,

I've been looking at what you said all morning and I'm still having problems.

I wanted to find and replace everywhere except where text was marked for
deletion. If the text is marked as a deletion then it should be ignored. This
is how far I've got. I don't understand how to test whether text is in fact
marked for deletion as part of a find/repalce operation.

That was my whole point, you do not need to check if the found text is of
the wdRevisionDelete type. That deleted text will be ignored if you use the
code I posted (Using Selection.Find) in my second post *and* set he view to
"Final" or "Final Showing Markup", it will work like you want.
Have you tried the code I posted?
 
J

Jeremy

Jean! My sincere apologies. Yes ofcourse! Your code did work and you are
right about the Final view.

I had a total mental block and simply didn't register what was in front of
me.

Thank you ever so much. I realise I don't need to worry about revisions
collections etc.

Cheers. Sorry to have laboured such an obvious point. I got locked into a
mindset.

Thank you. :) I'm really happy now!
 
J

Jean-Guy Marcil

Jeremy said:
Jean! My sincere apologies. Yes ofcourse! Your code did work and you are
right about the Final view.

I had a total mental block and simply didn't register what was in front of
me.

Thank you ever so much. I realise I don't need to worry about revisions
collections etc.

Cheers. Sorry to have laboured such an obvious point. I got locked into a
mindset.

Glad you worked it out.

And don't worry, we've all been there, especially me...
 

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