commandbuttonsave-as

removing Buttons when exporting to PDF


I am trying to remove or hide the 4 commandbuttons when exporting to PDF. The way I do it works for the first time exporting but if I export the same file again, I'll get an error referring to this line: Me.CommandButton3.Select even though all buttons are back. Is there a better way of hiding without having to delete them? I dont have printobject under properties as suggested on the internet. I should add, I still have other macros like a publish_date I do not wanna remove.

Also, I realized those commandbuttons arent saved as shapes, so trying to hide shapes didnt work either. But what about linking them to shapes and making the other shapes go into the fore and background when saving to PDF? would that work?!

Another idea was perhaps something like this:

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

It isnt working and I need the buttons back after exporting. Below my code with deleting the buttons, getting them back, but also getting an error with commandbutton 3 not working anymore, which is saving the file as .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

Solution

  • Ok I found a solution that works with a few tricks. So I added 4 rectangle shapes and I only wanna make 3 disappear while saving as PDF. It works when I wrap 3 of those shapes behind the text (or buttons) and add

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

    Somehow it does put those 3 in front of the text and only puts those three behind the text again and leaving the 4th rectangle permanent in front of another text. Just as I want it. Below my entire code:

    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