MS Office Forum / Word / Programming / November 2004
small macro creation
|
|
Thread rating:  |
justamailman - 18 Nov 2004 16:16 GMT 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?
 Signature XP home , Office 97
any or all responses are gratefully accepted and thank you for your time
Helmut Weber - 18 Nov 2004 16:55 GMT 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/
Helmut Weber - 18 Nov 2004 16:58 GMT Always the same problem
>.Text = "screen [0-9]{1;}" ' german et al. version >.Text = "screen [0-9]{1,}" ' english et al. version --- Helmut Weber
justamailman - 18 Nov 2004 21:59 GMT 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
> Hi, > hm..., I am assuming, at first, you'd like to [quoted text clipped - 50 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 19 Nov 2004 08:49 GMT 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 --- Greetings from Bavaria, Germany Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word 2002, Windows 2000
justamailman - 19 Nov 2004 13:47 GMT thanks but why an other sub after the first end sub. sorry if i don't understand
> Hi, > here comes another demo, which searches from the [quoted text clipped - 51 lines] > "red.sys" & chr(64) & "t-online.de" > Word 2002, Windows 2000 Helmut Weber - 19 Nov 2004 13:58 GMT 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/
justamailman - 19 Nov 2004 14:09 GMT yes but if I leave the way you gave it to me it gives me an error "no comments after end sub"
> Hi, > ResetSearch resets search options to what an ordinary [quoted text clipped - 6 lines] > Word XP, Win 98 > http://word.mvps.org/ justamailman - 19 Nov 2004 15:15 GMT ok helmut I solve my problem
> yes but if I leave the way you gave it to me it gives me an error "no > comments after end sub" [quoted text clipped - 9 lines] > > Word XP, Win 98 > > http://word.mvps.org/ justamailman - 19 Nov 2004 15:49 GMT 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
> Hi, > here comes another demo, which searches from the [quoted text clipped - 51 lines] > "red.sys" & chr(64) & "t-online.de" > Word 2002, Windows 2000 Helmut Weber - 19 Nov 2004 20:26 GMT 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/
justamailman - 22 Nov 2004 13:29 GMT 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.
> Hi, > this simplified example [quoted text clipped - 41 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 22 Nov 2004 19:07 GMT 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/
|
|
|