VBA for emailing – take care if Outlook is run as Administrator

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. 

VBA triggered Outlook error message

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

if Outlook is run with admin privileges (Run As Administrator) and Excel was run under a regular user account without admin privileges – these two applications will never find each other (Excel will not be able to run the second Outlook instance, nor connect to the already running under a different security context). You need to make sure that Outlook is not running or run Excel under the same security context with the Outlook process.
 
Ah ha, that sounds like my problem as I somewhere along the line did change my Outlook to be run as Administrator to overcome a problematic Add-In but have since forgot about it.
 
To check if your Outlook is run with Administrator rights,  with Outlook closed and holding down the SHIFT key, then right clicking on the Outlook application icon, choose Properties / Advanced and this dialog below shows and sure enough this was the problem.  Once unticked, all was ok, the VBA code ran fine and a frustrating problem over a day or so was solved!kid jumping with joy at beach
How to set Outlook to run as administrator or not