0

I have a Word document with a series of of command buttons that toggle their corresponding rows of text between hidden and unhidden. I would like for the buttons themselves to be hidden when I print the document to a pdf. For some reason, some of the buttons show up in print and others do not. I cannot figure out what is causing the difference. Does anyone have any tips on what to look at, or advice about the best way to ensure that command buttons are hidden when printing?

Also, for extra credit: The command buttons hide text in a table. Is there a way to hide the row in the table hidden as well so that I don't have any blank cells when hiding text?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

2 Answers2

0

DocumentBeforePrint event

in the app class module

Option Explicit

Public WithEvents appWord As Word.Application
 
Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    Hide_Command_Button_in_Word_When_Printing
End Sub

enter image description here

Hide all command buttons - Mudule procedure

A control that you add to the text layer is an InlineShape object, to which you gain access programmatically through the InlineShapes collection. A control that you add to the drawing layer is a Shape object, to which you gain access programmatically through the Shapes collection. https://learn.microsoft.com/en-us/office/vba/word/concepts/objects-properties-methods/using-activex-controls-on-word-documents

Sub Hide_Command_Button_in_Word_When_Printing()
    Dim cmdBtn As InlineShape
    Dim d As Document, ur As UndoRecord
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord "Hide_Command_Button_in_Word_When_Printing"
    Set d = ActiveDocument
    
    
    For Each cmdBtn In ActiveDocument.InlineShapes
        If cmdBtn.Type = wdInlineShapeOLEControlObject Then
            'If InStr(cmdBtn.OLEFormat.Object.Name, "CommandButton") = 1 Then
            If InStr(cmdBtn.OLEFormat.ClassType, "Forms.CommandButton.") = 1 Then
                cmdBtn.Range.Font.Hidden = True
                
                Rem There are still command buttons that are not hidden because they forgot to hide the blank paragraph where they are located
                If Len(cmdBtn.Range.Paragraphs(1).Range) <= 2 Then
                    cmdBtn.Range.Paragraphs(1).Range.Font.Hidden = True
                End If

                
            End If
        End If
    Next cmdBtn
    ur.EndCustomRecord
End Sub

enter image description here

Hide the row of the table

in the ThisDocument object

Private Sub CommandButton1_Click()
    Dim tb As Word.Table, d As Document, rw As Word.Row, ur As UndoRecord
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord "CommandButton1_Click"
    Set d = ActiveDocument
    Rem What table you want to
    Set tb = d.Tables(1)
    On Error GoTo 0
    Rem What row of the table you want to hide, but there must be no merge cells in the table
    tb.rows(1).Range.Font.Hidden = True 'hide the row
    ur.EndCustomRecord
End Sub

enter image description here

Oscar Sun
  • 1,427
  • 2
  • 8
  • 13
0

Command bars were deprecated. Instead, you need to use the Fluent UI (aka Ribbon UI) for customizing user interface in Office applications including MS Word. The Fluent UI allows declaring callbacks where you can manage the state of controls - disable/enable, hide/show and etc. For example, the getEnabled callback gets the enabled state of a control:

C#: bool GetEnabled(IRibbonControl control)

VBA: Sub GetEnabled(control As IRibbonControl, ByRef enabled)

C++: HRESULT GetEnabled([in] IRibbonControl *pControl, [out, retval] VARIANT_BOOL *pvarfEnabled)

Visual Basic: Function GetEnabled(control as IRibbonControl) As Boolean

If an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

Read more about the Fluent UI (aka Ribbon UI) in the following series of articles:

Finally, consider replacing command buttons with Fluent UI controls.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • The question is about command buttons, not command bars. Are you wrong or I misunderstood it? – Oscar Sun Jun 16 '23 at 16:29
  • Have you read the title of the post? The OP wrote - `I would like for the buttons themselves to be hidden when I print the document to a pdf.` – Eugene Astafiev Jun 16 '23 at 16:38
  • So *I have **a Word document with** a series of of **command buttons** that toggle their corresponding rows of text between hidden and unhidden.*, Is what in the document? – Oscar Sun Jun 16 '23 at 17:06
  • And OP said then: *For some reason, some of the buttons show up in print and others do not. I cannot figure out what is causing the difference.*, so I reply *Rem There are still command buttons that are not hidden because they forgot to hide the blank paragraph where they are located*, after my practical testing. – Oscar Sun Jun 16 '23 at 17:13