Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

search macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nyydo - 21 Jun 2007 05:00 GMT
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
Graham Mayor - 21 Jun 2007 06:13 GMT
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

> 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
nyydo - 21 Jun 2007 06:38 GMT
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


> Not much information there to go on, but this was posted recently :(
>
[quoted text clipped - 29 lines]
> > best regards
> > nyydo
Bear - 21 Jun 2007 16:10 GMT
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
Signature

Windows XP, Word 2000

nyydo - 22 Jun 2007 05:09 GMT
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

> Nyydo:
>
[quoted text clipped - 50 lines]
>
> Bear
Graham Mayor - 22 Jun 2007 07:31 GMT
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

Signature

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> dear bear
> first i want to thank u for answering me
[quoted text clipped - 65 lines]
>> --
>> Windows XP, Word 2000
nyydo - 22 Jun 2007 07:48 GMT
it works
thank u

> You could use an input box eg
> add the lines
[quoted text clipped - 78 lines]
> >> --
> >> Windows XP, Word 2000
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.