Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro to advance page 2

Status
Not open for further replies.

bjzielinski

IS-IT--Management
Nov 8, 2001
93
US
have a macro that does the following to a multiple page document.
On the current page, it changes the title to something, say Title 1, then prints the page.
It would then change the title again to Title 2, and print the page.

It does this 5 times. There is no looping involved for this process. However, I would like to perform this for each page, then go to the next page and perform the macro again. I was thinking a Do While not EOF or something to that extent.

I'm also not sure the correct function to advance to the next page.

All this is necessary to keep pages together when printed. Thanks!

Bill Zielinski
bzielinski@co.midland.mi.us
County of Midland, Michigan
 

bjzielinski,
I gave it my best shot, but there's one little problem; although it determines the correct number of pages in the document, it executes the loop one time less. In other words, it doesn't execute the loop for the last page. I've tried to solve the problem, but I'm stumped. I hope the code is of some use to you anyway.
Good luck!
Code:
Sub nextpage()
'
'For each page in doc do something 5 times
'
'
'Declare variables
Dim NumPages
Dim iCount
'Go to Beginning of Document
    Selection.HomeKey Unit:=wdStory
'Determine number of pages in doc and assign to NumPages
    NumPages = ActiveDocument.Content.ComputeStatistics(wdStatisticPages)
'Do everything between Do...Loop until active page = last page of doc
Do Until Selection.Information(wdActiveEndPageNumber) = NumPages
    'Do everything between For...Next maximum number of iCount
    For iCount = 1 To 5
        'Type the word Title then insert current number from iCount
        Selection.TypeText Text:="Title " & iCount
        'Press Enter to end paragraph
        Selection.TypeParagraph
        'Print the current page
        Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
            wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
            Collate:=True, Background:=False, PrintToFile:=False, PrintZoomColumn:=0, _
             PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
        'Select the previous line consisting of text Title & iCount
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        'Delete the line
        Selection.Delete Unit:=wdCharacter, Count:=1
    'Repeat the For...Next command until maximum count has been reached
    Next iCount
        'Go to next page
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
'Repeat Do...Loop command until current page = last page of doc
Loop
End Sub
 

bjzielinski,
I won't know until I'm back in the office tomorrow and have a chance to test it, but I think maybe by modifying the code as follows, it may work properly:

Current Code
Code:
Do Until Selection.Information(wdActiveEndPageNumber) = NumPages
...
Loop

Modified Code
Code:
Do
...
Loop Until Selection.Information(wdActiveEndPageNumber) = NumPages
 
Well, I'm afraid that modification didn't do the trick. Does anyone know the solution?
 
Thank you so much for your assistance.

Since it does not print the last page, I have two suggestions, perhaps one of them could work?

1. Put the same thing that is inside the loop OUTSIDE the loop, that is it would run when the do while loop is done. Hopefully it would be on the last page.

2. Make NumPages = ActiveDocument.Content.ComputeStatistics(wdStatisticPages) +1 It's possible it's skipping the last page because it would check whether the condition of pageno=number ofpages, and when its on the last page, that is true, thus the loop does not run.

I don't have access right now to test this out, but when I do I will try.

Thanks again!

Bill Zielinski
bzielinski@co.midland.mi.us
County of Midland, Michigan
 
It now works, thanks to PHV in the VBA forum, thread707-872503.

Correct code:
Code:
Sub nextpage()
'
'For each page in doc do something 5 times
'
'
'Declare variables
Dim myPageNum
Dim NumPages
Dim iCount
'Go to Beginning of Document
    Selection.HomeKey Unit:=wdStory
'Determine number of pages in doc and assign to NumPages
    NumPages = ActiveDocument.Content.ComputeStatistics(wdStatisticPages)
'Do everything between Do...Loop until active page = last page of doc
For myPageNum = 1 To NumPages
    'Do everything between For...Next maximum number of iCount
    For iCount = 1 To 5
        'Type the word Title then insert current number from iCount
        Selection.TypeText Text:="Title " & iCount
        'Press Enter to end paragraph
        Selection.TypeParagraph
        'Print the current page
        Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
            wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
            Collate:=True, Background:=False, PrintToFile:=False, PrintZoomColumn:=0, _
             PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
        'Select the previous line consisting of text Title & iCount
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        'Delete the line
        Selection.Delete Unit:=wdCharacter, Count:=1
    'Repeat the For...Next command until maximum count has been reached
    Next iCount
        'Go to next page
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
'Loop
Next myPageNum
'Loop Until Selection.Information(wdActiveEndPageNumber) = NumPages + 1
End Sub
 
Here may be shorter code. It uses a formfield to insert the changing Title text. You may or may not want to delte it

Code:
Sub 5PrintsNewTitle()
'
Dim lngDocPages As Long
Dim var
Dim var2
Dim i As Integer

lngDocPages = ActiveDocument.ComputeStatistics(wdStatisticPages)
   
On Error Resume Next
Selection.HomeKey Unit:=wdStory
    i = 1
For var = 1 To lngDocPages
 '  insert a formfield
  Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
  For var2 = 1 To 5
' update formfield text ("Title" +  counter)
' print current page
    ActiveDocument.FormFields("Text1").Result = "Title" & i 
    Application.PrintOut FileName:="", Range:=wdPrintCurrentPage, Item:= _
            wdPrintDocumentContent, Copies:=1
    i = i + 1
  Next
' delete formfield
  ActiveDocument.Bookmarks("Text1").Delete
' go to next page
  Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1, Name:=""
' reset counter
  i = 1
Next
End Sub

Gerry
 
Beautiful, fumei!
Much shorter and more efficient (as usual from you). I wish my brain would comprehend and absorb VBA more easily!
By studying your examples (and others), I'm learning a great deal and will no doubt improve in time. Thank you so much.

Cheers (with a star).
 
Thanks dcompto. Much appreciated. Check out some other formfield code in Thread68-871395

Actually, it isn't really a thread, as there have been no responses or other posts. I posted three chunks of code using formfields, that may be useful.

I don't know if you are doing this, but these are some of things I do to help me move along. Just a suggestion.

Build a separate .DOT file that you can load as a global template. Use THAT file to store your code. Back it up. If it is loaded globally, you can use all the code anywhere.

Do not store your VBA code in Normal.dot unless you have to. That way, if you ever need to delete Normal. your code will be safe.

Copy EVERY interesting code you find into that .DOT file.

Comment the code as much as possible, both in the document itself, and within the code.

Any code that seems remotely in the same area, put them together in their own modules: modFormFields, modPrinting, modUtilities, modFileandFolders etc etc.



Gerry
 
fumei,
I tried to award you a second star, but it didn't work. Will try again later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top