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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert blank page if document is odd number of pages

Status
Not open for further replies.

murfeezlaw

Programmer
Jul 21, 2010
11
US
I'm using an Access application to create a document(A) and insert many word documents(B) into it.

Is there a way to count the pages of the document being inserted? And if it's an odd number of pages to add a blank page? If i just do a page count of the document in VBA it'll count the pages of document A. I need it to count the pages in the document being inserted.

i tried...
objWord.Selection.InsertBreak Type:=wdSectionBreakOddPage

which worked great but i had to run some code at the end to fix the pages in the footers which defeated the purpose of the break on odd pages.


Dim s As Word.Section
Dim f As Word.Field
Dim fcode As Word.Range
For Each s In objWord.ActiveDocument.Sections
With s.Headers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
With s.Footers(wdHeaderFooterPrimary).Range
For Each f In .Fields
If f.Type = wdFieldNumPages Then
Set fcode = f.Code
fcode.Text = "SECTIONPAGES"
End If
Next f
.Fields.Update
End With
With s.Footers(wdHeaderFooterFirstPage).Range
For Each f In .Fields
If f.Type = wdFieldNumPages Then
Set fcode = f.Code
fcode.Text = "SECTIONPAGES"
End If
Next f
.Fields.Update
End With
Next s

Any ideas? Thanks guys

Ted
 
I would use a variable in the Access report in the header before the Word object to deterime the staring page say..

Code:
lngStartPage = Me.page

Then on the footer after the object...

Code:
IF (Me.Page - lngStartPage) Mod 2 <> 0 Then
      'Add blank page in Access here
End if

Unfortunately I forget how to add the page in Access but I know it is in one of the Access forums... I think it was one of the many reponses by PHV.
 
Is there a way to count the pages of the document being inserted? "

Not beforehand.

Gerry
 
Seeing Gerry's response and rereading the OP, I realize I completely missed the boat on what is trying to be accomplished...

I think Gerry nailed it on the head...

Although not being a Word Guru, I am thinking if you happened to have page breaks between each document you could open each document that is to be inserted and count its pages. But with out the page breaks, I do not see it working out. There would always be the difference of pages printed separately versus pages spanned.
 
Well you COULD open each file you want inserted - rather than just inserting it unknown. Then you can determine all sorts of things about it and act accordingly (like bring it into your building up document).

I am having trouble following what you are doing though. First of all you do not need to declare (and Set) the fcode Range object, you can action f (the variable holding the Field) directly. So, instead of:
Code:
With s.Footers(wdHeaderFooterPrimary).Range
   For Each f In .Fields
     If f.Type = wdFieldNumPages Then
       Set fcode = f.Code
       fcode.Text = "SECTIONPAGES"
     End If
   Next f
   .Fields.Update
End With
You can use:
Code:
' in PRIMARY footer
With s.Footers(wdHeaderFooterPrimary).Range
   For Each f In .Fields
     If f.Type = wdFieldNumPages Then
        f.Code.Text = "SECTIONPAGES"
     End If
   Next f
   .Fields.Update
End With
But again, I am not following your apparent issue with odd/even pages. Can you explain more clearly?

As an added comment, the above demonstrates the best-practice of clear naming.

fcode.Text = "SECTIONPASGES"

is VERY (too much in my opinion) close to
f.Code.Text = "SECTIONPASGES"

While using a full name such as aField (rather than "f") for the name of the generic field variable:

Dim aField As Word.Field

seems a bit longer, the advantage of reading unambiguous code is worth it. Plus using IntelliSense to the fullest helps a lot. It is not like you have to type out "aField" (instead of just "f") ; "aF" + Ctrl-Space fills in "aField" for you.
Code:
   For Each aField In .Fields
     If aField.Type = wdFieldNumPages Then
        aField.Code.Text = "SECTIONPAGES"
     End If
[/code[ seems easier to read to me.  I admit this is a personal opinion.

And easier to read is, gernally, a good thing.  Which bring sme back...I am not following what the problem is exactly.

OK.  You insert a SectionBreak before each file insertion.  Good.  That is a best-practice.  Again, you can NOT determine page numbers before you bring it in.  Word determines page numbers through the CURRENT printer driver.  Unitl it parses a file through the current printer driver actual page numbers are unknown.  Again, you can open the file, figure out the page issue and then either bring it in (from that open document) with, or without a blank page, and/or SectionBreaknextPage vs. SectionBreak Odd/Even.

Your choice.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top