BusinessObjects Board

Saving multiple saved .rep reports in one directory to PDF

Hello,

I am wondering if this is possible maybe through some sort of VBA code or a batch script… I am trying to save all of the saved *.rep reports in a specific directory to PDF. I have found information on adding VB code in a macro to a single file and then when that file is opened, it saves to PDF but I would have to add the code to EVERY file in the directory…

Is there any way I can save/export them ALL to PDF in one step?

Thanks,
Jen


JenObrien (BOB member since 2008-07-11)

There is some code here … Save for all users that will interatively process all documents in a folder. Substituting .ExportAsPDF for .SaveAs should get you close.


Dwayne Hoffpauir :us: (BOB member since 2002-09-19)

Thank you but I forgot to mention… I’m using 5.1.7 :frowning: I couldn’t open this without 6.0


JenObrien (BOB member since 2008-07-11)

In a new document, create a variable name . It can be displayed in your report if needed. Create a new VBA module (Alt-F11, Insert Module…), and copy in the following code.

Instructions are as follows …

Option Explicit

Sub SaveForAllUsers()

    Dim FileList As New Collection
    Dim FileName As Variant
    Dim Doc As Document
    On Error GoTo ErrorHandler

    ThisDocument.DocumentVariables("Messages").Formula = "Messages:"
    Call BuildList(FileList, ThisDocument.path & "\")

    On Error GoTo FileErrors
    For Each FileName In FileList
        Set Doc = Application.Documents.Open(FileName, True, False)
        Call Doc.SaveAs(Doc.FullName, True)
        Call Doc.Close(boDontSave)
    Next FileName

CleanUp:
    On Error Resume Next
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Number & ":  " & Err.Description
    Resume CleanUp

FileErrors:
    ThisDocument.DocumentVariables("Messages").Formula = _
        ThisDocument.DocumentVariables("Messages").Formula & vbCrLf & _
        FileName & ":  " & Err.Number & " - " & Err.Description
    Resume Next

End Sub

Private Sub BuildList(FileList As Collection, PathName As Variant)

    Dim DirList As New Collection
    Dim DirName As Variant
    Dim FileName As String

    On Error GoTo ErrorHandler

    FileName = Dir(PathName & "*.rep")
    Do Until FileName = vbNullString
        If PathName &amp; FileName <> ThisDocument.FullName Then
            Call FileList.Add(PathName &amp; FileName)
        End If
        FileName = Dir
    Loop

    DirName = Dir(PathName, vbDirectory)
    Do Until DirName = vbNullString
        If (GetAttr(PathName &amp; DirName) And vbDirectory) = vbDirectory And _
                DirName <> "." And DirName <> ".." Then
            Call DirList.Add(PathName &amp; DirName &amp; "\")
        End If
        DirName = Dir
    Loop

    For Each DirName In DirList
        Call BuildList(FileList, DirName)
    Next DirName

CleanUp:
    On Error Resume Next
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Number &amp; ":  " &amp; Err.Description
    Resume CleanUp

End Sub

Dwayne Hoffpauir :us: (BOB member since 2002-09-19)

Wow this is great! Thank you Dwayne!

I think I’m just about there… I have substituted the code for PDF but it insists on keeping the .rep prefix whne it saves the filename. Ex) FileName.rep.pdf

Do you have any ideas on how to strip that out? I tried some code to take only a portion of the filename but it seems like you need it there… it didn’t save when I did that.

Sub SaveForAllUsers()

Dim FileList As New Collection
Dim FileName As Variant
Dim Doc As Document
Dim sFileName As Variant

On Error GoTo ErrorHandler

ThisDocument.DocumentVariables("Messages").Formula = "Messages:"
Call BuildList(FileList, ThisDocument.Path &amp; "\")

On Error GoTo FileErrors
For Each FileName In FileList
    Set Doc = Application.Documents.Open(FileName, True, False)
    Set Doc = ActiveDocument
    sFileName = FileName &amp; ".pdf"
    Call Doc.ExportAsPDF(sFileName)
    Call Doc.Close(boDontSave)
Next FileName

CleanUp:
On Error Resume Next
Exit Sub

ErrorHandler:
MsgBox Err.Number & ": " & Err.Description
Resume CleanUp

FileErrors:
ThisDocument.DocumentVariables(“Messages”).Formula = _
ThisDocument.DocumentVariables(“Messages”).Formula & vbCrLf & _
FileName & ": " & Err.Number & " - " & Err.Description
Resume Next

End Sub

Thanks!
Jen


JenObrien (BOB member since 2008-07-11)

Try this …

sFileName = Left(FileName, Len(FileName) - 4) &amp; ".pdf"

Dwayne Hoffpauir :us: (BOB member since 2002-09-19)

It looks like that worked! I forgot to append pdf! Yikes :slight_smile:

Thank you SO MUCH for your help! This will save me TONS of time!

Jen


JenObrien (BOB member since 2008-07-11)