Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

How to close outlook from Task Schedular using VB Script

$
0
0
The VB Script code below check for an instance of outlook and close it if it is running before opening another instance of outlook and send the mail. It works fine when I tested it manually in QlikView but once we try to start it from the Task Schedular it does not recognize the running instance of outlook it will just opening another instance.
Please, how should I make Task Schedular to recognize the already running instance of outlook to close it before opening another instance? here is the code :
Code:

Sub mSendMail(pdfFilePath)
    Dim objOutlk
    Dim objMail
    Const olMailItem = 0
   
    ' Close any running instances of Outlook
    On Error Resume Next
    Set objOutlk = GetObject(, "Outlook.Application")
    If Not objOutlk Is Nothing Then
        objOutlk.Quit
        Set objOutlk = Nothing
        ' Wait for Outlook to close
        ActiveDocument.GetApplication.Sleep 10000 ' Wait for 10 seconds after closing Outlook
    End If
    On Error GoTo 0
   
    ' Start Outlook
    StartOutlook
   
    ' Wait for Outlook to start
    ActiveDocument.GetApplication.Sleep 5000 ' Wait for 5 seconds for Outlook to start
   
    ' Create a new instance of Outlook application
    Set objOutlk = CreateObject("Outlook.Application")
   
    ' Create a new mail item
    Set objMail = objOutlk.createitem(olMailItem)
   
    ' Recipient's email address
    objMail.To = "example@yahoo.com"
   
    ' Subject of the email
    objMail.Subject = "Testing " & Date()
   
    ' Body of the email
    objMail.HTMLBody = "Body of the email, This is an automatic generated email from QlikView."
   
    ' Add attachment (use the generated PDF file)
    objMail.Attachments.Add pdfFilePath
   
    ' Send the email
    objMail.Send
   
    ' Release resources
    Set objMail = Nothing
    Set objOutlk = Nothing
End Sub

Sub StartOutlook()
    Dim oShell
    Set oShell = CreateObject("Wscript.Shell")
    oShell.Run "cmd.exe /K ""C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"" ", 0, False
End Sub


Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>