Where am I on the page?

J

JB

Hi Folks,

I'm doing a find/replace using VBA and I only want the code to run through
the document once from top to bottom and was wondering if there was a way
for me to set the cursor on the first line, run through and do the checks
then when the checks are finished exit the sub?

Problem is that there may be more than one instance of the text I'm looking
for so I had to use a Do Until clause to keep teh search going but now I can
only stop it using a counter which is not where I want to be.

Any pointers appreciated.

J
 
P

Peter Hewett

Hi "JB" <johnnybhoy67"NOSPAM"@excite.com>

This simple procedure will do a straight forward search and replace on your document:

Public Sub SimpleSandR(ByVal strFind As String, _
ByVal strReplace As String)

With ActiveDocument.Content.Find
.Replacement.ClearFormatting
.ClearFormatting
.MatchCase = False
.MatchSoundsLike = False
.MatchWholeWord = False
.MatchWildcards = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.Text = strFind
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
End Sub

You call it like this:
SimpleSandR "dog", "cat"

replaces all occurrences of the word "dog" with the word "cat".

HTH + Cheers - Peter
 
J

JB

Hi Peter,
Thanks for the reply but it's actually a non specific text I'm looking for
and it's more to do with character styles (Sorry should have posted a better
question).

I'm trying to eliminate instances of HRef style without Anchor text and vice
versa. I have written some code to do this but my problem is where my
cursor is positioned and knowing when I've looped round the document (which
is what I don't know how to find out)

Hope this makes things clearer.

J
p.s. Here's what I have so far...if theres a better way than this please let
me know.

Sub HRef()
Selection.WholeStory
Selection.MoveLeft wdCharacter, 1
iCount = 0

Do Until iCount > 50
If iCount = 0 Then
Selection.HomeKey
End If

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("HRef")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute

x = Selection.Characters.count
Selection.MoveRight wdCharacter, 3
Selection.MoveLeft wdCharacter, 2, wdExtend

If Selection.Style <> "Anchor" Then
Selection.MoveLeft wdCharacter, 1
Selection.MoveLeft wdCharacter, x, wdExtend
Reply = (MsgBox(strMessage, vbYesNo, strTitle))
If Reply = vbYes Then
Selection.Style = "Default Paragraph Font"
End If
Else
bGood = True
End If
iCount = iCount + 1
Loop
 
P

Peter Hewett

Hi "JB" <johnnybhoy67"NOSPAM"@excite.com>

Try modifying this code. It will search for all occurrences of whatever you want, but
when it finds what your searching for it has a separate inner loop where you can
manipulate the text you searched for:

Sub SearchWithInnerAction()
Dim rngReplace As Word.Range
Dim rngFound As Word.Range

Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True

' Find all occurrences in the document
Do While .Execute

' Use the rngFound object to manipulate your text
' not the rngReplace object
Set rngFound = rngReplace.Duplicate

' Manipulate your text here

' Setup range to continue the search after the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
End Sub

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