Dim objApp As Object
Dim objRep As Object
Dim filename as String
Dim folder as String
Dim publish As String
Dim exportFormat As String

Sub Main ()
   Rem On Error Resume Next
   Set objApp = CreateObject("CognosPowerPlay.Application")
   rem folder = "C:\GAPS\Portal\System\Report"
   rem filename = "test.ppr"
   rem exportFormat = "pdf"
   rem publish = "C:\GAPS\Portal\daten"
   
   folder = GetField(Command, 1, ",")
   filename = GetField(Command, 2, ",")
   exportFormat = GetField(Command, 3, ",")
   publish = GetField(Command, 4, ",")

   Dim exportFile as String
   Dim objPDF as Object
   
   If LCase(Right(filename, 4)) = ".ppr" Or LCase(Right(filename, 4)) = ".ppx" Then
      Set objRep = CreateObject("CognosPowerPlay.Report")
      objRep.Open(folder + "\" + filename)
      exportFile = publish + "\" + Left(filename, Len(filename) -4)

      Select Case exportFormat
         Case "pdf"
            
            Set objPDF = objRep.PDFFile(exportFile + "_0.pdf", True)
            With objPDF
               .SaveEntireReport = True
               .AxisOnAllPages = True
               .ChartTitleOnAllPages = False
               .IncludeLegend = False
            End With
            objPDF.Save

            For i = 1 to objRep.Layers.Count
               Set objPDF = objRep.PDFFile(exportFile + "_" + i + ".pdf", True)
               With objPDF
                  .SaveEntireReport = False
                  .SaveAllCharts = False                     
                  .AxisOnAllPages = True
                  .ChartTitleOnAllPages = False
                  .IncludeLegend = False
                  .SetListOfLayersToSave objRep.Layers.Subset(i, i)
                  .SetListOfRowsToSave objRep.Rows
               End With
               objPDF.Save
            Next

         Case "asc"
            objRep.SaveAs exportFile, 3
         Case "xls"
            objRep.SaveAs exportFile, 4
         Case "ppx"
            objRep.SaveAs exportFile, 5
         Case Else
            objRep.Publish publish, False, True, True    
      End Select
      objRep.Close
   End if

   objApp.Quit
End Sub
