Dim objRep As Object
Dim objPDF as Object

Dim configFile as String
Dim publish As String

Dim Debug As Integer

Dim reportFile As String
Dim exportFormat As String
Dim layers As String

Declare Sub ReportExport   


Sub Main ()
   Debug = 1
   Rem On Error Resume Next
   If Debug = 1 Then
      configFile = "C:\Projekte\Python\gctools\config\Reports.csv"
      publish = "C:\GAPS\Portal\daten"
   Else
      configFile = GetField(Command, 1, ",")
      publish = GetField(Command, 2, ",")
   End If

   
   Dim lineStr As String
   Dim reportFile As String
   Dim exportFormat As String
   Dim layers As String
      
   Open configFile For Input As #1
      Do While Not EOF(1)
         Line Input #1, lineStr 
         reportFile = GetField(lineStr, 1, ";")
         exportFormat = GetField(lineStr, 2, ";")
         layers = GetField(lineStr, 3, ";")
         Print reportFile, exportFormat
         Call ReportExport

      Loop
   Close #1
End Sub


Sub ReportExport   
   ' reportFile, exportFormat, layers as Parameters
   Dim exportFile as String
   Dim layer as String
   Dim layerInt as Integer

   Set objRep = CreateObject("CognosPowerPlay.Report")
   objRep.Open(folder + "\" + reportFile)
   exportFile = publish + "\" + Left(reportFile, Len(reportFile) -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 255
            layer = GetField(layers, i, ",")
            If layer = "" Then
               Exit For
            End If
            layerInt = Val(layer)
            If layerInt > objRep.Layers.Count Then
               Exit For
            End If

            Set objPDF = objRep.PDFFile(exportFile + "_" + layer + ".pdf", True)
            With objPDF
               .SetListOfLayersToSave objRep.Layers.Subset(layerInt, layerInt)
               .SetListOfRowsToSave objRep.Rows
               .SaveEntireReport = False
               .SaveAllCharts = False           
               .AxisOnAllPages = True
               .ChartTitleOnAllPages = True
               .IncludeLegend = True
            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
   Set objRep = Nothing

End Sub

