Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Simple macro to repeat through entire document.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carl - 24 Feb 2006 17:38 GMT
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.
Greg - 24 Feb 2006 17:56 GMT
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
Carl - 24 Feb 2006 19:42 GMT
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.
Greg - 24 Feb 2006 19:58 GMT
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.)
Carl - 24 Feb 2006 20:01 GMT
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!

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.