Consulting

Results 1 to 2 of 2

Thread: Runnig a macro before printing

  1. #1

    Runnig a macro before printing

    I have a macro in word 2010, that adds a footer to documents when you print them (i.e. after you click the "final" print button, in the print preview screen).

    Currently, in order to foot the document, the user needs to run the macro first, and only after running it, when they print the document, the footer is added.


    I would like to automate the part of running the macro, so that selecting a printing option (Ctrl+P / File>Print) would run the macro automatically and open the print preview screen for the final printing.


    How can this be done?


    Thank you in advance

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    7,329
    Location
    https://msdn.microsoft.com/en-us/vba...int-event-word


    1. In a class module called clsWord

    Option Explicit
    
    Public WithEvents appWord As Word.Application
    
    Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
     Dim ans As Long
     ans = MsgBox("Do you REALLY want to print this?", vbYesNo)
     Cancel = (ans = vbNo)
    End Sub
    2. In a standard module

    Option Explicit
    
    Public msWord As clsWord

    3. In ThisDocument


    Option Explicit
    
    Private Sub Document_New()
        Set msWord = New ClsWord
        Set msWord.appWord = Word.Application
    
    End Sub
    
    Private Sub Document_Open()
        Set msWord = New ClsWord
        Set msWord.appWord = Word.Application
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 02-23-2018 at 07:29 AM. Reason: Forgot first Set
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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