Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Fitus9092
Contributor III
Contributor III

Macro To Generate multiple file with different file name

Hello,

i'm using this VBscript code to generate multiple pdf file for each value of the field "cdr cod".

Sub ReportPrint

Set soc = ActiveDocument.Fields("cdr cod")

Set socPossible = soc.GetPossibleValues

For i = 0 to socPossible.Count-1

soc.Select socPossible.Item(i).Text

ActiveDocument.GetApplication.WaitForIdle

ActiveDocument.PrintDocReport "RP01", "PDF-XChange 3.0", false

Next

End Sub

It works, but how i can set a file path and save automatically each file name with each value in "cdr cod"? 

Example: value1.pdf, value2.pdf etc.

Thanks.

Labels (1)
1 Solution

Accepted Solutions
marcus_sommer

Probably it's possible by changing the configurations from the used printer PDF-XChange 3.0 - within the registry or any config-files like it was possible with the old QlikView pdf-printer. Some years ago I looked myself for it but didn't found the right ones - whereby I didn't searched for long else moved rather quickly for a bypass-logic which is quite simple:

  • creating a default store-folder in C:\PrintTemp and using the print-properties to set this folder as default and disabling there any dialog for the saving-task
  • moving the pdf-file after the saving to the target-place by renaming it in the same statement

This means you need to add the following within your loop-code to get it to working:

...
Set objFSO = CreateObject("Scripting.FileSystemObject")
...
ActiveDocument.GetApplication.Sleep 1000
...
objFSO.MoveFile sPDFPath_src, PDFFullName
...

The bold ones are variables which you could set like you want but of course they must be a valid full-path. The sleep-statements - you may need multiple ones with n milliseconds between your various steps to ensure that the files were already stored and moved before the next loop-iteration runs - it depends mostly on the minimal performance of your storage/network.

- Marcus

View solution in original post

8 Replies
marcus_sommer

Probably it's possible by changing the configurations from the used printer PDF-XChange 3.0 - within the registry or any config-files like it was possible with the old QlikView pdf-printer. Some years ago I looked myself for it but didn't found the right ones - whereby I didn't searched for long else moved rather quickly for a bypass-logic which is quite simple:

  • creating a default store-folder in C:\PrintTemp and using the print-properties to set this folder as default and disabling there any dialog for the saving-task
  • moving the pdf-file after the saving to the target-place by renaming it in the same statement

This means you need to add the following within your loop-code to get it to working:

...
Set objFSO = CreateObject("Scripting.FileSystemObject")
...
ActiveDocument.GetApplication.Sleep 1000
...
objFSO.MoveFile sPDFPath_src, PDFFullName
...

The bold ones are variables which you could set like you want but of course they must be a valid full-path. The sleep-statements - you may need multiple ones with n milliseconds between your various steps to ensure that the files were already stored and moved before the next loop-iteration runs - it depends mostly on the minimal performance of your storage/network.

- Marcus

Fitus9092
Contributor III
Contributor III
Author

It doesn't work

Sub ReportPrint

 

Dim sPDFName

Dim sPDFPath

Set soc = ActiveDocument.Fields("cdr cod")

Set socPossible = soc.GetPossibleValues

For i = 0 to socPossible.Count-1

soc.Select socPossible.Item(i).Text


sPDFName = selo.Item(i).text & ".pdf"

sPDFPath = "C:\Users\bho\"

Set objFSO = CreateObject("Scripting.FileSystemObject")


ActiveDocument.GetApplication.WaitForIdle

ActiveDocument.PrintDocReport "RP01", "PDF-XChange 3.0", false

ActiveDocument.GetApplication.Sleep 1000

objFSO.MoveFile "sPDFPath"&sPDFName, "sPDFPath" &sPDFName

Next

End Sub

marcus_sommer

What didn't work - nothing happens, not the right/expected things, any error occurred (where and with which message)?

The line:

Set objFSO = CreateObject("Scripting.FileSystemObject")

should in general work within the loop but it's better to place it above.

Further helpful would be to use some message-boxes to display the variable-values to see if they contain the right content.

Beside this I mentioned above various settings within the printer-properties - were they applied?

- Marcus

Fitus9092
Contributor III
Contributor III
Author

Yes i did the print settings as you said and i changed the Set objFSO = CreateObject("Scripting.FileSystemObject") above the loop.

But still don't work, after print the first value in the temp folder he stop the run without giving any error.

 

Dim sPDFName

Dim sPDFPath

Set soc = ActiveDocument.Fields("cdr cod")

Set socPossible = soc.GetPossibleValues
Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = 0 to socPossible.Count-1

soc.Select socPossible.Item(i).Text


sPDFName = socPossible.Item(i).text & ".pdf"

sPDFPath = "C:\Users\bho\"




ActiveDocument.GetApplication.WaitForIdle

ActiveDocument.PrintDocReport "RP01", "PDF-XChange 3.0", false

ActiveDocument.GetApplication.Sleep 1000

objFSO.MoveFile "sPDFPath"&sPDFName, "sPDFPath" &sPDFName

Next

End Sub

Thanks.

Fitus9092
Contributor III
Contributor III
Author

Instead of using PDF-XChange 3.0, there is another way to use for example Microsoft Print to PDF ?

marcus_sommer

At least this part isn't correct:

objFSO.MoveFile "sPDFPath"&sPDFName, "sPDFPath" &sPDFName

because source and target are the same. It should look more like:

objFSO.MoveFile "C:\PrintTemp\Default.pdf", "D:\Folder\" & sPDFName

The file-name isn't Default - you need to look how it's called which could be also defined within the printer-properties.

Like above mentioned it's helpful to use msgbox to display any variable-content to see if it worked like expected. Also the moving-part with the renaming might be tested separately just with any dummy-files.

- Marcus

 

marcus_sommer

AFAIK QlikView changed already the default-printer with 12.6 to the Microsoft Print to PDF - what probably means that they just left to deliver an own (sold) pdf-printer because the MS printer is already on all current systems. Therefore you may try to switch to it whereby I don't know how it could be configured. Maybe you find something already here in the community. Otherwise you need to search directly by MS.

- Marcus 

Fitus9092
Contributor III
Contributor III
Author

I did it.

Many thanks Marcus