how to late bind various applications in outlook - vba

I have a complex vba macro that uses the following references. I don't know how to late bind all of these properly to the different objects I use. How can I research what is the proper late binding convention?
Outlook 16.0
Word 16.0
Regular Expressions 5.5
For example, I am declaring the following variables in my macro using early binding, how can I find the equivalent late binding convention?
dim myNameSpace as outlook.namespace --> set myNameSpace = Application.GetNamespace("MAPI")
dim myInbox as outlook.folder --> set myInbox = myNameSpace.Folders("name of folder")
dim myItems as outlook.items --> set myItems = myInbox.Items
dim wrdDoc as word.document
dim regExp as new RegExp --> regExp.Pattern = "pattern"
Any help or guidance would be appreciated!
thank you!

The late binding makes sense when you automate Outlook from other Office applications and don't want to keep a reference to the object library.
When we use late binding, we don't have to add reference for the VBA project. In that case you should declare all your Outlook-specific objects as Object:
dim myNameSpace as Object
set myNameSpace = OutlookApplication.GetNamespace("MAPI")
Use the CreateObject method to create a new application instance:
Set olApp = CreateObject("Outlook.Application")
This will make each computer create the Outlook Application object from the Outlook library that is installed on it. It avoids you to set an explicit reference to Outlook object library in any Office application or solution that you will distribute (remove that reference from the project before distributing it).

Related

Create email, based on Outlook template, from a Word menu

This is the code for other Word templates on the menu.
Private Sub "button name_Click()
Unload ####Menu
End Sub
This is code I've seen to create an Outlook item from Word.
Sub CreateFromTemplate()
Dim MyItem As Outlook.MailItem
Set MyItem = Application.CreateItemFromTemplate("C:\statusrep.oft")
MyItem.Display
End Sub
Sub CreateTemplate()
Dim MyItem As Outlook.MailItem
Set MyItem = Application.CreateItem(olMailItem)
MyItem.Subject = "Status Report"
MyItem.To = "Dan Wilson"
MyItem.Display
MyItem.SaveAs "C:\statusrep.oft", OlSaveAsType.olTemplate
End Sub
How do I combine these?
It seems you just need to automate Outlook from Word VBA. To start an Outlook Automation session, you can use either early or late binding. Late binding uses either the Visual Basic GetObject function or the CreateObject function to initialize Outlook. For example, the following code sets an object variable to the Outlook Application object, which is the highest-level object in the Outlook object model. All Automation code must first define an Outlook Application object to be able to access any other Outlook objects.
Dim objOL as Object
Set objOL = CreateObject("Outlook.Application")
To use early binding, you first need to set a reference to the Outlook object library. Use the Reference command on the Visual Basic for Applications (VBA) Tools menu to set a reference to Microsoft Outlook xx.x Object Library, where xx.x represents the version of Outlook that you are working with. You can then use the following syntax to start an Outlook session.
Dim objOL as Outlook.Application
Set objOL = New Outlook.Application
Most programming solutions interact with the data stored in Outlook. Outlook stores all of its information as items in folders. Folders are contained in one or more stores. After you set an object variable to the Outlook Application object, you will commonly set a NameSpace object to refer to MAPI, as shown in the following example.
Set objOL = New Outlook.Application
Set objNS = objOL.GetNameSpace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderContacts)
Once you have set an object variable to reference the folder that contains the items you wish to work with, you use appropriate code to accomplish your task, as shown in the following example.
Sub CreateNewOutlookMail()
Dim objOLApp As Outlook.Application
Dim NewMail As Outlook.MailItem
' Set the Application object
Set objOLApp = New Outlook.Application
' You can only use CreateItem for default items
Set NewMail = objOLApp.CreateItem(olMailItem)
' Display the new mail form so the user can fill it out
NewMail.Display
End Sub
See Automating Outlook from a Visual Basic Application for more information.

Late binding creating object VBA

I would like to use late binding to use the following reference :
Name : "Microsoft Windows Common Controls 6.0 (SP6)"
Filepath : "C:\Windows\System32\MSCOMCTL.OCX"
GUID : "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}"
I understood thanks to this topic the difference between early and late binding:
This is early binding: Dim olApp As Outlook.Application Set olApp =
New Outlook.Application
And this is late binding: Dim olApp As Object Set olApp =
CreateObject("Outlook.Application")
However, I do not know how to find my object name (see bold quoted text above). Every example on the internet uses Powerpoint or Outlook.application.
Any help? Thanks

How to do late binding in VBA?

I have a function that creates an email via VBA.
I made this through Excel 2016. When some of my colleagues try to use it there an error of missing references (Outlook Library 16.0).
I looked in the internet for solutions and found the best is Late Binding. I have read about it but I don't understand how to make it work in the following example code.
Sub EscalateCase(what_address As String, subject_line As String, email_body As String)
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = subject_line
olMail.BodyFormat = olFormatHTML
olMail.HTMLBody = email_body
olMail.Send
End Sub
This is early binding:
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
And this is late binding:
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
Late binding does not require a reference to Outlook Library 16.0 whereas early binding does. However, note that late binding is a bit slower and you won't get intellisense for that object.
As Callum pointed out, late binding involves changing your application reference to an object and not setting a reference to the library.
Without a reference Excel doesn't know anything about Outlook until runtime. This also means that not only will intellisense not work, the constant names for values in Outlook won't work either.
e.g. In Outlooks Immediate window if you type Application.CreateItem( you'll get a whole load of item types pop up to choose from. olContactItem for instance.
Excel hasn't a clue what olContactItem means - it's an Outlook constant that only Outlook or an application with a reference to Outlook understands.
In Outlooks immediate window type ?olContactItem and it will return 2. That's the number you need to use instead of the constant name.
So your code changes from
Application.CreateItem(olContactItem) to olApp.CreateItem(2)
You need to do this throughout your code.
In that situation I generally define the constant globally in a module like this, that way you preserve the descriptive value of the variable.
Public Const olFormatHTML = 2

MailItem.SaveAs when referencing with EntryID

I'm developing a drag and drop of a MailItem from Outlook (I know it's a MailItem and not any other type) into an Access memo field. Trying to call SaveAs on a MailItem object.
I get
Error 287 - Application-defined or object-defined error.
I've tried using the namespace, not using the namespace, using .Item, etc.
Here's my current code:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
Dim olMail As Outlook.MailItem
Set olMail = olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
olMail.SaveAs strPathAndFile, Outlook.OlSaveAsType.olMSG
Access 2010, Outlook 2010 both 32 bit. Win 7 machine is 64 bit.
Tried it on an all 32-bit machine, same error.
Tried Dmitry's code below, same error.
Finally got this working using Dmitry Streblechenko's awesome Redemption library. Thanks!
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
Dim oRDOSession As Redemption.RDOSession
Set oRDOSession = CreateObject("Redemption.RDOSession")
oRDOSession.MAPIOBJECT = olNs.Application.Session.MAPIOBJECT
If Not oRDOSession.LoggedOn Then oRDOSession.Logon
Dim oMsgItem As Redemption.RDOMail
Set oMsgItem = oRDOSession.GetMessageFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
strPathAndFile = "some\path\" & UniqueValueStr(Now()) & ".msg"
oMsgItem.SaveAs strPathAndFile
Why do you need to use Redemption?
I don't see any difference between using the OOM and Redempton.
Why do you need to use the GetItemFromID method in the code?
olNs.GetItemFromID(olNs.Application.ActiveExplorer.Selection(1).EntryID)
You have already got a reference to the selected object in the explorer window:
olNs.Application.ActiveExplorer.Selection(1)
Also I'd suggest breaking the chaing of calls and declaring each property or method call on a separate line.

In Outlook 2013, can a macro open a new custom form?

In Outlook 2013 using Developer tab -> Design a Form, I created a custom form (with no mods yet) from the delivered Message form and placed it in my Personal Forms Library. Outlook tells me that the Message class is: IPM.Note.MyForm
I've created a macro and set up a new ribbon button to run the macro. I would like the macro to open a new instance of my custom form, but I can't get it working.
With the following code I can get the macro to open a new instance of the delivered Message Form:
Set newItem = Application.CreateItem(olMailItem)
newItem.Display
Set newItem = Nothing
I can't get it to open my custom form. I've tried the following as arguments to CreateItem: olMailItem.MyForm and IPM.Note.MyForm.
The macro editor intellisense has about 9 options for arguments to CreateItem, all of them appear to be delivered objects/forms, and it errors if one of these options aren't used.
I've done very little vba and office macros, is there some way to get this macro to open my custom form? Thanks.
See Items.Add http://msdn.microsoft.com/en-us/library/office/ff861028(v=office.15).aspx
Sub AddForm()
Dim myNamespace As outlook.NameSpace
Dim myItems As outlook.Items
Dim myFolder As outlook.Folder
Dim myItem As outlook.MailItem
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set myItems = myFolder.Items
Set myItem = myItems.Add("IPM.Note.MyForm")
End Sub