title: Qlikview Macro VBScript to print pdf and email will not run consistently - Fails in email
body: <p>I have been tearing my hair out over the last few days in trying to get this macro to work consistantly on the Windows scheduler.</p>
  
  <p>Basically the workflow is as follows:</p>
  
  <p>1) Windows Scheduler - Daily, uses Admin user credentials</p>
  
  <p>2) Batch file - Reloads using /l</p>
  
  <p>3) Reloads Qlikview application, which has triggers on post reload to save a pdf and email it using PDF Xchange and an html formatted e-mail to cover mobile.</p>
  
  <p>I am getting such inconsistent behaviour that I cannot isolate the problem to any particular one thing.  Sometimes it works, sometimes it doesn't.  More often than not it fails on the Windows scheduler.  There is no error since QV has just thrown up the VBScript window in the hidden process.</p>
  
  <p>I've been changing permissions, which helped me reach a level of inconsistent performance as opposed to no performance.</p>
  
  <p>In addition, it appears that you cannot pass variables to the PDF Xchange printer.</p>
  
  <p>The code in the macro is as follows:</p>
  
  <pre><code>sub ExportPDF
    printReportPDF "\\SGH-SRV-FPS1\S-Drive\eCommerce\Data Analyst\Reporting\Daily E-Commerce Report\E-Commerce Daily Report.pdf"
  
    ActiveDocument.GetApplication.Sleep 2000
    ActiveDocument.PrintReport "RP01", "PDF-XChange 3.0"
    ActiveDocument.GetApplication.Sleep 8000
  end sub
  
  Function printReportPDF(pdfOutputFile)
    Set WSHShell = CreateObject("WScript.Shell")
    WSHShell.RegWrite "HKCU\Software\Tracker Software\PDF-XChange 3.0\OutputFile", pdfOutputFile, "REG_SZ"
    WSHShell.RegWrite "HKCU\Software\Tracker Software\PDF-XChange 3.0\BypassSaveAs", "1", "REG_SZ"
    Set WSHShell = nothing
  End function
  
  Sub ExportEmail
    Dim strvDest 'as string
    strvDest = ActiveDocument.Variables("vDestination").GetContent().String 
    msgbox(strvDestination)
  
    Define report variables
    get the date as a serial for the filename output
    Export an Object
  
    Set obj = ActiveDocument.ActiveSheet.SheetObjects("TX25")
    Set obj1 = ActiveDocument.ActiveSheet.SheetObjects("TX17")
    Set obj2 = ActiveDocument.ActiveSheet.SheetObjects("TX18")
    Set obj3 = ActiveDocument.ActiveSheet.SheetObjects("TX15")
    Set obj5 = ActiveDocument.ActiveSheet.SheetObjects("CH62")
    Set obj6 = ActiveDocument.ActiveSheet.SheetObjects("TX16")
    Set obj8 = ActiveDocument.ActiveSheet.SheetObjects("CH58")
    Set obj9 = ActiveDocument.ActiveSheet.SheetObjects("TX31")
    Set obj10 = ActiveDocument.ActiveSheet.SheetObjects("CH69")
    msgbox("defined objects")
  
    obj.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\MainLogo.jpg"
    obj1.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\MainHeader.jpg"
    obj2.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\DateRange.jpg"
    obj3.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader.jpg"
    obj5.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\DailySiteDetail.jpg"
    obj6.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader2.jpg"
    obj8.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\WeeklySiteDetail.jpg" 
    obj9.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader3.jpg"
    obj10.ExportBitmapToFile "D:\QlikView\SGP-UDA\QVS_Source\UserApp\WeeklySiteDetailLW.jpg" 
    msgbox("created objects")
  
    Dim objEmail
    Const cdoSendUsingPort = 2    Send the message using SMTP
    Const cdoAnonymous = 0        Do not authenticate
    Const cdoBasic = 1                basic (clear-text) authentication
    Const cdoNTLM = 2                  NTLM
    Const SMTPServer = "xxxx" ' changed for public consumption
    Const SMTPPort = 25                 ' Port number for SMTP
    Const SMTPTimeout = 120              ' Timeout for SMTP in seconds
  
    Set objEmail = CreateObject("CDO.Message")
    Set objConf = objEmail.Configuration
    Set objFlds = objConf.Fields
  
    With objFlds
       ———————————————————————
  
       SMTP server details
       removed the html links down to this being my first post
  
      .Update
      ———————————————————————
    End With
  
    allow the passing of a variable from the load script to define the distribution list
  
    if len(strvDest) &gt; 0 then
      msgbox("variable exists "&amp;strvDest) 
      objEmail.To = strvDest
    else
      msgbox("variable does not exist") 
      objEmail.To = "xxxx" 'changed for public consumption
    end if
  
    objEmail.From = "xxxx" 'changed for public consumption
    objEmail.Subject = "Daily Reporting"
  
    HTML = "&lt;!DOCTYPE HTML PUBLIC ""-//IETF//DTD HTML//EN""&gt;" &amp; chr(13) &amp; chr(10)
    HTML = HTML &amp; "&lt;html&gt;"
    HTML = HTML &amp; "&lt;head&gt;"
    HTML = HTML &amp; "&lt;meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1""&gt;"
    HTML = HTML &amp; "&lt;title&gt;Automated Emails!&lt;/title&gt;"
    HTML = HTML &amp; "&lt;/head&gt;"
    HTML = HTML &amp; "&lt;body bgcolor=""#FFFFFF""&gt;"
  
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:MainLogo.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:MainHeader.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:DateRange.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:SecondaryHeader.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:DailySiteDetail.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:SecondaryHeader2.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:WeeklySiteDetail.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:SecondaryHeader3.jpg"" &gt;"
    HTML = HTML &amp; "&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;img src=""cid:WeeklySiteDetailLW.jpg"" &gt;" 
    HTML = HTML &amp; "&lt;p&gt;"
    HTML = HTML &amp; "&lt;/p&gt;"
    HTML = HTML &amp; "&lt;/body&gt;"
    HTML = HTML &amp; "&lt;/html&gt;"
  
    Set objBP = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\MainLogo.jpg", "MainLogo.jpg", CdoReferenceTypeName)
    objBP.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;MainLogo.jpg&gt;"
    objBP.Fields.Update
  
    Set objBP1 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\MainHeader.jpg", "MainHeader.jpg", CdoReferenceTypeName)
    objBP1.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;MainHeader.jpg&gt;"
    objBP1.Fields.Update
  
    Set objBP2 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\DateRange.jpg", "DateRange.jpg", CdoReferenceTypeName)
    objBP2.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;DateRange.jpg&gt;"
    objBP2.Fields.Update
  
    Set objBP3 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader.jpg", "SecondaryHeader.jpg", CdoReferenceTypeName)
    objBP3.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;SecondaryHeader.jpg&gt;"
    objBP3.Fields.Update
  
    Set objBP5 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\DailySiteDetail.jpg", "DailySiteDetail.jpg", CdoReferenceTypeName)
    objBP5.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;DailySiteDetail.jpg&gt;"
    objBP5.Fields.Update
  
    Set objBP6 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader2.jpg", "SecondaryHeader2.jpg", CdoReferenceTypeName)
    objBP6.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;SecondaryHeader2.jpg&gt;"
    objBP6.Fields.Update
  
    Set objBP8 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\WeeklySiteDetail.jpg", "WeeklySiteDetail.jpg", CdoReferenceTypeName)
    objBP8.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;WeeklySiteDetail.jpg&gt;"
    objBP8.Fields.Update 
  
    Set objBP9 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\SecondaryHeader3.jpg", "SecondaryHeader3.jpg", CdoReferenceTypeName)
    objBP9.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;SecondaryHeader3.jpg&gt;"
    objBP9.Fields.Update 
  
    Set objBP10 = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\WeeklySiteDetailLW.jpg", "WeeklySiteDetailLW.jpg", CdoReferenceTypeName)
    objBP10.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;WeeklySiteDetailLW.jpg&gt;"
    objBP10.Fields.Update
  
    Set objBPDoc = objEmail.AddRelatedBodyPart("D:\QlikView\SGP-UDA\QVS_Source\UserApp\Qlikview Printing.pdf", "Qlikview Printing.pdf", CdoReferenceTypeName)
    objBPDoc.Fields.Item("urn:schemas:mailheader:Content-ID") = "&lt;Qlikview Printing.pdf&gt;"
  
    objBPDoc.Fields.Update
    objEmail.HTMLBody = HTML
  
    msgbox("attached objects")
  
    objEmail.Send
  
  Set objFlds = Nothing
  Set objConf = Nothing
  Set objEmail = Nothing
  
  ActiveDocument.Save
  Application.Quit
  </code></pre>
  
  <p>End Sub </p>