
In methode A wordt er gebruik gemaakt van een vroege binding en in methode B van een late binding.
Het voornaamste verschil zit in de declaratie en initialisatie van de Outlook objecten.
| Methode A | Methode B |
| Vroege binding | Late binding |
| Dim objOutlook As Outlook.Application | Dim objOutlook as Object |
| Dim objMail As Outlook.MailItem | Dim objMail As Object |
| Set objOutlook = New Outlook.Application | Set objOutlook = CreateObject("Outlook.Application") |
| Set objMail = objOutlook.CreateItem(olMailItem) | Set objMail = objOutlook.CreateItem(0) |
Het binden (koppelen) beoogt het binden van de ene applicatie met de andere applicatie. In dit geval dus Excel met Outlook.

Wanneer deze methode bij een oudere versie van Excel wordt uitgevoerd, zal deze een fout opleveren (zie rechter afbeelding).
In methode B wordt de koppeling @ runtime tot stand gebracht. Deze methode kan langer duren. Gezien Outlook als (generiek) object wordt gedeclareerd kan de IntelliSense niet gebruikt worden en zullen fouten niet specifiek worden aangegeven. Het grote voordeel is wel dat er geen aparte verwijzing gemaakt hoeft te worden zoals bij methode A. Hierdoor kan deze methode ten alle tijden versie-onafhankelijk worden gebruikt.
'-----------------------------------------------------------------------------------------------------------------------
' Auteur : pascalterheege.nl
' Datum : 22-2-2016
' Object : modOutlook
' Doel : twee verschillende methoden voor het aanroepen van een andere Office applicatie
' Bron : https://support.microsoft.com/nl-nl/kb/245115
' http://nieuwsbrief.helpmij.nl/artikel.php?id=2539
'-----------------------------------------------------------------------------------------------------------------------
Option Explicit
'-----------------------------------------------------------------------------------------------------------------------
' Datum : 22-2-2016
' Type : Subroutine
' Verwijzing : Microsoft Outlook X Object Library
' Opmerking : vroege binding
'-----------------------------------------------------------------------------------------------------------------------
Sub methodeA()
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim strAan As String
Dim strCC As String
Dim strOnderwerp As String
Dim strBericht As String
Set objOutlook = New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)
strAan = [B1]
strCC = [B2]
strOnderwerp = [B3]
strBericht = [B4]
With objMail
.To = strAan
.CC = strCC
.Subject = strOnderwerp
.Body = strBericht
.Display
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
'-----------------------------------------------------------------------------------------------------------------------
' Datum : 22-2-2016
' Type : Subroutine
' Opmerking : late binding
'-----------------------------------------------------------------------------------------------------------------------
Sub methodeB()
Dim objOutlook As Object
Dim objMail As Object
Dim strAan As String
Dim strCC As String
Dim strOnderwerp As String
Dim strBericht As String
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
strAan = [B1]
strCC = [B2]
strOnderwerp = [B3]
strBericht = [B4]
With objMail
.To = strAan
.CC = strCC
.Subject = strOnderwerp
.Body = strBericht
.Display
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
Download hier het oefenbestand.
Gezien de MAC een totaal ander besturingssysteem heeft zullen de Windows oplossingen niet werken. Deze verwijzen namelijk naar specifieke Windows gerelateerde (ActiveX) onderdelen.
Om Outlook toch aan te kunnen roepen wordt gebruikt gemaakt van het script RDBMacOutlook.scpt en de functie MacExcelWithMacOutlookPDF. Deze onderdelen zijn beiden ontwikkeld door Ron de Bruin.
Option Explicit
Sub verstuurPDF()
'-----------------------------------------------------------------------------------------------------------------------
' Auteur : pascalterheege.nl
' Datum : 22-1-2021
' Doel : huidige werkblad als bijlage toevoegen aan een nieuwe email
' Opmerking : plaats RDBMacOutlook.scpt in de juiste locatie, zie onderstaande link
' Bron : https://macexcel.com/examples/mailpdf/macoutlook/ (Ron de Bruin)
'-----------------------------------------------------------------------------------------------------------------------
Dim strPDF As String
Dim strHTML As String
Dim strBody As String
Dim shWerkblad As Worksheet
Set shWerkblad = ActiveSheet
strPDF = MacScript("return POSIX path of (path to desktop folder) as string") & "test.pdf"
With shWerkblad
' Schakel zoom uit om te voorkomen dat de layout ongewenst wordt gewijzigd
.PageSetup.Zoom = False
.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=strPDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False
End With
' Maak een HTML bestaand aan - in dezelfde locatie - voor standaard e-mail tekst
strHTML = ThisWorkbook.Path & Application.PathSeparator & "Tekst.html"
Open strHTML For Input As #1
strBody = Input(LOF(1), #1)
Close #1
MacExcelWithMacOutlookPDF _
subject:="Test e-mail", _
mailbody:=strBody, _
toaddress:="info@pascalterheege.nl", _
ccaddress:="", _
bccaddress:="", _
displaymail:="yes", _
accounttype:="", _
accountname:="", _
attachment:=strPDF
End Sub
Function MacExcelWithMacOutlookPDF(subject As String, mailbody As String, _
toaddress As String, ccaddress As String, _
bccaddress As String, displaymail As String, _
accounttype As String, accountname As String, _
attachment As String)
'Ron de Bruin : 11-Dec-2020 (renamed the function)
'https://macexcel.com/examples/mailpdf/macoutlook/
' Change the displaymail argument to string instead of boolean
Dim ScriptStr As String, RunMyScript As String
ScriptStr = subject & ";" & mailbody & ";" & toaddress & ";" & ccaddress & ";" & _
bccaddress & ";" & displaymail & ";" & accounttype & ";" & _
accountname & ";" & attachment
'Call the RDBMacOutlook.scpt script file with the AppleScriptTask function
RunMyScript = AppleScriptTask("RDBMacOutlook.scpt", "CreateMailInOutlook", CStr(ScriptStr))
'Delete the file we just mailed
Kill attachment
End Function

