I recently encountered a VBA problem related to the below commonly used and bullet proof VBA code for either controlling or opening an Outlook session from VBA. This was originally based on Ron de Bruin code which allows you to create an email from Excel VBA and use spreadsheet data to populate it or an image of a range or as an attachment. Below is a snippet of this.
BTW Ron de Bruin VBA code has always been invaluable for me over many years is now being kindly hosted on Jan Karel Pieterse’s equally invaluable site. Ron has removed his Windows VBA content from his site https://jkp-ads.com/rdb/ .
Dim OutApp As Object
Dim OutlookApp As Outlook.Application
Dim OutMail As Object
Dim strbody As String
Dim MakeJPG As String
Dim sRng As String
Dim Rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo 0
End If
Set OutMail = OutApp.CreateItem(0)
But it was failing at the CreateObject(“Outlook.Application”) line IF Outlook was already open.
Actually although it halted at CreateObject, it really ‘errored’ at GetObject(,”Outlook.Application”) which On Error Resume Next masked. It couldn’t control the open session of Outlook for reasons I later discovered below, then halted when it tried to open a new session as Outlook that was already open. If Outlook was not open, the above code was working fine. By the by sometimes Outlook seems to be stuck in the Task Manager even if you have closed it and needs an End Task to be sure it’s closed. Maybe this part is just an issue at my end.
OK back to the VBA code problem. I wondered if there was something incompatible with the latest version of Outlook. BTW I am talking about the classic Outlook not the new Outlook you can switch to as of Nov 24. The new Outlook is definitely at the moment incompatible with Excel VBA and they even have a error message saying this I found in blog posts.
But after a lot of Googling relatively recent posts, people still seem to be happily using this code. So why I am I having this problem?
Then a gem within this Stackoverflow post from Eugene emerged…
https://stackoverflow.com/questions/75649894/vba-excel-and-outlook-not-communicating