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!
Dim oApp as ObjectLate binding would imply the exact type ofoApp(for example) is not known until runtime. Early/late binding is about how your objects are declared, not when they're instantiated.