导出为 PDF 时删除按钮

Posted

技术标签:

【中文标题】导出为 PDF 时删除按钮【英文标题】:removing Buttons when exporting to PDF 【发布时间】:2021-09-15 13:30:55 【问题描述】:

我在导出为 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

【问题讨论】:

【参考方案1】:

好的,我找到了一个使用一些技巧的解决方案。所以我添加了 4 个矩形形状,我只想让 3 个消失,同时保存为 PDF。当我将其中的 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

【讨论】:

以上是关于导出为 PDF 时删除按钮的主要内容,如果未能解决你的问题,请参考以下文章

使用没有 PDF 导航按钮的 rmarkdown 渲染导出为 PDF

如果数据表中没有可用数据,如何删除搜索和导出按钮

为啥 DataTable TableTools 导出为 pdf 或 excel 或 csv 按钮不可见,只有打印按钮可见?

如何使用jquery数据表自定义按钮导出水晶报告pdf

jquery datatable如何在导出到excel pdf时从标题中删除下拉过滤器选择值

Python使用matplotlib可视化绘制并通过Tkinter生成按钮将可视化结果导出为pdf文件