打印Word文档时如何隐藏ActiveX命令按钮?



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

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

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

'Needs to hide button only on printing, not delete it
UserForm2.Show
CommandButton1.Select
Selection.Delete

我假设您在单词中有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

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

尝试"形状"集合以及"内联形状"集合。

以下是 Ibby 在 Microsoft Word MVP VBA FAQ FAQ 页面上的建议:

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

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

在调查了 CommandButton 发生的情况后,我检测到它们在文档中被封装在 InLineShape 对象中(如果尚未将其编辑为形状(包含在 InLineShape 集合中。

如果我们可以直接从 CommandButton 键入到 InShapeLine 会很棒,但我认为Microsoft不允许这样做,太糟糕了。

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

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

类 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

类 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

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

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

有了这个,您可以隐藏按钮进行打印。

最新更新