DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Script for report pdf file naming

Hello!

I would like for my pdf export report to have the same name as the data file. I have the script to create the pdf export and allows one to make the file name as they wish however I would like the name to automatically be the same as the csv file and located in the same folder (which is not always the same location). It would be preferred that it does this in the background with no windows popping up for user input.

 

Can someone please help me?!!

 

Dim PDFFile : PDFFile = LayoutWritePath & "Test Results.pdf"

Call Report.Sheets.ExportToPDF(PDFFile,False)

If (Err.Number <> 0) Then

Call MsgBoxDisp("Cannot create the PDF document.")

else

' show results in PDF reader

If FileExist(PDFFile) Then Call ExtProgram("", PDFFile)

If Err.Number <> 0 Then MsgBox "Cannot start the PDF Reader. Have you installed a PDF Reader on your computer?"

End If

 

Thank you!

0 Kudos
Message 1 of 4
(3,257 Views)

Hi I put this together.  You would need to modify your folderPath variable.  This only uses the name of the first csv file in your folderPath so if you just have one csv this will be fine.  Otherwise we'll have to adjust this.  Also, you might need a different value for desiredType so for that I would log the file.Type as the first statement of the For Each loop to see the different types.

Dim FSO, folderPath, aFolder, file
Dim APPEND: APPEND = FALSE
Dim FOUND: FOUND = FALSE
Dim desiredType: desiredType = "Microsoft Excel Comma Separated Values File"
Set FSO = CreateObject("Scripting.FileSystemObject")
folderPath = "Z:\Test Data\Data Watching Folder\"

If FSO.FolderExists(folderPath) Then ' Check the folder exists
  Set aFolder = FSO.GetFolder(folderPath)
  For Each file In aFolder.Files ' For each file in your folder
    If file.Type = desiredType Then ' If it is the right type, use the name
      Dim fileNameArray: fileNameArray = Split(file.Name, ".")
      Call Report.Sheets.ExportToPDF(folderPath & fileNameArray(0) & ".pdf", APPEND)
      FOUND = TRUE
      Exit For ' Exit processing
    End If
  Next
  If FOUND Then
    Call LogfileWrite(CurrentScriptName & ": saved " & folderPath & fileNameArray(0) & ".pdf")
  Else
    Call LogfileWrite(CurrentScriptName & ": no " & desiredType & " was found in specified folder")
  End If
Else
  Call LogfileWrite(CurrentScriptName & ": folder " & folderPath & " does not exist")
End If

 

0 Kudos
Message 2 of 4
(3,244 Views)

Thank you for your reply although I couldn't make it work with your script. Probably just me...

I found a simple solution, just needed to make a minor change to the one line of script I originally posted:

Dim PDFFile : PDFFile = OutPutPath&Data.Root.Name&".pdf"

0 Kudos
Message 3 of 4
(3,240 Views)

OK I did not realize that your root name is already the name of the csv file

0 Kudos
Message 4 of 4
(3,210 Views)