First Occurrences

R

rachelle34

I have this code:

With Selection.Find
..Text = "From(*)RESTRICTED(*)Subject: O"
..Replacement.Text = "O"
..Forward = True
..Wrap = wFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchSoundsLike = False
..MatchAllWordForms = False
..MatchWildcards = True
End With

Selection.Find.Execute Replace:=wdReplaceAll

It's there to remove a header from an email document. So instead of
finding the first occurrence of 'Subject: O', it finds the last. Is
there some way to make it not do that?

Help?

_____
RAM
 
P

Peter Hewett

Hi RAM

The code actually replace the first "From(*)RESTRICTED(*)Subject: O"
starting wherever the selection object starts. Try this instead it replace
the first occurrence in the document:

Sub SimpleSandR3()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "From(*)RESTRICTED(*)Subject: O"
.Replacement.Text = "O"
.MatchCase = False
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceOne
End With
End Sub

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Rachelle

I've just notice that I've overwritten part of my code with yours! So
please replace the line:
With Selection.Find

with:
With ActiveDocument.Content.Find

Cheers - Peter

Hi RAM

The code actually replace the first "From(*)RESTRICTED(*)Subject: O"
starting wherever the selection object starts. Try this instead it replace
the first occurrence in the document:

Sub SimpleSandR3()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "From(*)RESTRICTED(*)Subject: O"
.Replacement.Text = "O"
.MatchCase = False
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceOne
End With
End Sub

HTH + Cheers - Peter
 
R

rachelle34

The code did not do anything to the the document. Can you give me anymore advice?
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Try placing

Selection.HomeKey wdStory

before your code to ensure that it starts from the beginning of the
document.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Doug

It wont do anything the Selection objects not being used, my amended code
should be:

Sub SimpleSandR3()
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "From(*)RESTRICTED(*)Subject: O"
.Replacement.Text = "O"
.MatchCase = False
.MatchWildcards = True
.Forward = True
.Wrap = wdFindStop
If .Execute(Replace:=wdReplaceOne) Then
MsgBox "Replacement made"
Else
MsgBox "Seach text not found"
End If
End With
End Sub

Rachelle, make sure you're using the above. I've just added a bit of debug
code to make it easier to see what's going on (or not!). If the code's still
not doing anything then maybe the text you are searching for does not match
the pattern you are searching with.

Also make sure the document you are searching is the ActiveDocument.

HTH + Cheers - Peter
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

I was intending to be used with Rachel's original code in which the
Selection Object was being used. I now note however that there were a few
other problems with her code. With corrections made for those problems, it
deletes the first instance of the text, no matter where the selection is
located when the text is run,

Also your original amended code works as is, (deleteing only the first
instance) in a document in which I create multiple instances of text that
will be found by the string that she is searching for so I think that
Rachel's problem must be that the string does not match the original text

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
R

rachelle34

So it does the replace but it does not replace the first instance o
'Subject: O' but skips the first instance and deletes every thing dow
to the second instance of 'Subject: O'. Any more advice
 
P

Peter Hewett

Hi rachelle

The revised code should work, if it's skipping the first instance, I'd guess
that there's a very subtle difference between the first and second
occurrence. To eliminate the code search for a single recurring word in the
text and see if it finds it. If it does, then you've eliminated the code and
the search pattern is the culprit.

HTH + Cheers - Peter
 

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