Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Script for Pressing a button which sends email notification

Hi experts,

I have a edit module script which send email when executed.. This module is set triggered for a button under external action "RUN MACRO".

when this button is pressed, The macro runs and send an email notification on job completion.

In order to press this button automatically , a batch script is written and schedule on windows scheduler, which is not working as desired... but there is no problem with the edit module script. when the qvw file is opened and manaully if you click on button, it send notification.

but problem is with the triggers of qvw file

Here is the vb script, which get triggered by window batch script and this bacth scipt gets exceuted on arrival of falg file.

macro.vbs:

Set MyApp = CreateObject("QlikTech.QlikView")
Set MyDoc = MyApp.OpenDoc ("D:\SMTP_Mail.qvw","","")
Set ActiveDocument = MyDoc
ActiveDocument.Reload
ActiveDocument.GetSheetObject("SendMail").Press
MyDoc.Save
Set MyDoc = Nothing
MyApp.Quit
Set MyApp = Nothing

Batch scipt: ( on occurance of flag.txt file in D -drive, vb macro gets exceuted)

set tmpfolder=D:\Flag
if exist "%tmpfolder%\*.txt" cscript "Macro.vbs"

Kindly assist me! and let me know if you need more details...

Thank you!

Vinod

5 Replies
colinh
Partner - Creator II
Partner - Creator II

Hi Vinod

You should be able to do everything within your QlikView document, without needing an external macro. The most reliable way of doing this that I have found is as follows:

  • Create a macro to send your mail, which it looks like you already have (mine is called MailReport).
  • Create a variable in your document (I call it vPrint) with a value = 0.
  • Create a routine to check the value of this variable and send the mail if vPrint = 1. Mine looks like this:

sub start

set p = ActiveDocument.Variables("vPrint")

if p.GetContent.String <> "0" then

MailReport

p.SetContent "0",true

ActiveDocument.Save

ActiveDocument.GetApplication.Quit

end if

end sub

  • Create an On Open trigger to run the macro start when the document is opened.
  • Create a batch script with the following 2 lines (these will replace cscript "Macro.vbs" in your batch file):

"c:\Program Files\QlikView\qv.exe" /r "D:\SMTP_Mail.qvw"

"c:\Program Files\QlikView\qv.exe" /vvPrint=1 "D:\SMTP_Mail.qvw"

This will reload the document without mailing, and then open it again and set vPrint = 1, which will cause it to run the print macro. I have also tried this using an On Reload trigger, but I found that much less reliable.

You can open your document for editing as normal, as vPrint is only = 1 when the document is opened from the batch file.

The first time you run this you will probably be prompted to set the macro security to Allow System Access.

Hope this is useful.

Colin.



Not applicable
Author

Hi Colin,

Thanks for your quick reply. The code worked successfully.

To make me more clear about the solution,

Could you please clarify In what sequence it is executing the code...

step1.batch file code c:\Program Files\QlikView\qv.exe" /r "D:\SMTP_Mail.qvw" : This opens the qlikview file and reloads it.

step2. "c:\Program Files\QlikView\qv.exe" /vvPrint=1 "D:\SMTP_Mail.qvw" : This assigns the value to variable vprint =1

step3. but on open of qvw file, routine " start" has to be exceuted which inturn call sub routine " MailReport"...?? but when is this happening?

I am just missing the flow..:-(

Moreover, This qvw application has to be created as dependent task on qvd generator.

once qvd generator is exceuted, we have to run this qvw application.. How to tell this qlivkiew?

Hope I am clear!

Thanks

Vinod

colinh
Partner - Creator II
Partner - Creator II

To make step 3 happen, you have to set an On Open trigger to run the start macro. Select Settings > Document Properties > Triggers, and under Document Event Triggers, select On Open. Then click on Add Action(s) and add an External Action - Run Macro. Enter start in the Macro Name field.

Then every time the document opens, start will check the value of vPrint. When you're opening from File > Open, or by double clicking, the value is always = 0, so the start macro doesn't call the print routine. When you open from the batch file, it passes vPrint = 1, which causes the print routine to run.

As for running after the QVD generator, there are two ways I use to do this. You can either do it the way you have done, by checking for the existence of your text file, and maybe run the batch job every hour, or on some set schedule, or you can just use one batch file and run the commands one after the other. So in your batch file you'd have:

"c:\Program Files\QlikView\qv.exe" /r "D:\QVD_Generator.qvw"

"c:\Program Files\QlikView\qv.exe" /r "D:\SMTP_Mail.qvw"

"c:\Program Files\QlikView\qv.exe" /vvPrint=1 "D:\SMTP_Mail.qvw"

Windows will run the QVD generator and then run SMTP_Mail.



The only drawback of this is that if QVD Generator fails, SMTP Mail will not run, although you can get around this by setting ErrorMode = 0 in QVD generator and having an email alert that checks ScriptErrorCount > 0 in QVD Generator, as well as build in a chart or alert in SMTP Mail that checks the age of the QVDs using the filetime() function in the script.

Colin.

Not applicable
Author

Hi colin,

Its was absolute perfect !!. Thanks for your reply..Till now i was testing on the email functionality and how to acheive it independently..

Can you suggest me one thing...

This is my flow...

flag file will arrive on server and by which my task created for QVD generator on QEMC will get triggered and qvd files will be created. I have created a dependent task on QEMC itself to run the final application like..on its completion, trigger/run application.qvw file. ..

Now where to incorporate this email functionality..... so that it best fits the requirement.. On compeletion of reload of QVD , it starts application relaod and over after, it sends the email status notification .

Thanks

Vinod

colinh
Partner - Creator II
Partner - Creator II

Hi Vinod

I'm not sure I'm following you exactly. At what point is the flag file created, before or after the QVDs are created? Is a flag file created by the QVD creation process or is it used to trigger the QVD creation process.

Are you using QlikView Server to do the reloads? You will not be able to use the server reload schedule to run the document that sends the mail, the macro won't run. You will need to use batch files and Windows Task Scheduler.

Colin.