When using a VBA code in excel to open outlook. If outlook is already open a new email window opens as it should be. If outlook is closed. I get a error.

Anonymous
2023-10-13T17:49:34+00:00

When using a VBA code in excel to open outlook. If outlook is already open a new email window opens as it should. If outlook is closed. I get a error. with a notification on the outlook icon. "Another program is using Outlook. To disconnect programs and exit Outlook, click the Outlook icon and then click Exit Now."

I can only proceed if I end outlook through task manager.

I have been using chat GPT etc to try and solve the issue.

This is the first business version of excel I have owned, through switching between personal and business causes the same issue.

The current VBA code I am using is as follows:

Sub SendEmail()

Dim OutlookApp As Object 

Dim mItem As Object 

Dim Cell As Range 

Dim Subj As String 

Dim EmailAddr As String 

Dim Body As String 

Dim cc As String 

Dim bcc As String 

' Check if Outlook is already running 

On Error Resume Next 

Set OutlookApp = GetObject(, "Outlook.Application") 

On Error GoTo 0 

' If Outlook is not running, create a new instance 

If OutlookApp Is Nothing Then 

    Set OutlookApp = CreateObject("Outlook.Application") 

End If 

EmailAddr = Range("B1") 

cc = Range("B2") 

bcc = Range("B3") 

Subj = Range("B4") 

Body = Range("B6") 

' Create a new mail item 

Set mItem = OutlookApp.CreateItem(0) 

' Assign values to the mail item properties 

With mItem 

    .To = EmailAddr 

    .Subject = Subj 

    .Body = Body 

    .cc = cc 

    .bcc = bcc 

    .Display ' or .Send to send the email 

End With 

ExitPoint:

Set OutlookApp = Nothing 

Set mItem = Nothing 

End Sub

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. OssieMac 48,001 Reputation points Volunteer Moderator
    2023-10-14T20:37:13+00:00

    Have done some more research on this issue. Before setting the outlook object variable to nothing, close Outlook first like the following. I'll be interested in the outcome.

    OutlookApp.Quit 
    
    Set mItem = Nothing 
    
    Set OutlookApp = Nothing
    

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-10-14T14:43:18+00:00

    Thanks for your reply.

    I spent quite some time at this and have decided to try and just keep outlook running at all times as the VBA works then. If only I could stop me and my staff hitting the X on the outlook window.

    I presume it must be something to do with my global permissions but not really too clued up on where I should look or amend these settings, within my business account. Though the same prob exists when switching to personal profile.

    In task manager Outlook does not appear until I run the code, hence thinking it must be a permission setting opposed to another program using Outlook and causing the "Another program is using Outlook. To disconnect programs and exit Outlook, click the Outlook icon and then click Exit Now."

    Was this answer helpful?

    0 comments No comments
  3. OssieMac 48,001 Reputation points Volunteer Moderator
    2023-10-14T06:21:43+00:00

    Does this occur after you have run the code several times? Not sure of the reason but I think it might be something to do with some of the processes not closing when the following code line runs.

    Set OutlookApp = Nothing

    I have had some success by not setting the Outlook variable to nothing. Just leave outlook open instead of continually opening and closing it.

    Not sure if it affects it and I am not sure if my next comment is correct. However, I question setting the app to nothing, which closes the app, before the item is set to nothing.

    Suggest restarting the computer to ensure that all apps and processes get closed and try again with my above suggestions.

    Was this answer helpful?

    0 comments No comments