• Run macro when print selected

    Author
    Topic
    #471673

    MSO 2003 xpp sp3

    Hi

    I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.

    Viewing 2 reply threads
    Author
    Replies
    • #1244426

      I am wanting to have a macro run in Word to create a header and insert specific text when File/Print is selected.

      You can intercept printing by naming a macro FilePrint. There are some fine points to it, but generally speaking, you can display the dialog, capture the user’s instructions, update the document, then execute the user’s instructions. (You may also need to intercept the FilePrintDefault toolbar button — printing that bypasses the Print dialog).

      Regarding the changes you want to make, it’s always a little tricky not ruining the existing contents of a header or footer when editing them in code. Or did you mean you want to insert some content at the beginning of the “body” of the document? Can you describe your requirements in a little more detail? You could, for example, upload “before and after” documents showing what the code should do.

    • #1244433

      Jefferson hi

      I have managed to find some code to do what I want. WE are not worried about other content in the header as there should not be any.

      FYI the code I have used

      In the project explorer “ThisDocument” I have put the code below into the code window.

      Code:
      Private Sub Document_Open()
      
      	Call Register_Event_Handler
      
      End Sub

      Inserted a new blank module and inserted the code below

      Code:
      Dim X As New Class1
      Public Sub Register_Event_Handler()
          Set X.App = Word.Application
      End Sub
      
      Inserted a Class Module and inserted the code below.
      
      Public WithEvents App As Word.Application
      
      Private Sub App_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
      
          If ActiveWindow.View.SplitSpecial  wdPaneNone Then
              ActiveWindow.Panes(2).Close
          End If
          If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
              ActivePane.View.Type = wdOutlineView Then
              ActiveWindow.ActivePane.View.Type = wdPrintView
          End If
          ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
          Selection.Font.Size = 8
          Selection.TypeText Text:= _
              "Document uncontrolled when printed: Printed on: "
          Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
              "PRINTDATE  @ ""dddd, d MMMM yyyy"" ", PreserveFormatting:=True
          Selection.TypeText Text:=", "
          Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
              "PRINTDATE  @ ""h:mm am/pm"" ", PreserveFormatting:=True
          ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
          Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
              wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
              ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
              False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
              PrintZoomPaperHeight:=0
      
      End Sub

      For some reason it prints 2 copies of the test document!

      Thanks for replying though

      • #1244449

        This is definitely not the simplest approach, but it might be best in your case.

        For some reason it prints 2 copies of the test document!

        Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user’s instructions in the Print dialog.

        I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.

        What happens on the second print? Does the header get replaced or does the content get doubled?

        • #1244464

          This is definitely not the simplest approach, but it might be best in your case.

          Your code contains this method: Application.PrintOut. However, you are not settings Cancel = True, so Word goes ahead and prints following the user’s instructions in the Print dialog.

          I think the best solution is to comment out Application.PrintOut and just let Word print normally, especially if the user might be printing only a few pages of the document or even a selection.

          What happens on the second print? Does the header get replaced or does the content get doubled?

          Jefferson you’re right I commentdout that line of code. It just printed 2 copies

    • #1244460

      hi Bonriki,

      Why don’t you simply add the necessary text & field coding to the document (and it’s template, so that future documents get created with it)?

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1244465

        hi Bonriki,

        Why don’t you simply add the necessary text & field coding to the document (and it’s template, so that future documents get created with it)?

        Paul hi

        Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.

        • #1244467

          Paul hi

          Mostly the document will be viewed on the internet, go here, Promapp, and hover over the Definitions folder and click the link. This shows the document but we need to show that it will be uncontrolled if printed.

          Hi Phil,

          That’s OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
          {IF{PRINTDATE @ yyyyMMddHHmm}= {DATE @ yyyyMMddHHmm} “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
          or:
          {IF{PRINTDATE @ yyyy}> 0 “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
          The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the ‘Document uncontrolled when printed.’ outside the field code, so that it always appears in the header.

          Cheers,
          Paul Edstein
          [Fmr MS MVP - Word]

          • #1244472

            Hi Phil,

            That’s OK as far as it goes, but why not incorporate the info in the standard footer, where the alert can be seen before the document is printed? Alternatively, if you only want the message to appear on printed versions, use either of the following field codes in the header:
            {IF{PRINTDATE @ yyyyMMddHHmm}= {DATE @ yyyyMMddHHmm} “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
            or:
            {IF{PRINTDATE @ yyyy}> 0 “Document uncontrolled when printed. Printed on: {PRINTDATE @ “DDDD, D MMMM YYYY, hh:mm am/pm”}”}
            The first version generates a field that displays on the electronic file for up to a minute after printing, after which a field update will hide it again; the second version leaves a permanent message on the electronic file. In either case, it seems to me that it would be preferable to move the ‘Document uncontrolled when printed.’ outside the field code, so that it always appears in the header.

            Hi again Paul

            I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.

            If the documents require amending they will need to be uploaded again

            • #1244476

              Hi again Paul

              I think it is OK as it is. The nature of the software is that it will not save altered documents back to the library where they were extracted from.

              If the documents require amending they will need to be uploaded again

              Hi Phil,

              But doesn’t the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents – with field coding, there’s nothing that relies on the user’s response.

              Cheers,
              Paul Edstein
              [Fmr MS MVP - Word]

            • #1244590

              Hi Phil,

              But doesn’t the same risk exist if the user simply downloads the document from the library (eg via File|Save As)? If they then work from their saved copy, regardless of whether the document gets printed, surely the same risk exists? Also, for the macro solution to work, you need to persuade the user to enable macros for these documents – with field coding, there’s nothing that relies on the user’s response.

              Paul hi

              Your right there is risk that users may use File|Save As but we seem to have convinced our users not to do that. All the users computers have a group template, similar to normal.dot, that is sets the parameters for Word to run under including setting the secuurity level to Medium. We have many macro driven forms letters etc so they are all conditioned to hit Enable Macros.

    Viewing 2 reply threads
    Reply To: Run macro when print selected

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

    Your information: