I'm mostly trying to understand code that it's working:
Sub sendOutlookEmail()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "[email protected]"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
I learnt that it's best practice to create objects by early binding, as in
Dim oApp as New Outlook.Application
rather than by late binding, as in
Dim oApp as Outlook.Application
Set oApp = CreateObject("Outlook.application")
So I was trying to reduce the code.
1. Are both oApp and oMail objects?
Or is oMail a property or method of the oApp object (maybe a method that creates a new object)? If so:
Since oApp is an object Outlook.Application and oMail is an object such as oApp.CreateItem(olMailItem) I was trying to define straight away the oMail object by early binding like so:
Dim oMail as New Outlook.Application.CreateItem(olMailItem)
But that gives me a Syntax error. And the following:
Dim oMail as New Outlook.Application.olMailItem
Gives me a Type mismatch error. So:
2. Is there a way to create only the oMail, straight away?
Or do I have to create first the oApp object first anyway, in order to be able to create the oMail item (that is, another object dependent on the first)?
3. Is there any way to define the oMail object by early binding in just one line?
I'm new in programming, I hope I've explained myself properly and that my questions make some kind of sense heheh.
Have a nice day!
CodePudding user response:
- Yes, they are both objects.
- No, Only
Outlook.Application
object is creatable. All other objects (MAPIFolder
,MailItem
,Recipient
, etc). are retrieved from a call to other Outlook objects, (e.g.,Application.CreateItem
) - Unlimately, early binding vs late binding makes zero difference. Early binding gives you Intellisense and compile-time errors. It can also be slightly faster (not that you would notice that in VBA). Functionality-wise, the two are the same.
CodePudding user response:
There is no best practice when using early or late binding. Both approaches are valid and can be used. For example, in cases when you don't want or can't add a COM reference your choice would be the late-binding technology which allows declaring everything like object and keep calling properties and methods using a vtable. But at the same time the early-binding technology prevents developers from doing mistakes by adding COM references and using intellisense suggestions when coding and typing property and method calls. Moreover, it works faster than late-biding. You can read more about the two technologies in the Using early binding and late binding in Automation article. But that is your choice, not the best practice. If you don't have any arguments for the late-binding, I'd go with early-binding.
Both are COM objects. Most of the time COM objects are created by calling properties and methods of other objects when dealing with the Outlook object model, only the Outlook
Application
class is instantiated in the code. For example, a new MailItem instance can be created by calling theCreateItem
method. But also you may consider creating a newMailItem
instance in a specific folder. In that case you need to use theItems
collection (see theFolder.Items
property) which provides theAdd
method. It returns the new Outlook item.No, you must run Outlook (or create a new instance programmatically or connect to the existing one) to be able to create a new
MailItem
object in the code.You can declare the object in a single line of code. But you need to instantiate it somewhere. And that doesn't depend on the late or early binding technologies. The principle is the sample. To see the difference between the two let's consider the following:
In the case of late-binding technology used the declaration may look like that:
Dim mailItemObj as Object
In the case of early-binding technology that looks in the following way:
Dim mailItemObj as Outlook.MailItem
That's it!
P.S. You may find the How to create and show a new Outlook mail item programmatically: C#, VB.NET article helpful.