none
Problem to override built in command in Word 2010 RRS feed

  • Question

  • In Word 2010 I want to replace the built in Macro described here:
    - Use the File | Print menu.
    - Click the Print button
    Using the CTRL-ALT and + (+ button used from numeric keypad) Word tells me that BestandAfdrukken (FilePrint) in English) is the default macro.

    By using the code

    Sub BestandAfdrukken()  
       Msgbox ("Bla bla")
    End sub

    I would expect this to show the” Bla bla” message after clicking the button. However nothing happens.
    Using a similar solution with FileSave works fine.
    Could you help me finding a solution?

    BTW: I have seen Dutch and English command names in my Dutch Word version. Can you use both languages at the same time?

    kind regards
    Reyer

    Sunday, April 15, 2012 7:21 PM

Answers

  • I do not know about 2010, but in previous versions the Print button and File>Print are DIFFERENT procedures.

    Sub FilePrint() is fired by File>Print

    Sub FilePrintDefault() is fired by clicking the Print button.

    They are DIFFERENT.  So if you override FilePrint - with a Sub FilePrint() - clicking the Print button is NOT affected.  To change the effect of clicking the Print button you must re-write FilePrintDefault.


    • Proposed as answer by fumei2 Sunday, April 22, 2012 12:46 AM
    • Edited by fumei2 Sunday, April 22, 2012 12:53 AM
    • Marked as answer by Bruce Song Monday, April 23, 2012 7:21 AM
    Sunday, April 22, 2012 12:45 AM

All replies

  • As far as I know, you should always use the English version of the command, regardless of the interface language, so FilePrint instead of BestandAfdrukken, and InsertPageBreak instead of InvoegenPaginaEinde.

    Regards, Hans Vogelaar

    Sunday, April 15, 2012 7:33 PM
  • I tried:

    Sub PrintPreviewAndPrint()
     MsgBox ("Print preview and print")
    End Sub

    and

    Sub BestandAfdrStandaard()
     MsgBox ("BestandAfdrukStandaard")
    End Sub

    They both worked in these different languages without a problem.

    In Word (in the Macro's dialog) one can see all built in Commands.

    You can see here and in the real dialog that it is a mix of Dutch and English.

    This dialog tells me that BestandAfdrukken is a valid Command but it doesn't work. Neither does FilePrint.

    Any idea?

    regards
    Reyer

    Wednesday, April 18, 2012 2:38 PM
  • Apparently the ability to intercept the Print command by creating a macro named FilePrint has been disabled by the introduction of the Backplane in Word 2010. It may be possible to use a combination of XML and VBA to repurpose the File > Print ribbon command, but that's a lot more complicated. (See http://msgroups.net/microsoft.public.word.vba.general/Intercept-Print-Word-2010)

    Regards, Hans Vogelaar

    Wednesday, April 18, 2012 4:33 PM
  • I do not know about 2010, but in previous versions the Print button and File>Print are DIFFERENT procedures.

    Sub FilePrint() is fired by File>Print

    Sub FilePrintDefault() is fired by clicking the Print button.

    They are DIFFERENT.  So if you override FilePrint - with a Sub FilePrint() - clicking the Print button is NOT affected.  To change the effect of clicking the Print button you must re-write FilePrintDefault.


    • Proposed as answer by fumei2 Sunday, April 22, 2012 12:46 AM
    • Edited by fumei2 Sunday, April 22, 2012 12:53 AM
    • Marked as answer by Bruce Song Monday, April 23, 2012 7:21 AM
    Sunday, April 22, 2012 12:45 AM
  • The best way I've found to intercept (well psuedo intercept) the print command in Word 2010 is to use the DocumentBeforePrint event.

    Add a standard module (mine is named Main) and add the following code:

    Option Explicit
    Private myDemoClass As DemoClass
    Public bQuickPrint As Boolean
    Sub AutoOpen()
    Set myDemoClass = New DemoClass
    End Sub
    Function GreenMsg() As String
    GreenMsg = "The right or left margin in this document exceeds the allowable limit." & vbCr + vbCr _
           & "Please set the right and left margins to one inch or less and try again."
    End Function
    Function InValidMargins() As Boolean
    InValidMargins = False
    If ActiveDocument.PageSetup.RightMargin > 72 Or ActiveDocument.PageSetup.LeftMargin > 72 Then
      InValidMargins = True
    End If
    End Function
    Sub FilePrintDefault()
    bQuickPrint = True
    ActiveDocument.PrintOut Background:=True
    bQuickPrint = False
    End Sub

    Add a class module (mine is named DemoClass) and add the following code:

    Option Explicit
    Private WithEvents oWordApp As Word.Application
    Private Sub Class_Initialize()
    Set oWordApp = Word.Application
    End Sub
    Private Sub oWordApp_DocumentBeforePrint(ByVal oDoc As Document, Cancel As Boolean)
      If Main.InValidMargins Then
        Cancel = True
        MsgBox Main.GreenMsg
        If Not bQuickPrint Then CommandBars.ExecuteMso ("PrintPreviewAndPrint")
      End If
    End Sub

     


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Monday, April 23, 2012 7:19 PM