title: Call Outlook procedure using VBScript
body: <p>I have a procedure in Outlook that sends all the saved messages in <code>Drafts</code> folder.<bR>
  Below is the code:<br></p>
  
  <pre><code>Public Sub SendMail()
  
  Dim olApp As Outlook.Application
  Dim olNS As Outlook.NameSpace
  Dim olFolder As Outlook.MAPIFolder
  Dim olDraft As Outlook.MAPIFolder
  Dim strfoldername As String
  Dim i As Integer
  
  Set olApp = Outlook.Application
  Set olNS = olApp.GetNamespace("MAPI")
  Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
  
  strfoldername = olFolder.Parent
  
  Set olDraft = olNS.Folders(strfoldername).Folders("Drafts")
  
  If olDraft.Items.Count &lt;&gt; 0 Then
      For i = olDraft.Items.Count To 1 Step -1
          olDraft.Items.Item(i).Send
      Next
  End If
  
  End Sub
  </code></pre>
  
  <p>Above code works fine.<br></p>
  
  <p><strong>Question:</strong></p>
  
  <p>I want to use <code>Task Scheduler</code> to fire this procedure as a specified time.<br>
  1. Where will I put the procedure in Outlook, Module or ThisOutlookSession?<br>
  2. I am not good in <code>vbscript</code> so I also don't know how to code it to call the Outlook Procedure. I've done calling Excel Procedure but Outlook doesn't support <code>.Run</code> property.<br></p>
  
  <p>So this doesn't work:</p>
  
  <pre><code>Dim olApp
  
  Set olApp = CreateObject("Outlook.Application")
  olApp.Run "ProcedureName"
  
  Set olApp = Nothing
  </code></pre>
  
  <p>I've also read about the <code>Session.Logon</code> like this:</p>
  
  <pre><code>Dim olApp
  
  Set olApp = CreateObject("Outlook.Application")
  olApp.Session.Logon
  olApp.ProcedureName
  
  Set olApp = Nothing
  </code></pre>
  
  <p>But it throws up error saying object <code>ProcedureName</code> is not supported.<br>
  Hope somebody can shed some light.<br></p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>Ok, I've figured out 2 work around to Avoid or get pass this pop-up.</p>
  
  <p><img src="https://i.imgur.com/3HURYjc.png" alt="popup"></p>
  
  <p><strong>1st one:</strong> is as KazJaw Pointed out.<br></p>
  
  <p>Assuming you have another program (eg. Excel, VBScript) which includes sending of mail via <code>Outlook</code> in the procedure.<br>
  Instead of using <code>.Send</code>, just <code>.Save</code> the mail.<br>
  It will be saved in the <code>Outlook's Draft</code> folder.<br>
  Then using below code,  send the draft which fires using <code>Outlook Task Reminder</code>.<br></p>
  
  <pre><code>Option Explicit
  Private WithEvents my_reminder As Outlook.Reminders
  
  Private Sub Application_Reminder(ByVal Item As Object)
  
  Dim myitem As TaskItem
  
  If Item.Class = olTask Then 'This works the same as the next line but i prefer it since it automatically provides you the different item classes.
  'If TypeName(Item) = "TaskItem" Then
      Set my_reminder = Outlook.Reminders
      Set myitem = Item
      If myitem.Subject = "Send Draft" Then
          Call SendMail
      End If
  End If
  
  End Sub
  
  Private Sub my_reminder_BeforeReminderShow(Cancel As Boolean)
  
  Cancel = True
  Set my_reminder = Nothing
  
  End Sub
  </code></pre>
  
  <p>Above code fires when <code>Task Reminder</code> shows with a subject "Send Draft".<br>
  But, we don't want it showing since the whole point is just to call the <code>SendMail</code> procedure.<bR>
  So we added a procedure that <code>Cancels</code> the display of reminder which is of <code>olTask</code> class or <code>TaskItem</code> Type.<br></p>
  
  <p>This requires that <code>Outlook</code> is running of course.<bR>
  You can keep it running 24 hours as i did or, create a <code>VBscript</code> that opens it to be scheduled via <code>Task Scheduler</code>.</p>
  
  <p><strong>2nd one:</strong> is to use API to programatically click on <kbd>Allow</kbd> button when the security pop-up appears.<br>
  Credits to SiddarthRout for the help.<br>
  Here is the <a href="http://www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/" rel="noreferrer">LINK</a> which will help you programmatically click on the <kbd>Allow</kbd> button.<br>
  Of course you have to tweak it a bit.</p>