Simple macro to repeat through entire document.

C

Carl

Howdy,

I'm trying to get a simple macro to repeat throughout an entire
document. This will be used about once a week on a report. It copies 7
characters out of each page and pastes them to the beginning of the
page. The characters will be different on nearly every page, with a
few repeats. The number of pages will also differ, running anywhere
from 1,000 to 10,000. I'd like to hit a single key to run the job
for the whole job rather then hold it down. I have searched through
the threads, but haven't been able to figure out the solution.

I'm running this on a Windows XP machine with MS Word 2000, and
possibly MS Word '97 on a Win98 machine.

Selection.MoveDown Unit:=wdLine, Count:=3
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=7, Extend:=wdExtend
Selection.Copy
Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=3
Selection.Paste
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1,
Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub



I read in a thread that the following code could stop the macro at the
end of a document:

Do
//whatever
Loop Until (Selection.End = ActiveDocument.Content.End - 1)

However I haven't been able to get it to stop the macro and it runs
non-stop until I force Microsoft Word to close.

I was thinking I could get the stop to base on the .Forward of the Goto
Next so that when .Forward = False it stops, but I'm not sure how to
go about coding that.

Any help getting this to work would be greatly appreciated.

Thanks in advance,
Carl Morse.
 
G

Greg

Carl,

Save a copy of your document first and try this:

Sub Test()
ActiveDocument.Range(0, 0).Select
Dim i As Long
For i = 1 To ActiveDocument.ComputeStatistics(wdStatisticPages)
With Selection
.MoveDown wdLine, 3
.MoveRight wdWord, 1
.MoveRight wdCharacter, 7, wdExtend
.Copy
.HomeKey wdLine
.MoveUp wdLine, 3
.Paste
.GoTo wdGoToPage, wdGoToNext, 1
End With
Next i
End Sub
 
C

Carl

Gregg,

That worked beautifully thank you.

So, I'm guessing that the coding:

'
' Macro2 Macro
' Macro recorded 2/23/2006 by Carl Morse
'
is worthless text that should be removed?

Would I also change the Test() to fit whatever macro name I have?

Thanks again,
Carl Morse.
 
G

Greg

Carl,

Any text preceeded by ' is provided only as information and has no
bearing on the performance of the code. You can delete that if you
want.

You can name the macro whatever you want as long as it is one word and
you are careful not to duplicate an existing word command (e.g.,
FileSaveAs, etc.)
 
C

Carl

Greg,

Thanks. That's great. I think I can even see how to modify all my
other macros to work the same way. To do the whole page that is.

Brilliant!
 

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