small macro creation

J

justamailman

Hi, I am creating a small macro that included a find search. I am searching
for a word first and than it has to find "screen #" after. But I want my # (a
number)to be added as i want each time. How do I go about that please?
 
H

Helmut Weber

Hi,
hm..., I am assuming, at first, you'd like to
search for a word, lets say "combined", and find all
"screen #" after it.
Here is a code snippet to get you going.
Sub Test446()
Dim i As Integer
ResetSearch
Selection.HomeKey unit:=wdStory
Selection.ExtendMode = False
With Selection.Find
.Text = "Combined"
If .Execute Then
Selection.Collapse direction:=wdCollapseEnd
.Text = "screen [0-9]{1;}" ' screen + any simple number
.MatchWildcards = True
While .Execute
MsgBox Selection.Text
Wend
End If
End With
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
But!!!
In case you want to search for a word and to find out
how often "screen #" appears between this word and the next
same word or the documents end, it might get a bit complicated.
As, in this example, all "screen #" after the first
"combined" would be listed, no matter, whether another "combined"
would be there.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

justamailman

well my number # could be from 1 to lest than 40 and also I do not see a
place where I am asked to enter the # I am looking at each time. The word I
am looking is "source". And then it has to find the word "screen #(1-40 which
I tell my macro each time for a specific #). I hope it help you understand my
question
 
H

Helmut Weber

Hi,
here comes another demo, which searches from the
cursor position onwards for "source", and then
for "screen " plus the number from the inputbox.
But only to show the principle, there are many ways
of improvement:
Sub Test446()
Dim sFnd As String ' string to find
sFnd = InputBox("search for screen #")
sFnd = "screen " & sFnd
ResetSearch
Selection.Collapse
Selection.ExtendMode = False
With Selection.Find
.Text = "source"
.Wrap = wdFindStop
If .Execute Then
MsgBox "found: source"
Selection.Collapse Direction:=wdCollapseEnd
Else
MsgBox "not found: source"
Exit Sub
End If
.Text = sFnd
If .Execute Then
MsgBox "found: " & sFnd
Else
MsgBox "not found: " & sFnd
End If
End With
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
J

justamailman

thanks but why an other sub after the first end sub. sorry if i don't
understand
 
H

Helmut Weber

Hi,
ResetSearch resets search options to what an ordinary
user would expect. It relieves you of setting all search
options in your specific macro.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

justamailman

yes but if I leave the way you gave it to me it gives me an error "no
comments after end sub"
 
J

justamailman

also how would you copy the phrase between screen 5 and next screen 6. all
the screen # follow each other so the text is just between them. Thanks again
 
H

Helmut Weber

Hi,
this simplified example
searches for "source",
searches for "screen "x,
switches on extendmode,
searches for "screen "x+1,
shortens the selection by 8 characters
copies the selection to the clipboard.
I am assuming, that "resetsearch" is still available,
that all searched for expressions are in the text
and in the appropriate order, that the selection is
the insertion point and somewhere in the text before "source"!
Lots of assumptions, but otherwise it would not be an example anymore.
---
Sub Test448()
Dim sFnd1 As String ' string to find screen x
Dim sFnd2 As String ' string to find screen x + 1
sFnd1 = 5 'InputBox("search for screen #")
sFnd2 = sFnd1 + 1
sFnd1 = "screen " & sFnd1
sFnd2 = "screen " & sFnd2
ResetSearch
Selection.Collapse
Selection.ExtendMode = False
With Selection.Find
.Text = "source"
.Wrap = wdFindStop
.Execute
Selection.Collapse direction:=wdCollapseEnd
.Text = sFnd1
Selection.Collapse direction:=wdCollapseEnd
Selection.ExtendMode = True
.Text = sFnd2
.Execute
Selection.End = Selection.End - 8
Selection.Copy
End With
ResetSearch
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

justamailman

sorry Helmut but it does not work prpperly. It copies all the words after
"source" up to screen y. It need to copy only between screen x and screen y.
Your code look right to me but does not work. I tried to correct it in
different ways but with no success.
 
H

Helmut Weber

Hi, somehow an "execute" got lost:
Sub Test448()
Dim sFnd1 As String ' string to find screen x
Dim sFnd2 As String ' string to find screen x + 1
sFnd1 = InputBox("search for screen #") ' sreen x
sFnd2 = sFnd1 + 1
sFnd1 = "screen " & sFnd1 ' screen x
sFnd2 = "screen " & sFnd2 ' screen x + 1
ResetSearch
Selection.Collapse
Selection.ExtendMode = False
With Selection.Find
.Text = "source"
.Wrap = wdFindStop
.Execute
Selection.Collapse direction:=wdCollapseEnd
.Text = sFnd1
.Execute ' this line was missing !!!!!!
Selection.Collapse direction:=wdCollapseEnd
Selection.ExtendMode = True
.Text = sFnd2
.Execute
Selection.End = Selection.End - 8
Selection.Copy
End With
ResetSearch
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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