在打印期间隐藏Word VBA中的按钮

问题描述 投票:1回答:2

我有一些用于Microsoft Word的VBA代码,我正在努力缩小(隐藏)一个命令按钮。单击按钮后需要缩小,然后出现打印对话框,然后打印后,按钮需要返回到正常大小。我有前两个部分工作正常,但打印后按钮返回正确的尺寸不起作用。我正在两次调用iShp导致错误。有关如何解决这个问题的任何想法?

谢谢!

Dim iShp As Word.InlineShape
For Each iShp In ActiveDocument.InlineShapes
    If iShp.OLEFormat.Object.Name = "cmdSaveCert" Then
        iShp.Width = 1
        iShp.Height = 1
    End If
Next

With ActiveDocument
Dialogs(wdDialogFilePrint).Show   'allows user to select Printer
End With

Dim iShp As Word.InlineShape
For Each iShp In ActiveDocument.InlineShapes
If iShp.OLEFormat.Object.Name = "cmdSaveCert" Then
        iShp.Width = 28185
        iShp.Height = 13710
    End If
ms-word word-vba
2个回答
0
投票

谢谢你的提示!结合你们两个人的建议让我的代码正常运行。我不得不在最后一个'End If'语句下面抛出'Next'以使一切正常。


0
投票

这对我有用:

Option Explicit

Private Type SizeCache
    width As Integer
    height As Integer
End Type

Private Sub ShowPrintDialog(Optional ByVal doc As Document)
    Dim wasProtected As Boolean
    Dim currentShape As InlineShape
    Dim dimensions() As SizeCache, size As Long, entry As SizeCache
    ' Prepare Document
    If doc Is Nothing Then
        Set doc = ThisDocument
    End If
    If doc.ProtectionType = wdAllowOnlyFormFields Then
        wasProtected = True
        doc.Unprotect
    End If
    ' Hide all buttons
    For Each currentShape In doc.InlineShapes
        If Not currentShape.OLEFormat Is Nothing Then
            entry.width = currentShape.width
            entry.height = currentShape.height
            size = size + 1
            ReDim Preserve dimensions(size)
            dimensions(size) = entry
            With dimensions(UBound(dimensions))
                Debug.Print currentShape.OLEFormat.Object.Name & ": " & .width & "x" & .height
            End With
            currentShape.width = 1
            currentShape.height = 1
        End If
    Next
    '
    ' At last, allows user to select Printer and print
    '
    Dialogs(wdDialogFilePrint).Show
    ' Restore all buttons
    Dim i As Long: i = 1
    For Each currentShape In doc.InlineShapes
        If Not currentShape.OLEFormat Is Nothing Then
            With dimensions(i)
                currentShape.width = .width
                currentShape.height = .height
            End With
            i = i + 1
        End If
    Next
    ' Restore Document
    If wasProtected Then
        doc.Protect wdAllowOnlyFormFields, True
    End If
End Sub

Public Sub CommandButton1_Click()
    Call ShowPrintDialog
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.