Is it possible to run a VBA function whenever Outlook starts up? - vba

I would like to have a VBA function run as soon as Outlook starts up. Is this possible and if so, what do I need to do? My searches on Google have failed me.
I don't mind that the security alert will pop up.

Use the Application_Startup event in ThisOutlookSession:
Private Sub Application_Startup()
MsgBox "Foo"
End Sub

Have you looked at the Application_Startup() event?

Related

Outlook VBA Type mismatch (error 13) when initializing handler for Sent Mail folder

This is my first thread on the forum so I hope i do everything as is needed.
Since a few years we use a vba outlook macro at our firm to send our sent mails to a folder. The folder is specified when pressing the SEND button. When we created the macro, we used to save the file directly after pressing send, but then only a draft file was saved to the specified folder. Herefore we initiated a handler to check when the mail is added to the Sent Mail folder so we could save this mail to the specified folder. Since yesterday, only some of my colleagues started getting a Type mismatch error while running this macro. Today another couple of colleagues is having this issue as well. Most of us can still use the macro without having any errors.
I narrowed the problem down to the initialize_handler itself by creating a much easier macro. So now when sending any email, I initialize a handler for the items in my Sent Mails folder. When an item is added to the Sent Mails folder, a messagebox should appear telling me an item is added.
The type mismatch error is created on "Set myOlItems = ...."
Public WithEvents myOlItems As Outlook.Items
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Call Initialize_handler
End Sub
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal ObjectSent As Object)
MsgBox "Item is added to Sent Mails"
End Sub
I hope someone has a solution to this. I wasn't able to find out what could trigger the error?
Thanks!
I'd recommend starting from breaking the chain of property and methods calls by declaring each property or method call on a separate line of code. So, we will be able to find out which property or method calls fails.

Outlook explorer activate handler--DoEvents causes Outlook window to maximize

I have a macro that runs when an explorer is activated. I discovered that if I put a DoEvents function/statement in the macro, then any time I use another app (say, a browser) and then click back on the main Outlook (i.e., explorer) window title bar, the Outlook window maximizes, as if I had double-clicked on it.
If I comment DoEvents out, the window behaves normally.
This behavior occurs even when DoEvents is the only statement in the Activate macro.
The macro runs as expected when the Activate event occurs, but the window state changes for no apparent reason if DoEvents is present.
Is this a known issue?
Thanks!
==== EDIT =====
If I run the following code in ThisOutlookSession, the strange window behavior occurs:
Private WithEvents my_x As Explorer
Private Sub Application_Startup()
Set my_x = Application.ActiveExplorer
End Sub
Private Sub my_x_Activate()
DoEvents
End Sub
In addition, clicking once on an item in an explorer when Outlook does not have the focus causes the item to open, as if double-clicked. Plus occasional other strange behaviors.
I am using Outlook 2013 in Win10.
There is absolutely no reason to use DoEvents. Ever. You might be stealing some of the Windows messages that Outlook itself expected to handle.
First of all, I'd suggest scanning your machine for viruses.
Then I'd recommend checking the list of running add-ins in Outlook. You may try to turn them off and see how Outlook works after.
There is no need to use the DoEvents in the Activate event handler.

Automatically save draft email

I'm looking for VBA code for Outlook 2013 that will automatically save a draft either when opening the composition window, when replying or forwarding an email (it's OK if it fires on newly-composed messages also) or just before sending it when hitting the Send button.
Essentially emulating hitting the "Save" icon or Ctrl-S either before composing or before the message gets sent.
Outlook generates a "Could not complete the operation because the mail provider does not support it" error when replying to or forwarding a non-Google Apps account email when Google Apps accounts are combined with other email accounts in Outlook.
I discovered that a work-around is to manually save the email before sending it. I can do that manually but often forget. Can't do it after the error message appears. Trying to save after the "could not complete" error is thrown results in another error.
BTW, I already have a routine running off the Application_ItemSend event; will adding another conflict?
Update: Here is a version of what I tried:
Private Sub Application_ItemSend(ByVal item As Object, Cancel As Boolean)
' Runs when the item is being sent.
Call AutoSaveDraft(item)
Call CheckSubject(item)
End Sub
(CheckSubject fires)
Private Sub AutoSaveDraft(item As Object)
item.Save
SendKeys "+{F12}" ' Save the message. ##
SendKeys "^S"
Sleep 1000 ' Pause execution 1 second. The Windows sleep function is called in the beginning ##
End Sub
(Yes, I have all three potential save methods. Have also tried them individually.
You are free to call the Save method in the ItemSend event handler if it helps to avoid issues.
You may find the Getting Started with VBA in Outlook 2010 article helpful.
Since the original poster doesn't make it clear, this is what actually works to prevent the errors related to non-Google Apps accounts when simply hitting Send:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Item.Save
End Sub

Make the Userform in Outlook stay on the screen - VBA

So I have created a VBA Macro embedded withing Outlook. The code runs a userform.
When I run my macro:
Currently:
When I minimize Outlook, my userform also minimizes.
What I Want:
When I minimize outlook I want my userform to stay on the screen.
Any ideas on how to achieve this? I am using vbModeless to display my userform, as i want the user to access the outlook content while the userform is running.
Try adding this to your code.
Private Sub UserForm_Initialize()
Dim olapp As Object
Set olapp = GetObject(, "Outlook.Application")
olapp.ActiveWindow.WindowState = 1
End Sub
This will minimize Outlook the moment userform is shown.
Not exactly what you want (what you describe is a bit complicated), but nearly the same effect.

How to add MS outlook reminders event handlers with VBA

I want to extend MS Outlook so that when a calendar reminder pops up, I can run a VBA hook that can run an external program (like a batch script). In my case, I want to "forward" the reminder to a Linux desktop since I work in both environments and I don't always have the Windows desktop visible.
I see an example at http://office.microsoft.com/en-us/outlook-help/HV080803406.aspx and have opened VBA Developer view in MS outlook 2010 and inserted a class module and added that VBA code, but I do not see how to activate this code - when a reminder pops up, this code is not activated.
Update
Here is what I ended up adding to Outlook's ThisOutlookSession to run an external batch script when a reminder pops up.
Public WithEvents objReminders As Outlook.Reminders
Private Sub Application_Startup()
Set objReminders = Application.Reminders
End Sub
Private Sub objReminders_ReminderFire(ByVal ReminderObject As Reminder)
Cmd = "C:\path\to\my\reminder-hook.cmd" & " " & ReminderObject.Caption
Call Shell(Cmd, vbHide)
End Sub
Put it in the "ThisOutlookSession" module and restart Outlook.
Also, ensure that macros are enabled in Outlook settings.