7

I have a procedure in Outlook that sends all the saved messages in Drafts folder.
Below is the 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 <> 0 Then
    For i = olDraft.Items.Count To 1 Step -1
        olDraft.Items.Item(i).Send
    Next
End If

End Sub

Above code works fine.

Question:

I want to use Task Scheduler to fire this procedure as a specified time.
1. Where will I put the procedure in Outlook, Module or ThisOutlookSession?
2. I am not good in vbscript 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 .Run property.

So this doesn't work:

Dim olApp

Set olApp = CreateObject("Outlook.Application")
olApp.Run "ProcedureName"

Set olApp = Nothing

I've also read about the Session.Logon like this:

Dim olApp

Set olApp = CreateObject("Outlook.Application")
olApp.Session.Logon
olApp.ProcedureName

Set olApp = Nothing

But it throws up error saying object ProcedureName is not supported.
Hope somebody can shed some light.

SOLUTION:

Ok, I've figured out 2 work around to Avoid or get pass this pop-up.

popup

1st one: is as KazJaw Pointed out.

Assuming you have another program (eg. Excel, VBScript) which includes sending of mail via Outlook in the procedure.
Instead of using .Send, just .Save the mail.
It will be saved in the Outlook's Draft folder.
Then using below code, send the draft which fires using Outlook Task Reminder.

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

Above code fires when Task Reminder shows with a subject "Send Draft".
But, we don't want it showing since the whole point is just to call the SendMail procedure.
So we added a procedure that Cancels the display of reminder which is of olTask class or TaskItem Type.

This requires that Outlook is running of course.
You can keep it running 24 hours as i did or, create a VBscript that opens it to be scheduled via Task Scheduler.

2nd one: is to use API to programatically click on Allow button when the security pop-up appears.
Credits to SiddarthRout for the help.
Here is the LINK which will help you programmatically click on the Allow button.
Of course you have to tweak it a bit.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
L42
  • 19,427
  • 11
  • 44
  • 68
  • 1
    I see Sid's answer below and you discussion beneath. Did you consider the following steps of doing the job: **1.** use Window task scheduler to run Outlook (just Outlook.Exe, not VBScript file). I think this would be possible in your OS. **2.** Use `Private Sub Application_Startup() event` in `ThisOutlookSesion`. **3.** In the event set `if statement` referring to time when your macro should start. `Call YourMacro` inside conditional statement. **Conclusion:** If step 1 run at certain time which is checked in step 3, I think, you get what you need. – Kazimierz Jawor Jan 17 '14 at 09:02
  • @KazJaw thanks for taking the time. But this isn't possible because i have other task scheduled which requires OL to run always. but i'll check if i can set a timing. – L42 Jan 17 '14 at 09:21
  • 1
    does it mean that you have your OL application (almost) always running? then you could (possibly) use Outlook task to do the job (not Windows System task). – Kazimierz Jawor Jan 17 '14 at 09:29
  • yes, always running. and yes haven't tried that :D – L42 Jan 17 '14 at 09:42

3 Answers3

4

Tried & Tested!

Assuming that you have Outlook Application always running (according to comment below your question) you can do what you need in the following steps:

  1. add a new task in Outlook, set subject to: "run macro YourMacroName" and set time (plus cycles) when your macro should start.

  2. go to VBA Editor, open ThisOutlookSession module and add the following code inside (plus see the comments inside the code):

    Private Sub Application_Reminder(ByVal Item As Object)
    
    If TypeName(Item) = "TaskItem" Then
        Dim myItem As TaskItem
        Set myItem = Item
        If myItem.Subject = "run macro YourMacroName" Then
    
            Call YourMacroName    '...your macro name here
    
        End If
    End If
    End Sub
    
Kazimierz Jawor
  • 18,861
  • 7
  • 35
  • 55
  • Ok this works.:) but how do i dismiss the reminder. If it stays visible, other task won't run. Sorry, haven't deal with task :p also, if i dismiss it, will it still recur based on my settings? – L42 Jan 20 '14 at 03:44
  • 1
    Ok. Already sorted above comment :D. Thanks for the wonderful idea :D – L42 Jan 20 '14 at 07:04
3

Where will I put the procedure in Outlook, Module or ThisOutlookSession?

Neither. Paste the below code in a Text File and save it as a .VBS file. Then call this VBS file from the Task Scheduler as shown HERE

Dim olApp, olNS, olFolder, olDraft, strfoldername, i

Set olApp = GetObject(, "Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(6)

strfoldername = olFolder.Parent

Set olDraft = olNS.Folders(strfoldername).Folders("Drafts")

If olDraft.Items.Count <> 0 Then
    For i = olDraft.Items.Count To 1 Step -1
        olDraft.Items.Item(i).Send
    Next
End If
Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • so you're saying that a `call` is not possible? – L42 Jan 17 '14 at 07:16
  • No I am not saying that as I have not tested it. One moment. Let me recheck it. The above is a shortest way of doing it. – Siddharth Rout Jan 17 '14 at 07:17
  • ok, i'm doing this because my `Outlook` is having the security pop-up when sending a message was triggered from another program. Out of curiosity, i thought that what if i just call the procedure and `Outlook` still sends the mail, will the pop-up still trigger? Above works, but sending of mail was still triggered by `another` program. so the pop-up still appears. – L42 Jan 17 '14 at 07:19
  • I just tested it. You need to place the macto in `ThisOutlookSession`. Once you do that then you can call that macro in outlook using `Application.SendMail`. But this technique seems to be unsupported outside Outlook VBA. Researching more... – Siddharth Rout Jan 17 '14 at 07:29
  • Ok I did a little research and it seems that it doesn't support Application.MyMacro from vbscript. – Siddharth Rout Jan 17 '14 at 07:44
  • yeah, I also came up with the same conclusion. I just did try posting it hoping that somehow, somebody has done the same. I'll leave it here for a while :D – L42 Jan 17 '14 at 07:50
  • Sure. Another link where an outlook MVP says it is possible. See [THIS](http://answers.microsoft.com/en-us/office/forum/office_2010-outlook/how-to-run-outlook-macro-from-other-application/7568c68f-51fa-4687-aaee-f5b8103643c7) but it doesn't work for me. – Siddharth Rout Jan 17 '14 at 07:51
  • yep, i've included that in my post. :( that doesn't work. But I'm wondrin why `Application.ProcedureName` works inside `Outlook` while it doesn't in `Excel Vba` or `VBScript`. Didn't the `Set` cover it, passing the `Outlook` application object? – L42 Jan 17 '14 at 08:00
  • We can always get rid of the pop up either by reducing the macro security or by using APIs. – Siddharth Rout Jan 17 '14 at 08:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45443/discussion-between-l42-and-siddharth-rout) – L42 Jan 17 '14 at 08:36
1

If you are using Outlook 2007 or newer I have found you can easily eliminate the security pop up you mentioned above when running your script by doing the following:

  1. In Outlook 2007 Trust Center, go to Macro Security - Select "No security Check for macros"

  2. In Outlook 2007 Trust Center, go to Programatic Access - Select "Never warn me abous suspicious activity.

Of course that technically leaves you open to the remote possibility for someone to email you some malicious email script or something of that nature I assume. I trust my company has that managed though and this works for me. I can use VBS scripts in Outlook, Access, Excel to send emails with no security pop up.

Another Option:

If you don't want to do that, another option that has worked well for me prior to this is here: http://www.dimastr.com/redemption/objects.htm

Basically a dll redirect that does not include the popup. It leaves your other default security in place and you write \ call your VBA for it and send mail without the secutity pop-ups.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
NBSTL68
  • 11
  • 2