This content is not available in your preferred language.

The content is shown in another available language. Your browser may include features that can help translate the text.

Saving a DIAdem Report as PDF Using VBScript

Updated Feb 27, 2023

Environment

Software

  • DIAdem

This article demonstrates how to programmatically save a DIAdem Report as a PDF with Visual Basic Script (VBS). The functions used throughout the example identify how to create a specify a PDF file name and save location.

Follow the steps below to implement a VBS that automatically saves a DIAdem report as a PDF:

1. In the SCRIPT Pane, open a new VBS.
  • Click File >> New VBS.
  • Save the file as "Save Report to PDF.VBS".
2. Define the following variables to use throughout the script:
  • MyFileNames - used to store the paths of loaded data files.
  • MyFolders - used to store the path of the PDF report.
  • CurrentPath - used to split the data file path into an array.
  • MyPDFName - used to store the file name of the PDF.
  • iCount - used to iterate through a For Loop.

3. Clear the Data Portal with the Call Data.Root.Clear() command.
4. Use the FileNameGet() function to prompt the user to select data files to load.
  • To load data files from the current read path, set the [FileDlgName] parameter to DataReadPath.
  • The example below demonstrates how to configure a File Dialog to load .dat files.
Option Explicit  'Forces the explicit declaration of all the variables in a script.
Dim MyFileNames, MyFolders, CurrentPath, MyPDFName, iCount

MyFolders = "C:\Users" '--- Directory to load report from and save PDF to

Call Data.Root.Clear() '--- clear the Data Portal.
Call FileNameGet("ANY", "FileRead", DataReadPath, "DAT data (*.dat),*.dat", "All.lst", True, "Data selection") '--- Select files to load with a file dialog.
MyFileNames = Split(FileDlgFileName,"|") '--- Store the selected file paths in MyFileNames.

5. Store the selected files in the MyFileNames array using the following line:
 
MyFileNames = Split(FileDlgFileName,"|") '--- Store the selected file paths in MyFileNames.

6. Load each of the selected files with a For Loop, as shown.
 
For iCount = 0 To Ubound(MyFileNames) '--- Load the selected files.
  Call DataFileLoad(MyFileNames(iCount))
Next

7. Optional: Load a template report (.tdr file) to automatically plot the data in tables/graphs. This can then be exported to a PDF later.
  • Use the Report.LoadLayout method, similarly to below.
  • Note: a report must exist in the directory stored in MyFolders.
Call Report.LoadLayout(MyFolders & "\Report Template.TDR") '--- Load a template report into DIAdem and populate it with loaded data.
Call Report.Refresh '--- View the Report panel.

8. Get the full path of the last loaded data file and split it to isolate the file extension. Then, save the report in the same directory.
  • Use the Split() method below to achieve this.
MyFileNames = Split(FileDlgFileName,".") '---- Split the file path to remove the .dat extension.
Call Report.SaveLayout(MyFileNames(1)) '---- Save the report as <filename>.tdr instead of <filename>.dat.tdr

9. Isolate the name of the loaded data file and save the PDF with the same name.
 
CurrentPath = Split(MyFileNames(0), "\") '--- Get the full path of your current .dat file (without the .dat extension).
MyPDFName = CurrentPath(UBound(CurrentPath)) '--- Find the last element in the CurrentPath array i.e. removing the C:\Users part so you're just left with the file name.
Call Report.Sheets.ExportToPDF(MyFolders & "\" & MyPDFName,FALSE) '--- Save the report as a PDF.
Call MsgBoxDisp("PDF saved to: " & MyFolders & "\" & MyPDFName

Once complete, the full VBS should resemble the following:
 
Option Explicit  'Forces the explicit declaration of all the variables in a script.
Dim MyFileNames, MyFolders, CurrentPath, MyPDFName, iCount

MyFolders = "C:\Users" '--- Directory to load report from and save PDF to

Call Data.Root.Clear() '--- clear the Data Portal.
Call FileNameGet("ANY", "FileRead", DataReadPath, "DAT data (*.dat),*.dat", "All.lst", True, "Data selection") '--- Select files to load with a file dialog.
MyFileNames = Split(FileDlgFileName,"|") '--- Store the selected file paths in MyFileNames.

For iCount = 0 To Ubound(MyFileNames) '--- Load the selected files.
  Call DataFileLoad(MyFileNames(iCount))
Next

Call Report.LoadLayout(MyFolders & "\Report Template.TDR") '--- Load a template report into DIAdem and populate it with loaded data.
Call Report.Refresh '--- View the Report panel.

MyFileNames = Split(FileDlgFileName,".") '---- Split the file path to remove the .dat extension.
Call Report.SaveLayout(MyFileNames(1)) '---- Save the report as <filename>.tdr instead of <filename>.dat.tdr

CurrentPath = Split(MyFileNames(0), "\") '--- Get the full path of your current .dat file (without the .dat extension).
MyPDFName = CurrentPath(UBound(CurrentPath)) '--- Find the last element in the CurrentPath array i.e. removing the C:\Users part so you're just left with the file name.
Call Report.Sheets.ExportToPDF(MyFolders & "\" & MyPDFName,FALSE) '--- Save the report as a PDF.
Call MsgBoxDisp("PDF saved to: " & MyFolders & "\" & MyPDFName)