Results 1 to 2 of 2

Thread: How to close outlook from Task Schedular using VB Script

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    2

    How to close outlook from Task Schedular using VB Script

    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

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,909

    Re: How to close outlook from Task Schedular using VB Script

    What is your script's entry point? Is this the complete script?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width