0
votes

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!

1
"...As Object" is pretty much appropriate for any object-type variable when using late binding. You'll also need to declare any constants you use from those libraries in your code, or use the numeric values directly.Tim Williams
for reg expressions, i am using a matchcollection. how can i find out the proper way to set that object? is there anywhere i can refer to to find out the createobject syntax?Monduras
Did you try "As Object" ? That should work. There's no "CreateObject" for that type.Tim Williams
@TimWilliams ty, that's very helpful. most of my subsequent variables work when i update them to as objects.Monduras

1 Answers

0
votes

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