Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Word VBA: How do I make a macro run until the end of the file


(!)

HowdeeDoodee's Avatar
HowdeeDoodee HowdeeDoodee is offline
Member with 585 posts.
THREAD STARTER
 
Join Date: Aug 2004
Experience: Intermediate
05-Sep-2006, 01:44 AM #1
Word VBA: How do I make a macro run until the end of the file
I have this macro. Now, to run the macro I have to hold down the Alt+R key continuously until the macro repeats until the macro reaches the end of the file. How do I change the macro so when i hit the shortcut key, the macro repeats on its own without me having to hold down the shortcut key?

Code:
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "[|]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "^t"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "change^t"
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Find.ClearFormatting
       With Selection.Find
        .Text = "^p"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="[OR]"
    Selection.Paste
    Selection.MoveRight Unit:=wdCharacter, Count:=1
Thank you in advance for replies.
Jimmy the Hand's Avatar
Member with 1,220 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: Level 15 Paladin
05-Sep-2006, 04:40 AM #2
Well, there is the primitive way, and there is the sophisticated way.
I tell you how it's done in the primitive way.
Put a "Do" command to the beginning of the macro, and put a "Loop" command to the end of the macro.
When you hit the shortcut, the macro will run until no match is found, then stop with an error message.
This is good for a few-time use, for yourself. If you create an application for other users, however, the sophisticated way is recommended. It's the same Do...Loop cycle, plus a conditional exit from the loop. Something like:

If nomatch Then Exit Do

I most probably could do the coding, if you provide a sample text to test the modified macro on.
HowdeeDoodee's Avatar
HowdeeDoodee HowdeeDoodee is offline
Member with 585 posts.
THREAD STARTER
 
Join Date: Aug 2004
Experience: Intermediate
05-Sep-2006, 05:06 AM #3
Thank you Jimmy.

I also found an answer much like your own. If a user wants to run a macro 100 times, here is how to do it in code.

To loop the macro in code add this around your macro (after the sub line & any declarations)

Sub NameOfTheMacro

For i = 1 to 100

'your code

next i

end sub

example:
Sub Beeps()
For i = 1 To 50
Beep 'your macro
Next i
End Sub

Thank you again, Jimmy.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑