导出为 PDF 时删除按钮

removing Buttons when exporting to PDF

我试图在导出为 PDF 时删除或隐藏 4 个命令按钮。我这样做的方式第一次导出时有效,但如果我再次导出同一个文件,我会收到一条错误消息,指的是这一行:Me.CommandButton3.Select,即使所有按钮都回来了。有没有更好的隐藏方法而不必删除它们?我没有按照互联网上的建议在属性下设置 printobject。我应该补充一点,我还有其他宏,比如 publish_date 我不想删除。

另外,我意识到那些命令按钮没有保存为形状,所以试图隐藏形状也没有用。但是,如何将它们链接到形状并在保存为 PDF 时使其他形状进入前景和背景呢?那行得通吗?!

另一个想法可能是这样的:

Dim s As Shape
For Each s In ActiveDocument.Shapes
If s.Type = msoFormControl Then
If s.Type = wdButtonControl Then
    s.Delete
End If
End If
Next s

它不起作用,我需要在导出后恢复按钮。在我的代码下方,删除了按钮,将它们取回,但也出现了命令按钮 3 不再工作的错误,它将文件保存为 .docm:

Private Sub CommandButton1_Click()
Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Finale Versionen\"
Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
Dim Title As String: Title = "Besprechungsnotizen"
Dim newTitle As String
Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
Dim User As String
Dim Version As String

If Split(ActiveDocument.Name, ".")(0) = OrigFileName Then
    'file has not been resaved
Else
    'file has been saved before so extract data from filename
    Dim nameElements As Variant
    nameElements = Split(Split(ActiveDocument.Name, ".")(0), "_")
    User = nameElements(UBound(nameElements))
    Version = nameElements(UBound(nameElements) - 1)
    Title = nameElements(UBound(nameElements) - 3)
End If
If User = "" Then
    User = InputBox("Wer erstellt? (Name in Firmenkurzform)")
    newTitle = MsgBox("Anderer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
        If newTitle = vbYes Then
            Title = InputBox("Wie soll der Titel sein?")
        Else
        End If
    Version = "0"
Else
    newVersion = MsgBox("Neue Version?", vbQuestion + vbYesNo + vbDefaultButton2, "Neue Version")
        If newVersion = vbYes Then
        Dim currentUser As String
        currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
            If currentUser = User Then
            Else
                User = User & currentUser
            End If
        Version = Format$(Version + 1)
        Else
        Version = Format$(Version)
        End If
End If

Me.CommandButton1.Select
Selection.Delete
Me.CommandButton2.Select
Selection.Delete
Me.CommandButton3.Select
Selection.Delete
Me.Refresh.Select
Selection.Delete


ActiveDocument.ExportAsFixedFormat OutputFileName:=FilePath & _
                                MyDate & "_" & Title & "_i_0" & Version & "_" & User & ".pdf", _
                                ExportFormat:=wdExportFormatPDF, _
                                OpenAfterExport:=False, _
                                OptimizeFor:=wdExportOptimizeForPrint, _
                                Range:=wdExportAllDocument, _
                                IncludeDocProps:=True, _
                                CreateBookmarks:=wdExportCreateWordBookmarks, _
                                BitmapMissingFonts:=True
ActiveDocument.Undo
ActiveDocument.Undo
ActiveDocument.Undo
ActiveDocument.Undo
ActiveDocument.Undo
 End Sub

好的,我找到了一个可以使用一些技巧的解决方案。所以我添加了 4 个矩形形状,我只想在保存为 PDF 时让 3 个消失。当我将其中 3 个形状包裹在文本(或按钮)后面并添加

时,它会起作用
With ActiveDocument
.Shapes(1).WrapFormat.Type = wdWrapFront

ActiveDocument.ExportAsFixedFormat OutputFileName:=FilePath & ...
...
.Shapes(1).WrapFormat.Type = wdWrapBehind
End With

它确实以某种方式将这 3 个放在文本前面,并且只将这三个再次放在文本后面,而将第 4 个矩形永久保留在另一个文本前面。就像我想要的那样。在我的整个代码下面:

Private Sub CommandButton1_Click()
Const FilePath As String = "//SRVDC\Arbeitsordner\Intern\Meetings\Finale 
Versionen\"
Const OrigFileName As String = "20210910_Besprechungsnotizen_00_"
Dim Title As String: Title = "Besprechungsnotizen"
Dim newTitle As String
Dim MyDate As String: MyDate = Format(Date, "YYYYMMDD")
Dim User As String
Dim Version As String

If Split(ActiveDocument.Name, ".")(0) = OrigFileName Then
    'file has not been resaved
Else
    'file has been saved before so extract data from filename
    Dim nameElements As Variant
    nameElements = Split(Split(ActiveDocument.Name, ".")(0), "_")
    User = nameElements(UBound(nameElements))
    Version = nameElements(UBound(nameElements) - 1)
    Title = nameElements(UBound(nameElements) - 3)
End If
If User = "" Then
    User = InputBox("Wer erstellt? (Name in Firmenkurzform)")
    newTitle = MsgBox("Anderer Titel?", vbQuestion + vbYesNo + vbDefaultButton2, "Titel")
        If newTitle = vbYes Then
            Title = InputBox("Wie soll der Titel sein?")
        Else
        End If
    Version = "0"
Else
    newVersion = MsgBox("Neue Version?", vbQuestion + vbYesNo + vbDefaultButton2, "Neue Version")
        If newVersion = vbYes Then
        Dim currentUser As String
        currentUser = InputBox("Wer bearbeitet? (Name in Firmenkurzform)")
            If currentUser = User Then
            Else
                User = User & currentUser
            End If
        Version = Format$(Version + 1)
        Else
        Version = Format$(Version)
        End If
End If
With ActiveDocument
.Shapes(1).WrapFormat.Type = wdWrapFront
ActiveDocument.ExportAsFixedFormat OutputFileName:=FilePath & _
                                MyDate & "_" & Title & "_i_0" & Version & "_" & User & ".pdf", _
                                ExportFormat:=wdExportFormatPDF, _
                                OpenAfterExport:=False, _
                                OptimizeFor:=wdExportOptimizeForPrint, _
                                Range:=wdExportAllDocument, _
                                IncludeDocProps:=True, _
                                CreateBookmarks:=wdExportCreateWordBookmarks, _
                                BitmapMissingFonts:=True
                                
.Shapes(1).WrapFormat.Type = wdWrapBehind
End With

End Sub