Late binding creating object VBA - 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

Related

Why does VBA code fail when setting a new instance of an Outlook Application object?

The first sub routine runs fine when called upon application startup, while the second does not bring up Outlook and instead takes a minute or two, culminating in an error. The only difference is that in the "set" statement, I put the word "New". I am aware that this means it is trying to open up a new instance of the Outlook client, while one is already running, but why wouldn't there be a way for the Outlook to deal with the request for a new application while one is already running, or use the new instance instead of the existing instance?
Sub SaveAttachment1_Initialize()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox)
End Sub
Sub SaveAttachment1_Initialize()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox)
End Sub
If you are running in Outlook VBA, that line must be
Set olApp = Application
If not, the second one is correct. The first one makes no sense - you are assigning a variable to type (?).

how to late bind various applications in outlook

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).

Late Binding Outlook.Folder

I am having issues with the Microsoft Outlook Object Library, when working with two different Office versions (2007 and 2016).
I already found out that late binding is a solution here, but I don't know how to late bind other objects than Outlook.Application. Especially "Outlook.Folder" and "Outlook.Selection" is what I need for late binding.
I tried creating objects just like I did for my Outlook.Application called "appoutlook" but the code below keeps giving me the error message that the object creation via ActiveX components is not possible...
1st class:
Public Type Email_Attributes
olApp As Object
olFolder As Object
olSelection As Object
End Type
2nd class:
Dim olMail As Object
Dim appoutlook As Object
Set appoutlook = CreateObject("Outlook.Application")
Const olMailItem As Long = 0
Set olMail = appoutlook.CreateItem(olMailItem)
Dim strAttName As String
Dim new_mail As Email_Attributes
Application.ScreenUpdating = False
Set new_mail.olApp = CreateObject("Outlook.Application")
Set new_mail.olFolder = CreateObject("Outlook.Folder")
Set new_mail.olSelection = CreateObject("Outlook.Selection")
Set new_mail.olFolder = GetObject("ActiveExplorer.CurrentFolder")
Set new_mail.olSelection = GetObject("ActiveExplorer.Selection")
Any help is much appreciated!

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.