• Logbook pagination: There has got to be a better way…

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Logbook pagination: There has got to be a better way…

    Author
    Topic
    #486327

    We use MS Word to create pages for the several types of log books we are required to keep. We have a Word document for each logbook that has a single page with a page number in the footer.

    We have a macro that we run that asks how many pages we want (usually 200), increments the page number and prints that page until the requested number of pages are printed.

    We use a PDF printer that creates a PostScript file. The problem we are seeing is that the first dozen pages are queued – you can see them in the printer queue – then the remaining pages are apparently sent directly to the PS file, and THEN the first dozen pages are printed.

    When that PS file is converted to PDF, the first dozen pages are at the end of the PDF, instead of at the front where they belong.

    I’ve tried spooling with immediate printing, and with delayed printing, and not spooling at all. Nothing seems to change the outcome.

    Here are the ways I’ve thought of attacking this problem:

    –Is there a way to have word do this automatically without creating a 200 page doc and without the macro sending one page at a time to the printer?

    –Is there a way to force the pages to ALL go directly to the PS file instead of piling up the first 12 in the queue?

    –Is there a way to expand the queue to hold all 200 pages so they’ll get processed IN ORDER?

    –Alternatively could the macro, instead of printing each page, append these pages together into a single large document and then print that? How would that be done in VBA?

    Thanks in advance!

    Viewing 7 reply threads
    Author
    Replies
    • #1357014

      Yes it can be done very easily using VBA. Can you post the code you are using currently?

      Wouldn’t it be more staightforward to avoid macros and just create documents of each logbook variant (with say 500 pages) and then the user can determine how many pages they want printed?

      • #1357124

        Andrew,
        Andrew,

        Having the logbook docs contain 500 pages, and re-paginating at the required starting number would be a way to do it. But occasionally the format of the page change and that change would have to be cascaded to all the pages.

        Mabe an alternative approach might be to have the macro take the one page doc, copy it as a separate document object, duplicate the pages X number of times, number the pages in that ONE doument, then send that ONE document to the PDF printer.

        Here is the code we are currently using:

        Sub paginate()

        ‘ paginate Macro
        ‘ Macro created 11/17/2005 by DM

        Dim Message As String, Title As String, Default As String, NumCopies As Long
        Dim Rng1 As Range
        Dim strPrinter As String
        Dim strOutput As String

        strOutput = “C:Documents and Settingsdm” + ActiveDocument.Name + “.ps”
        Set fs = CreateObject(“Scripting.FileSystemObject”)
        Set a = fs.CreateTextFile(strOutput, True)
        a.Close

        ‘ Set prompt.
        Message = “Enter the number of copies that you want to print”
        ‘ Set title.
        Title = “Print”
        ‘ Set default.
        Default = “1”

        ‘ Display message, title, and default value.
        NumCopies = Val(InputBox(Message, Title, Default))

        ‘Get the starting page number from “Settings.txt”
        SerialNumber = System.PrivateProfileString(“C:Settings.Txt”, _
        “MacroSettings”, “SerialNumber”)

        If SerialNumber = “” Then
        SerialNumber = 1
        End If

        ‘Find the current page number, bookmarked as “SerialNumber”
        Set Rng1 = ActiveDocument.Bookmarks(“SerialNumber”).Range
        Counter = 0

        ‘cache the current printer name and Switch to the PDF printer
        strPrinter = Application.ActivePrinter
        Application.ActivePrinter = “Adobe PDF3”

        ‘Generate the pages, printing each to the PDF printer
        While Counter < NumCopies
        Rng1.Delete
        'Rng1.Text = SerialNumber
        Rng1.Text = Format(SerialNumber, "0000#")
        ActiveDocument.PrintOut True, True, wdPrintCurrentPage, strOutput
        SerialNumber = SerialNumber + 1
        Counter = Counter + 1
        Wend

        'set the default prionter back to where it was before
        Application.ActivePrinter = strPrinter

        'Save the next page number back to the Settings.txt file ready for the next use.
        'NOTE: This is not neccessary since the same settings.txt is used for all logs,
        'and the numbers differ so it is always changed before running anyway.
        System.PrivateProfileString("C:Settings.txt", "MacroSettings", _
        "SerialNumber") = SerialNumber

        'Recreate the bookmark ready for the next use.
        'This IS reqired so the current page number in the doc is bookmarked for use the next time the macro runs.
        With ActiveDocument.Bookmarks
        .Add Name:="SerialNumber", Range:=Rng1
        End With

        End Sub

        I have no idea why they use a separate txt file to store the serial number since it is used for all the log books and has to be reset anyway, I'd just assume grab it the same way the page count it taken with an input box.

        Here is the settings.txt file:
        [MacroSettings]
        SerialNumber=30801

        Thanks

    • #1357045

      The problem with out-of-order pages is a by-product of background printing. The simple fix is to disallow it, via:
      .PrintOut Background:=False

      For the multi-page generation approach, see: http://www.msofficeforums.com/word-vba/12959-sequential-document-numbering.html

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1357125

        Thanks Macropod!
        Setting the background printing to false resolved the problem.

        And thanks for that link. That discussion looks interesting.

        Dunno if I’ll have the time to overhaul all the logbooks to do it that way, but at least the pages are in order now usign the current macro.

    • #1357129

      Macropod,

      With the background printing turned off, I am getting a “Print to file” dialog propmpting for a file name for a printer file (*.prn). Bu the postscript file is already created and THATS what I am after anyway. When I cancel that dialog and run the PS file, I get the PDF with pages in the correct order. How can I suppress the dialog, since it is not needed?

    • #1357181

      One other question:

      The macro works flawlessly now on our XP machines. But we have a Windows 7 machine that the code crashes on the ActiveDocument.PrintOut command. The odd thing is that it WILL execute and print several pages, then crash with a runtime error 5142.

      Then in debug, if you try to re-execute it just repeats the error. But if you move the pointer up one command to the
      Rng1.Text = Format(SerialNumber, “0000#”) command then resume, it will print several more pages before crashing with another 5142 error.

      I know the code is right, AND the PDF printer driver is working or it would not execute at all. If I just reset the execution pointer every time it crashes, eventually you get all the pages into the postscript file and it does create a PDF with all pages in the correct order.

      So why does it crash every 3 – 5 pages?

      I was thinking it might be a timing thing where the code is trying to execute another printout before the last one is done because the Win7 machine is running so much faster than the XP machines. I tried putting in a delay (a counting loop) of up to 7 seconds between the Rng1.text and the ActiveDocument.PrintOut, but that did not work either.

      I can put the Rng1.text and ActiveDocument.PrintOut into a separate function and trap for the error, and just re-execute the function til it DOES execute without error.

      Any other suggestions or ideas?

    • #1357190

      The error may be due to a fault in your Office or print driver installation on the Win 7 machines.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1357197

      Tried reinstalling Adobe Acrobat 9 – no dice. Tried PDF Creator from SourceForge instead of Adobe, but got the same error:

      32377-MacroCrash

    • #1357250

      Try inserting a ‘DoEvents’ line after your ‘ActiveDocument.PrintOut’ line. That should allow Word to do whatever housekeeping it needs as each loop is processed.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1357414

      That worked! We are good to go. Thanks!

    Viewing 7 reply threads
    Reply To: Logbook pagination: There has got to be a better way…

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: