Share via

How to properly format the page in MS Project when using ExportAsFixedFormat to generate a correctly formatted PDF?

Anonymous
2024-10-15T09:10:13+00:00

Hello,

I am trying to export a view in MS Project to PDF using the ExportAsFixedFormat method. However, the generated PDF is not formatted correctly. The columns don't fit on a single page width, and the layout spills over onto additional pages, which is not the intended behavior.

We have tried using the following methods to control the page setup:

  • FilePageSetupPage: to limit the print width to a single page and scale the height as needed.
  • FilePageSetupMargins: to set the margins for the top, bottom, left, and right sides.

Unfortunately, neither of these methods seems to work as expected when exporting the project as a PDF. Below is the current version of the code that we're using:

' Main function to export the "_Contract View" as a PDF

Public Sub PrintContractViewToPDF()

Dim pdfPath As String 

Dim suggestedFileName As String 

' Suggested filename with current date 

suggestedFileName = "Codeword\_ContractSection\_" & Format(Date, "yyyy-mm-dd") & ".pdf" 

' Open the dialog for the user to select the save path and filename 

pdfPath = GetSaveAsFile("Save as PDF", suggestedFileName) 

' Check if a path was selected 

If pdfPath <> "" Then 

    ' Select the "\_Contract View" view 

    On Error Resume Next 

    Application.ActiveProject.Views("\_Contract View").Apply 

    On Error GoTo 0 

    ' Set up the page configuration for single page width and scale the height accordingly 

    Application.FilePageSetupPage \_ 

        Name:="\_Contract View", \_ 

        Portrait:=True, \_ 

        PagesWide:=1, \_ 

        PagesTall:=2 

    ' Set up the margins 

    Application.FilePageSetupMargins \_ 

        Name:="\_Contract View", \_ 

        Top:=0.98, \_ 

        Bottom:=0.98, \_ 

        Left:=0.79, \_ 

        Right:=0.79, \_ 

        Borders:=pjAroundEveryPage 

    ' Export the active view as a PDF 

    Application.ActiveProject.ExportAsFixedFormat \_ 

        FileName:=pdfPath, \_ 

        FileType:=pjPDF, \_ 

        IncludeDocumentProperties:=True, \_ 

        IncludeDocumentMarkup:=True 

    ' Success message 

    MsgBox "PDF successfully exported to: " & pdfPath, vbInformation 

Else 

    MsgBox "Operation canceled.", vbExclamation 

End If 

End Sub

' Function to get the save path and filename from the user

Private Function GetSaveAsFile(strTitle As String, suggestedFileName As String) As String

Dim SaveFile As OPENFILENAME 

Dim lReturn As Long 

' Set file filter for PDFs 

SaveFile.lpstrFilter = "PDF Files (\*.pdf)" & Chr(0) & "\*.pdf" & Chr(0) 

SaveFile.nFilterIndex = 1 

SaveFile.hwndOwner = 0 

SaveFile.lpstrFile = String(257, 0) ' Buffer for the 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 

lReturn = GetSaveFileName(SaveFile) 

If lReturn = 0 Then 

    GetSaveAsFile = "" 

Else 

    GetSaveAsFile = Trim(Left(SaveFile.lpstrFile, InStr(1, SaveFile.lpstrFile, vbNullChar) - 1)) 

End If 

End Function

Any suggestions or insights on how to properly configure the page setup when using ExportAsFixedFormat would be greatly appreciated. We have tested multiple configurations but cannot get the PDF to export with the desired formatting.

Thank you in advance for your help!

Microsoft 365 and Office | Project | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

John Project 49,715 Reputation points Volunteer Moderator
2024-10-15T15:52:53+00:00

RColombini,

Your question is beyond the scope of this forum. I suggest you post your question to this forum: https://stackoverflow.com/questions/tagged/ms-project

That said, I have found a few instances wherein a VBA method simply does not work as advertised. In those cases I find a workaround since the probability of getting a fix for an errant VBA instruction is unlikely - just my experience.

John

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. John Project 49,715 Reputation points Volunteer Moderator
    2024-10-15T18:21:08+00:00

    RColombini,

    I'm glad you found a workaround, thanks for the feedback.

    John

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-10-15T16:00:28+00:00

    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

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-10-15T11:04:19+00:00

    If somebody wants to test the code, must add the declaration below also.

    Private Declare PtrSafe Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

    Private Type OPENFILENAME

    lStructSize As Long 
    
    hwndOwner As LongPtr 
    
    hInstance As LongPtr 
    
    lpstrFilter As String 
    
    lpstrCustomFilter As String 
    
    nMaxCustFilter As Long 
    
    nFilterIndex As Long 
    
    lpstrFile As String 
    
    nMaxFile As Long 
    
    lpstrFileTitle As String 
    
    nMaxFileTitle As Long 
    
    lpstrInitialDir As String 
    
    lpstrTitle As String 
    
    flags As Long 
    
    nFileOffset As Integer 
    
    nFileExtension As Integer 
    
    lpstrDefExt As String 
    
    lCustData As LongPtr 
    
    lpfnHook As LongPtr 
    
    lpTemplateName As String 
    

    End Type

    Was this answer helpful?

    0 comments No comments