【问题标题】:How to hide an ActiveX Command Button when printing a word document?打印word文档时如何隐藏ActiveX命令按钮?
【发布时间】:2021-08-07 16:15:22
【问题描述】:

我有一个 ActiveX 命令按钮,按下它会打开一个用户窗体以允许将数据输入到 word 文档中。此按钮需要在处理文档时保持可见,但在打印时不可见。

如何仅在打印时隐藏/使其不可见?

与 Excel VBA 中的属性包含“PrintObject”选项不同,word VBA 没有此功能。我能做的最好的事情是在被点击后删除按钮,但这并不是我真正想要的。

'Needs to hide button only on printing, not delete it

UserForm2.Show
CommandButton1.Select
Selection.Delete

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    我假设您在 word 中有 ActiveX Command Button 并使用用户表单输入的数据在相应的字段中获取馈送,并且您正在关闭用户表单然后尝试打印文档并且打印的文件中不应包含 ActiveX Command Button

    将以下代码粘贴到CommandButton_Click event

    Private Sub CommandButton1_Click() 
    
     With ActiveDocument 
        .Shapes(1).Visible = msoFalse 
        .PrintOut Background:=False 
        .Shapes(1).Visible = msoTrue 
     End With 
    
    End Sub
    

    【讨论】:

    • 这是正确的,不幸的是,当我运行此代码时,我收到以下运行时错误:“指定集合的​​索引超出范围。”引用此代码的第 3 行和第 5 行。你知道我做错了什么吗?
    • @Jakec arun 不得不猜测如何处理文档表面上的命令按钮。代码中的假设是它是文档中第一个应用了文本换行格式的对象。您需要调整代码以解决正确的对象。由于没有文件,我们不知道如何识别它......
    • @CindyMeister 命令按钮是文档中的第一个也是唯一的对象。我试图指定对象,但我继续收到相同的错误或错误“未找到具有指定名称的项目”。 ActiveDocument .Shapes("CommandButton1").Visible = msoTrue
    • @JakeC 按钮是用文本换行格式化的,还是与文本内联的。如果是后者,它将是InlineShapes 集合的成员,所以试试InlineShapes(1),而不是?啊,但在那种情况下 .Visible 不起作用,它必须有 .Range.Font.Hidden = True 才能隐藏它。
    • 谢谢@CindyMeister!使用以下代码让它工作:InlineShapes(1).Range.Font.Hidden = TrueActiveDocument.PrintOut Background:=False
    【解决方案2】:

    @ille P. - 你应该发布一个新问题,也许带有指向这个问题的链接。

    试试 Shapes 集合和 inlineshape 集合。

    Ibby 在Microsoft Word MVP VBA FAQ pages 上提出以下建议:

    Private Sub CommandButton1_Click()
        With ActiveDocument
            .Shapes(1).Visible = msoFalse
            .PrintOut Background:=False
            .Shapes(1).Visible = msoTrue
        End With
    End Sub
    

    所以翻译成集合:

    Dim oShape as Shape
    For Each oShape in ActiveDocument.Shapes
        oShape.Visible = False
    Next oShape
    

    这会隐藏所有形状,而不仅仅是按钮。 您可以为按钮添加书签并将其用作过滤范围。

    【讨论】:

      【解决方案3】:

      在调查了 CommandButtons 发生的情况后,我检测到它们在文档中被封装在 InLineShape 对象中(如果它没有被编辑为 Shape),它们被包含在 InLineShape 集合中。

      如果我们可以直接从 CommandButton 类型转换为 InShapeLine 那就太好了,但我认为微软不允许这样做,太糟糕了。

      这个想法是创建两个类模块:

      • 一个是事件管理器,负责捕捉“打印前”事件,另一个是。
      • 另一个是 InLineShape 对象的封装,该对象具有使对象可见的方法,利用 PictureFormat 对象的 Brightness 属性。

      CLASS clsButton

      Option Explicit
      
      Private M_ishpButton As InlineShape
      
      Public Property Get button() As InlineShape
          Set button = M_ishpButton
      End Property
      
      Public Property Set button(oObj As InlineShape)
          Set M_ishpButton = oObj
      End Property
      
      Public Property Get Visible() As Boolean
          Visible = Not bIsHidden
      End Property
      
      Public Property Let Visible(bValue As Boolean)
          Dim oPictureFormat As PictureFormat
          
          Set oPictureFormat = M_ishpButton.PictureFormat
      
          If bValue Then
              Call show
          Else
              Call hide
          End If
      End Property
      
      Private Function bIsHidden() As Boolean
          Dim oPictureFormat As PictureFormat
      
          Set oPictureFormat = M_ishpButton.PictureFormat
          
          If oPictureFormat.Brightness = 1 Then bIsHidden = True: Exit Function
          
          bIsHidden = False
      
      End Function
      
      Private Sub hide()
          Dim oPictureFormat As PictureFormat
          
          Set oPictureFormat = M_ishpButton.PictureFormat
          
          oPictureFormat.Brightness = 1
      End Sub
      
      Private Sub show()
          Dim oPictureFormat As PictureFormat
          
          Set oPictureFormat = M_ishpButton.PictureFormat
          
          With oPictureFormat
              .Brightness = 0.5
              .Contrast = 0.5
          End With
      End Sub
      

      CLASS clsEvents

      Option Explicit
      
      Public WithEvents appWord As Word.Application
      Public WithEvents docWord As Word.Document
      Private m_button As New clsButton
      
      
      Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
         
          'If process is not cancel and button is not visible
          With m_button 
              If Cancel = False And .Visible = True Then
                  .Visible = False
              End If
          End With
      End Sub
      
      Private Sub appWord_WindowDeactivate(ByVal Doc As Document, ByVal Wn As Window)
          
          'If button is not visible then set to true
          With m_button 
              If .Visible = False Then
                  .Visible = True
              End If
          End With
      End Sub
      
      Public Property Set button(oObj As clsButton)
          Set m_button = oObj 
      End Property
      

      分类此文档 现在,在 ThisDocument 类中应该创建对象和链接

      Dim oEventsManager As New clsEvents
      Dim oEditedButton As New clsButton
      
      Const BUTTON_LINKS As String = "cmdUpdateLinks" 'For example
      
      Dim oInShpDoc As Word.InlineShape, oOleDoc As Word.OLEFormat, oInShapesDoc As Word.InlineShapes
      
      Public Sub Set_Manager_Events()
          Set oEventsManager.appWord = Word.Application 'ThisDocument.Application
          Set oEventsManager.docWord = ThisDocument
          
          Set oInShpDoc = FNC_oGet_Button_Variable(BUTTON_LINKS)
      
      
          If Not oInShpDoc Is Nothing Then
              Set oEditedButton.button = oInShpDoc
              Set oEventsManager.button = oEditedButton
          End If
      End Sub
      
      '###### EVENTOS OF BUTTON
      Private Sub cmdUpdateLinks_Click()
      
          If oEventsManager.appWord Is Nothing Then Call Set_Manager_Events
          
          Call UpdateLinks ' Is one example
      End Sub
      
      
      
      Public Function FNC_oGet_Button_Variable(sCodeName As String) As InlineShape
      
          Dim oForm As InlineShape, oFormsInLine As InlineShapes, oOLEFormat As OLEFormat
      
          Set oFormsInLine = ThisDocument.InlineShapes
      
          If oFormsInLine .Count < 1 Then GoTo bye
      
          i = 0
          For Each oForm In oFormsInLine 
      
              With oForm 
                  Set oOLEFormat = .OLEFormat
              
                  If Not oOLEFormat Is Nothing Then
      
                      If InStr(1, oOLEFormat.ClassType, "CommandButton") > 0 Then
                          If .OLEFormat.Object.Name = sCodeName Then
                              Set FNC_oGet_Button_Variable= oForm 
                              Exit Function
                          End If
                      End If
                  End If
              End With
          Next
      
      bye:
          Set FNC_oGet_Button_Variable = Nothing
      End Function
      

      这样你就可以隐藏打印按钮了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2011-04-08
        • 2016-09-04
        • 1970-01-01
        • 2017-06-04
        相关资源
        最近更新 更多