search macro

N

nyydo

hi
i want a macro that when run i can give a sritng to search
the nacro will fund the strung and after will contunue
best regards
nyydo
 
G

Graham Mayor

Not much information there to go on, but this was posted recently :(

Sub Demo()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = "stuff to find"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False

Do While .Execute
' at this point, the range
' oRg contains the most
' recently found occurrence
' of the.Text string...
' do whatever you need to do
' for example:
MsgBox oRg.Text & " at " & oRg.Start

' prepare for next loop
oRg.Collapse wdCollapseEnd
Loop
End With
End Sub
 
N

nyydo

thank u
the macro i want has to do' for example

go to the first letter in the text
ask me for the text i want to search
find it
go 2 letters to right

thanks again
 
B

Bear

Nyydo:

When you record a macro of using the right arrow key two characters to the
right, what code do you get? That's the code you should put in the Do While
loop.

The code Graham showed you will process the entire document from start to
end, without the need of positioning the cursor at the start of the document.

You probably want to:

- find the text
- move the cursor
- stop the macro and do some manual work?
- repeat the whole deal, but starting from where you are

You do that by controlling the start point of the range object. Let me
modify the code Graham supplied to do that.

Sub Demo()

Dim oRg As Range

' Set range from start to end
Set oRg = ActiveDocument.Range

' Start range at current location
oRg.Start = Selection.Start

' Exclude field codes and hidden text
With oRg.TextRetrievalMode
.IncludeFieldCodes = False
.IncludeHiddenText = False
End With

' Find the next instance
With oRg.Find
.Text = "stuff to find"
.Wrap = wdFindStop
.Execute
If .Found = True Then
' Collapse the found object
oRg.Collapse
' Select the found object
oRg.Select
' Code developed by Nyydo
' to move the cursor
Else
MsgBox "The search item was not found."
End If
End With
End Sub


Bear
 
N

nyydo

dear bear
first i want to thank u for answering me
i learn a lot from your code
still there is something i did not recieve:
i need the macro fo many searcing
each of them search other text/
us ther any way that the text i am searching will not be part from the macro
and in the running time i'll be asked to give the text i want the macro to
search
thanks again
nydo
 
G

Graham Mayor

You could use an input box eg
add the lines

Dim sFindText As String
sFindText = InputBox("Enter the text to be found", "Find Text")

Immediately after the Dim line at the start of the macro
and then change

..Text = "stuff to find"
to
..Text = sFindText


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
N

nyydo

it works
thank u

Graham Mayor said:
You could use an input box eg
add the lines

Dim sFindText As String
sFindText = InputBox("Enter the text to be found", "Find Text")

Immediately after the Dim line at the start of the macro
and then change

..Text = "stuff to find"
to
..Text = sFindText


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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