Hello John,
I just learned that somehow, during the ExportAsFixedFormat method execution, the task name column gets at least 30% wider than the configuration in the current view. I reduced the width of the column by about 25%, the longest text was not completely visual, but after executing the ExportAsFixedFormat method, the result PDF was perfectly formatted with all the text completely visual and with reasonable space to the next column.
I don't if this is a bug, it doesn't seem that the font format and size were changed, but now it works. Below is the final code for exporting from an event command.
' Main function to export the "_Contract View" to PDF using ExportAsFixedFormat
' Hauptfunktion zum Exportieren der "_Contract View" als PDF mit ExportAsFixedFormat
Public Sub PrintContractViewToPDF()
Dim pdfPath As String
Dim suggestedFileName As String
' Suggested file name with the current date
' Vorgeschlagener Dateiname mit aktuellem Datum
suggestedFileName = "Codeword\_ContractSection\_" & Format(Date, "yyyy-mm-dd") & ".pdf"
' Open dialog box for user to choose file location and name
' Öffnet das Dialogfeld, damit der Benutzer den Speicherort und den Dateinamen auswählen kann
pdfPath = GetSaveAsFile("Save as PDF", suggestedFileName)
' Check if a path was selected
' Überprüft, ob ein Pfad ausgewählt wurde
If pdfPath <> "" Then
' Select the "\_Contract View" view
' Wählt die Ansicht "\_Contract View" aus
On Error Resume Next
Application.ActiveProject.Views("\_Contract View").Apply
On Error GoTo 0
' Set up the page for one-page width and as many pages as necessary for height
' Konfiguriert die Seite für eine Seitenbreite und so viele Seiten wie nötig für die Höhe
Application.FilePageSetupPage \_
Name:="\_Contract View", \_
Portrait:=True, \_
PercentScale:=85, \_
PaperSize:=pjPaperA3
' Set up the margins for printing
' Konfiguriert die Ränder für den Druck
Application.FilePageSetupMargins \_
Name:="\_Contract View", \_
Top:=0.98, \_
Bottom:=0.98, \_
Left:=0.79, \_
Right:=0.5, \_
Borders:=pjAroundEveryPage
' Display the Print Preview to check the print layout
' Zeigt die Druckvorschau an, um das Drucklayout zu überprüfen
Application.FilePrintPreview
' Export the active view as a PDF using ExportAsFixedFormat method
' Exportiert die aktive Ansicht als PDF mit der Methode ExportAsFixedFormat
On Error Resume Next
Application.ActiveProject.ExportAsFixedFormat \_
FileName:=pdfPath, \_
FileType:=pjPDF, \_
IncludeDocumentProperties:=True, \_
IncludeDocumentMarkup:=True, \_
FromDate:=Application.ActiveProject.ProjectStart, \_
ToDate:=Application.ActiveProject.ProjectFinish
On Error GoTo 0
' Success message after export
' Erfolgsmeldung nach dem Export
MsgBox "PDF successfully exported to: " & pdfPath, vbInformation
' PDF erfolgreich exportiert nach: & pdfPath
Else
MsgBox "Operation cancelled.", vbExclamation
' Vorgang abgebrochen
End If
End Sub
' Function to get the file path and name for saving
' Funktion zum Abrufen des Dateipfads und -namens zum Speichern
Private Function GetSaveAsFile(strTitle As String, suggestedFileName As String) As String
Dim SaveFile As OPENFILENAME
Dim lReturn As Long
' Set up file filter for PDFs
' Konfiguration des Dateifilters für PDFs
SaveFile.lpstrFilter = "PDF Files (\*.pdf)" & Chr(0) & "\*.pdf" & Chr(0)
SaveFile.nFilterIndex = 1
SaveFile.hwndOwner = 0
SaveFile.lpstrFile = String(257, 0) ' Buffer for file path
SaveFile.lpstrFile = suggestedFileName & Chr(0) & String(256 - Len(suggestedFileName), 0)
#If VBA7 Then
SaveFile.nMaxFile = LenB(SaveFile.lpstrFile) - 1
SaveFile.lStructSize = LenB(SaveFile)
#Else
SaveFile.nMaxFile = Len(SaveFile.lpstrFile) - 1
SaveFile.lStructSize = Len(SaveFile)
#End If
SaveFile.lpstrFileTitle = SaveFile.lpstrFile
SaveFile.nMaxFileTitle = SaveFile.nMaxFile
SaveFile.lpstrInitialDir = "C:\" ' Initial directory
SaveFile.lpstrTitle = strTitle
SaveFile.flags = 0
' Show the save file dialog
' Zeigt den Dialog zum Speichern der Datei an
lReturn = GetSaveFileName(SaveFile)
If lReturn = 0 Then
GetSaveAsFile = ""
Else
GetSaveAsFile = Trim(Left(SaveFile.lpstrFile, InStr(1, SaveFile.lpstrFile, vbNullChar) - 1))
End If
End Function