The question is simple, yet hard to achieve for me for some reason. How can I get to fire the quit/close event when outlook 2007 is being closed?
I want do display a Yes/No msgbox in VBA which executes code depending on the option chosen when outlook is being closed.
I thought I had the solution using:
Dim WithEvents myOlApp As Outlook.Application
Sub Initialize_handler()
Set myOlApp = CreateObject("Outlook.application")
End Sub
Private Sub myOlApp_Quit()
MsgBox "TEST"
End Sub
First I tried to insert it into my Module but this gave me the Only valid in object modules error. So then I created a new class module and pasted the code in here (which gave no errors) but still the event wont fire. What is going wrong and how to fix it?
Set myOlApp = CreateObject("Outlook.application")
There is no need to create a new Outlook instance. You should use the Application property availble in Outlook VBA.
Private Sub Application_Quit()
MsgBox "Goodbye, " & Application.GetNamespace("MAPI").CurrentUser
End Sub
Take a look at the Getting Started with VBA in Outlook 2010 article in MSDN.
Related
I have a problem with my macros on outlook.
I am currently trying via a batch to call outlook and pass it as a parameter the name of a macro that I get via an environment variable I've set in my batch. However I do get the name of my macro, but the process stops at the time of the Call function. Could someone tell me the right way to proceed?
VBA ThisOutlookSession
Private Sub Application_Startup()
Dim strMacroName As String
strMacroName = CreateObject("WScript.Shell").Environment("process").Item("MacroName")
'MsgBox strMacroName
'MsgBox VarType(strMacroName)
If strMacroName <> "" Then Call strMacroName
End Sub
VBA Modules
Option Explicit
Sub macro1()
MsgBox "macro1"
End Sub
Sub macro2()
MsgBox "macro2"
End Sub
Batch
Set WorkingPath=C:\Temp\Outlook
Set MacroName=%1
start OUTLOOK.EXE
Set MacroName=
Set WorkingPath=
the result
There are several aspects here... The first point is possible security issues when dealing with the Outlook. You can read more about that in the Security Behavior of the Outlook Object Model article.
Another point is that you can call VBA macros declared in the ThisOutlookSession module in the following way (for example, from any other Office application):
Sub test()
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
OutApp.HelloWorld
End Sub
Where the HelloWorld sub is declared in the ThisOutlookSession module in following way:
Option Explicit
Public Sub HelloWorld()
MsgBox "Hello world !!"
End Sub
Note, you may call any module from the ThisOutlookSession module. There is no need to get access to other modules directly.
I set up a web calendar in office 365 which is connected with "Data Sourse" with Access 2016. Also at the same time the local Outlook is "looking" on the web folder of calendar (outlook at office 365). All three Access (i.e. linked table)-web calendar-local calendar synchronize correctly when adding, changing or removing an appointment.
I would like to "trap" all of the above events and use in my database.
for that reason I added module with ModOutlookRespond with code:
Public Sub Initialize_handler()
Set m_olapp = GetObject(, "Outlook.Application")
m_olapp.GetNamespace("MAPI").Logon "Microsoft Outlook"
Set m_olNameSpace = m_olapp.GetNamespace("MAPI")
'this allows us to capture an appoitment item change event
Set m_olAppoitmentItems = m_olNameSpace.GetDefaultFolder(olFolderCalendar).Items
Debug.Print "Connection Initialised"
End Sub
and added Initialize_handler() on the Open event of Main form.
On the Main form I added also the code:
Public WithEvents m_olapp As Outlook.Application
Public WithEvents m_olAppoitments As Outlook.Items
Public m_olNameSpace As Outlook.NameSpace
Private Sub m_olAppoitments_ItemAdd(ByVal Item As Object)
Debug.Print "Item has added" & Item.Subject
End Sub
I am getting so far only the Debug.Print "Connection Initialised" but not a single time any message from Add event (or any other I tried).
Any ideas what I can try?
I want to perform some text replacements that are just too complicated for Automatic replacement feature of Microsoft outlook. Microsoft has rather extensive documentation on Microsoft Outlook VBA scripting hosted on GitHub.
I started by pressing Alt+F11 which opened Visual Basic Editor. This is what I see in the project explorer:
Please note that I am not VBA programmer, so what I did afterwards probably looks stupid. I figured out that OutlookOutlook namespace represents all Outlook classes, so I just picked one of the classes that sounds like a text field:
Dim WithEvents textField As Outlook.OlkTextBox
And I created a dummy event that should fire before change in text field:
Private Sub textField_BeforeUpdate(Cancel As Boolean)
MsgBox "Nope!"
Cancel = True
End Sub
Of course, when start outlook and edit message, the script doesn't get executed. So I suppose that for starter, I need to get a refference to actual text editor. How do I do that?
My question basically is, how to get this code execute when I try to edit any outlook message:
MsgBox "Nope!"
Cancel = True
Try the write event.
Private WithEvents myItem As MailItem
Private Sub Application_Startup()
Set myItem = ActiveInspector.CurrentItem
End Sub
Private Sub myItem_Write(Cancel As Boolean)
MsgBox "Nope!"
Cancel = True
End Sub
I have a rule in Outlook which sends a daily email into a particular folder. I then have a VBA script which upon noticing a new unread message in that folder goes in and saves the attachment to a folder on my hard drive and does a few other formatting type things (on the attachment).
I then just linked up the script to the rule in the Outlook rules wizard so it runs as a package.
The problem is as follows: the script is kicked off BEFORE the message is sorted into the appropriate folder. In reality it should run after the message is sorted (otherwise there is nothing for it to act upon). Any ideas on how to rectify?
The code currently begins as follows:
sub saveattachment()
Should it be this instead?
private sub saveattachment()
or
public sub saveattachment()
Would it be better to have the "rule" embedded in the macro instead and then just run it as a private sub anytime the daily email appears in my Inbox?
If you need to assign a VBA macro sub to the Outlook rule, the VBA sub should look like the following one:
Public Sub Test(mail as MailItem)
' your code goes there
End Sub
An instance of the MailItem class is passed as a parameter and stands for the email arrived to the Inbox.
But in case if you need to be sure that your code is triggered when a mail is moved to a particular folder you need to handle the ItemAdd event of the Items class which comes from that folder. Be aware, the event is not fired when more than 16 items are added to the folder simultaneously.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Dim myOlAtts As Outlook.Attachments
Set myOlMItem = myOlApp.CreateItem(olMailItem)
myOlMItem.Save
Set myOlAtts = myOlMItem.Attachments
' Add new contact to attachments in mail message
myOlAtts.Add Item, olByValue
myOlMItem.To = "Sales Team"
myOlMItem.Subject = "New contact"
myOlMItem.Send
End Sub
Finally, you may find the Getting Started with VBA in Outlook 2010 article helpful.
This question already has answers here:
How do I trigger a macro to run after a new mail is received in Outlook?
(2 answers)
Closed 5 years ago.
Win 7, Outlook 2013 I use VBA code that takes an action on some of the files that arrive in my inbox. However, I have to click/run button to run this macro.
Is there a way that this code could run automatically when an email arrives?
I have tried an Outlook rule to run the script but not successful.
I tried this, but this works only when once I run the macro
Private Sub Application_NewMail()
Call GetAttachments_From_Inbox (My Macro)
End Sub
I specifically use a script I run as a rule, applied to all messages. This gives you easy and clear access to the mail item you receive. Since you want to run this on each message setting up WithEvents on your inbox is probably your best bet.
For example:
You can create a listeners for an Outlook folder as follows:
Private WithEvents mainInboxItems As Outlook.Items
Public Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set mainInboxItems = objNS.Folders("whatever your main mailbox is called").Folders("AssignNumber").Items
'assumes your "AssignNumber" folder is a subfolder of the main inbox
'otherwise you can nest Folders("myArchive").Folders("AssignNumber).items etc
End Sub
You can do this for as many folders as you want, too.
You can then assign the ItemAdd method to each of them like:
Private Sub mainInboxItems_ItemAdd(ByVal item As Object)
'do Stuff to mailitem
Call GetAttachments_From_Inbox (item)
End Sub
All this code can go in ThisOutlookSession.