Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Word XP Last page (Office XP)

    How can I programmatically go to the last page of a document and find the number of characters on that page? The document can be any number of pages in length. Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word XP Last page (Office XP)

    Try these lines:

    Dim lngWordCount As Long
    ' Move to end of document
    Selection.EndKey Unit:=wdStory
    ' Use predefined page bookmark to get the page
    ' Use ComputeStatistics to get accurate word count
    lngWordCount = ActiveDocument.Bookmarks("Page").Range.ComputeStat istics(wdStatisticCharacters)

    Note: wdStatisticCharacters doesn't count spaces; use wdStatisticCharactersWithSpaces if you want to count those too. ActiveDocument.Bookmarks("Page").Range.Characters. Count returns more or less the same as the count with spaces.

  3. #3
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: Word XP Last page (Office XP)

    Hans,

    Thanks for the code. Unfortunately it was a part of something I was trying to do but doen't help solve my dilemma.

    What I have is a variable number of documents that are prepared individually based on a text list of documents passed by an Access database.

    My program gets a 'boilerplate' document in the list and merges data into each document, saving it with a new name. Each document has a different header/footer. Each document also has different margins, etc.

    Once the documents are prepared, I need to concatenate them all together so I wind up with one large document which can be printed as a single print job.

    Since I'm printing duplex, I have to add a blank page to those documents that have odd number pages so that the next document begins printing on an odd page. This is done when each individual document is prepared.

    The problem I'm having is that when I go to the end of the concatenated document to insert the next document using:

    With Selection
    .EndKey Unit:=wdStory
    .InsertFile FileName:=strNextDoc, Range:="", ConfirmConversions :=False, Link:=False, Attachment:=False
    End With

    the headers/footers no longer appear, or appear incorrectly when the doument is inserted. And the new page setup properties don't carry along with it either.

    I suspect it has something to do with the section breaks and/or header/footer properties immediately following the section break added in the first step.

    Do you a reliable method via code to append a document and preserve everything in the inserted document?

    Any help would be deeply appreciated.

    This is using Word 2002.

    Thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word XP Last page (Office XP)

    Selection.EndKey Unit:=wdStory does not insert a section break, it only moves to the end of the document. I think you should insert a next-page section break before inserting the file:

    With Selection
    .EndKey Unit:=wdStory
    .InsertBreak Type:=wdSectionBreakNextPage
    .InsertFile ...
    End With

    (instead of wdSectionBreakNextPage, you can also use wdSectionBreakEvenPage or wdSectionBreakOddPage to force the inserted document to begin on and even or odd page.)

  5. #5
    2 Star Lounger
    Join Date
    Jun 2001
    Location
    York, Pennsylvania, USA
    Posts
    164
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Re: Word XP Last page (Office XP)

    Hans,

    The first pass (creating the individual documents) added the page break (or section break - I haven't had much luck with either) at the end of the individual document.

    During pass 2 where the documents are concatenated, when I append the succeeding document via the 'insert' is where Word decides that the headers and footers need to be inherited from the main document. If I turn off the 'Same as Previous' after the break, which Word decides is the default when inserting breaks, prior to the insert, then the newly inserted document loses its header/footer and picks up the empty header/footer.

    Very frustrating.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word XP Last page (Office XP)

    A page break is definitely not what you need, since page settings cannot be different on both sides of a page break. Perhaps you must insert a section break, and turn of Same as Previous before inserting the next file. But others know much more about that than I do, I hope they will offer more solid help than I can offer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •