Discussion:
Word macro with unix grep like function
(too old to reply)
Nimmi Srivastav
2010-06-10 13:12:56 UTC
Permalink
Hi,

I would appreciate if someone could help me with an MSWORD macro that
does the following:
(1) Prompts the user to enter a search string
(2) Searches all occurrences of the specified string in the active
document
(3) Dumps all lines containing the specified string in a new window
(line being defined as a string of characters that ends with ^p)

Thanks in advance,
Nimmi
Graham Mayor
2010-06-10 13:38:30 UTC
Permalink
How about

Dim Source As Document
Dim Target As Document
Dim oPara As Paragraph
Dim sText As String
Dim oRng As Range
Set Source = ActiveDocument
Set Target = Documents.Add
sText = InputBox("Enter string")
For Each oPara In Source.Range.Paragraphs
If InStr(1, oPara.Range, sText) Then
Target.Range.InsertAfter oPara.Range
End If
Next oPara

http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Post by Nimmi Srivastav
Hi,
I would appreciate if someone could help me with an MSWORD macro that
(1) Prompts the user to enter a search string
(2) Searches all occurrences of the specified string in the active
document
(3) Dumps all lines containing the specified string in a new window
(line being defined as a string of characters that ends with ^p)
Thanks in advance,
Nimmi
n***@yahoo.com
2010-06-10 17:51:17 UTC
Permalink
Post by Graham Mayor
How about
Dim Source As Document
Dim Target As Document
Dim oPara As Paragraph
Dim sText As String
Dim oRng As Range
Set Source = ActiveDocument
Set Target = Documents.Add
sText = InputBox("Enter string")
For Each oPara In Source.Range.Paragraphs
    If InStr(1, oPara.Range, sText) Then
        Target.Range.InsertAfter oPara.Range
    End If
Next oPara
http://www.gmayor.com/installing_macro.htm
--
Works like a charm.....

Thanks - NS
Graham Mayor
2010-06-11 05:50:56 UTC
Permalink
You are welcome :)
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Post by Graham Mayor
How about
Dim Source As Document
Dim Target As Document
Dim oPara As Paragraph
Dim sText As String
Dim oRng As Range
Set Source = ActiveDocument
Set Target = Documents.Add
sText = InputBox("Enter string")
For Each oPara In Source.Range.Paragraphs
If InStr(1, oPara.Range, sText) Then
Target.Range.InsertAfter oPara.Range
End If
Next oPara
http://www.gmayor.com/installing_macro.htm
--
Works like a charm.....

Thanks - NS
Loading...