Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Rename file with Macro based on current selection

Hi,

I have been struggling with a macro to rename a file. The background is that I have an action that prints a report with PDF exchange 3.0, the report is generated and saved to a predefined location (set in the printer settings) so far so good.  The problem is that the output file is always the same name (QlikView Printing) what I need to do is rename this file based on a selection mande in a field.

For example, if the user selects USA in the Region field then I need the output pdf to be renamed as USA.pdf.

I have used to function ActiveDocument.Fields("Region").GetPossibleValues  but I am not sure how to pass this to the filename.

I have done this before using a timestamp function but cannot get the selected value into the filename.

Any Ideas? Example code would be great.

I should also mention that I am well aware of 3rd party solutions such as Nprinting that would solve this problem but in this case this is not an option.


1 Solution

Accepted Solutions
Not applicable
Author

Hi Nils,

In case you were still looking for a way to do that, here's a sample code (change to correct field, filename and extension, set the security flag to "allow system access"):

sub RenameFileBasedOnSelection

set vFld = ActiveDocument.Fields("Child").GetSelectedValues

if vFld.Count > 0 then

     vSelected = vFld.Item(0).text

     for i = 1 to vFld.Count - 1

          vSelected = vSelected & ", " & vFld.Item(i).text

     next

     set obj = CreateObject("Scripting.FileSystemObject")

     strFile = "C:\Users\Test\Desktop\QVW\s.csv"

     strRename = "C:\Users\Test\Desktop\QVW\" & vSelected & ".csv"

     if obj.FileExists(strFile) then

          obj.MoveFile strFile, strRename

     else

          msgbox("File Doesn't Exist")

     end if

     set obj = nothing

else

      msgbox("Please select at least one value")

end if

end sub

Hope this will help you.

View solution in original post

3 Replies
Not applicable
Author

Hi Nils,

In case you were still looking for a way to do that, here's a sample code (change to correct field, filename and extension, set the security flag to "allow system access"):

sub RenameFileBasedOnSelection

set vFld = ActiveDocument.Fields("Child").GetSelectedValues

if vFld.Count > 0 then

     vSelected = vFld.Item(0).text

     for i = 1 to vFld.Count - 1

          vSelected = vSelected & ", " & vFld.Item(i).text

     next

     set obj = CreateObject("Scripting.FileSystemObject")

     strFile = "C:\Users\Test\Desktop\QVW\s.csv"

     strRename = "C:\Users\Test\Desktop\QVW\" & vSelected & ".csv"

     if obj.FileExists(strFile) then

          obj.MoveFile strFile, strRename

     else

          msgbox("File Doesn't Exist")

     end if

     set obj = nothing

else

      msgbox("Please select at least one value")

end if

end sub

Hope this will help you.

Anonymous
Not applicable
Author

Thanks this does the trick!

romanp
Contributor
Contributor

Wounderful, when you once in switzerland let me know. your earned minimum one lunch with me 🙂