VBA to Create Outlook Email Not Working for All Users

Damien Hartzell

New Member
Joined
Jun 6, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I have an .xlsm with VBA set to create an email in Outlook. Most users have success in using the button that initiates the macro, however for some users the macro spins and then does nothing. I've narrowed it down to the line Set OutMail = OutApp.CreateItem(0) in the following code. Any insight as to why this may work for some but not others would be most appreciated.

Here is the code:
VBA Code:
Sub CreateEmailFromExcel()

On Error GoTo Err

Dim rng As Range
Dim OutApp As Outlook.Application
Dim OutMail As MailItem

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

Tenure = Range("H5").Text
Un = Range("H7").Text
weekend = Range("H3").Text

GracePeriod = Range("F3").Value

NewHireOccurrences = Range("C7").Value

    NHCA1 = Range("B9").Text
    NHWritten = Range("C9").Text

    NHCA2 = Range("B10").Text
    NHFinal = Range("C10").Text

TotalOccurrenceHours = Range("H10").Value

    TenCA1 = Range("B15").Text
    TenDocCoaching = Range("C15").Text
    TenDocCoachingHoursSince = Range("F15").Value
    
    TenCA2 = Range("B16").Text
    TenWritten = Range("C16").Text
    TenWrittenHoursSince = Range("F16").Value

    TenCA3 = Range("B17").Text
    TenFinal = Range("C17").Text
    TenFinalHoursSince = Range("F17").Value

UnionOccurrences = Range("C20").Value

    UnCA1 = Range("B22").Text
    UnDocCoaching = Range("C22").Text
    UnDocCoachingOccurrencesSince = Range("F22").Text

    UnCA2 = Range("B23").Text
    UnVerbal = Range("C23").Text
    UnVerbalOccurrencesSince = Range("F23").Text

    UnCA3 = Range("B24").Text
    Un1stWritten = Range("C24").Text
    Un1stWrittenOccurrencesSince = Range("F24").Text

    UnCA4 = Range("B25").Text
    Un2ndWritten = Range("C25").Text
    Un2ndWrittenOccurrencesSince = Range("F25").Text
    
    UnCA5 = Range("B26").Text
    UnFinal = Range("C26").Text
    UnFinalOccurrencesSince = Range("F26").Text


FMLAHours = Range("C29").Value

    'add default signature; this will be listed in htmlbody as "OutMail.HTMLBody"
        OutMail.Display
    
    'Update Font and Size with this: <P STYLE='font-family:CalistoMT;font-size:16pt'>

With OutMail
    If Tenure = "New Hire" Then
        .To = ""
        .Subject = "Your Unscheduled and/or FMLA Time"
        .HTMLBody = "<P STYLE='font-family:CalistoMT;font-size:16pt'> Hello. <br><br>" & _
        "Attendance is a vital aspect of employment. It's integral to supporting the larger team and, ultimately, the Members seeking important medical care on the other side of the work we do. <br><br>" & _
        "You currently have " & NewHireOccurrences & " occurrence(s)." & vbNewLine & vbNewLine & _
        "Please, ensure all absences are scheduled and approved in NICE WebStation, as well as that you have the time to cover those absences in Workday. <br><br>" & _
        "Reminders: a) The 40-hour grace period includes all unscheduled, paid Time Off Types (including FMLA), b) Total Occurrences includes unscheduled paid time exceeding that 40 hours, as well as Unpaid and Blackout time accrued, and c) any time that indicates Excused is not applying toward 40-hour grace period or to disciplinary action steps. <br><br>" & _
        "Thank you! <br><br>" & _
        RangetoHTML(rng) & "<br><br>" & _
        OutMail.HTMLBody
        .Display
    
    ElseIf Un = "Yes" Then
        .To = ""
        .Subject = "Your Unscheduled and/or FMLA Time"
        .HTMLBody = "<P STYLE='font-family:CalistoMT;font-size:16pt'> Hello. <br><br>" & _
        "Attendance is a vital aspect of employment. It's integral to supporting the larger team and, ultimately, the Members seeking important medical care on the other side of the work we do. <br><br>" & _
        "You currently have " & UnionOccurrences & " occurrence(s) of unscheduled time. <br><br>" & _
        "Your FMLA usage for the past 12 months is currently " & FMLAHours & " hour(s). <br><br>" & _
        "Please, ensure all absences are scheduled and approved in NICE WebStation, as well as that you have the time to cover those absences in Workday. <br><br>" & _
        "Thank you! <br><br>" & _
        RangetoHTML(rng) & "<br><br>" & _
        OutMail.HTMLBody
        .Display

    ElseIf Tenure = "Tenure" Then
        .To = ""
        .Subject = "Your Unscheduled and/or FMLA Time"
        .HTMLBody = "<P STYLE='font-family:CalistoMT;font-size:16pt'> Hello. <br><br>" & _
        "Attendance is a vital aspect of employment. It's integral to supporting the larger team and, ultimately, the Members seeking important medical care on the other side of the work we do. <br><br>" & _
        "You have accrued " & GracePeriod & " hour(s) toward your 40-hour grace period. You currently have " & TotalOccurrenceHours & " Total Occurrence Hour(s), which is the sum of Unpaid, Blackout Period and Unscheduled Paid Time beyond the 40-hour grace period. <br><br>" & _
        "Your FMLA usage for the past 12 months is currently " & FMLAHours & " hour(s). <br><br>" & _
        "Please, ensure all absences are scheduled and approved in NICE WebStation, as well as that you have the time to cover those absences in Workday. <br><br>" & _
        "Reminders: a) The 40-hour grace period includes all unscheduled, paid Time Off Types (including FMLA), b) Total Occurrence Hours includes unscheduled paid time exceeding that 40 hours, as well as Unpaid and Blackout time accrued, and c) any time that indicates Excused is not applying toward 40-hour grace period or to disciplinary action steps. <br><br>" & _
        "Thank you! <br><br>" & _
        RangetoHTML(rng) & "<br><br>" & _
        OutMail.HTMLBody
        .Display
    End If
End With

    Set OutMail = Nothing
    
Err:
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.StatusBar = False
    Application.DisplayStatusBar = oldStatusBar
    Application.EnableEvents = True

End Sub

Function RangetoHTML(rng As Range)

    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
       
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

Dim ActiveTable As String
    Range("H15").Select
    ActiveTable = ActiveCell.ListObject.Name
                    With Range(ActiveTable).ListObject
                    
                    ''
                        Range("H15:M15").Select
                        Range(Selection, Selection.End(xlDown)).Select

                    ''
                    Selection.Copy
                    End With

'
Set Cell = Nothing

    'Copy the range and create a new workbook to paste the data in
    '''rng.Copy
    On Error GoTo Err
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        '.Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        'autofit columns to text:
        Columns("A:F").AutoFit
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

'''''''''''''''''''''''
    

'''''''''''''''''


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
    
Err:
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.StatusBar = False
    Application.DisplayStatusBar = oldStatusBar
    Application.EnableEvents = True
    
End Function
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I could be way off on this one, but do your users happen to use Citrix or a similar environment for working on Excel files? I have had this issue when people open an Excel file in Citrix, but have never opened up Outlook in Citrix, when means there's no profile for Outlook to use and it fails to open. Once these users opened up Outlook in Citrix and went through the set up process (a one-time process), the VBA worked just fine from then on.
 
Upvote 0
I could be way off on this one, but do your users happen to use Citrix or a similar environment for working on Excel files? I have had this issue when people open an Excel file in Citrix, but have never opened up Outlook in Citrix, when means there's no profile for Outlook to use and it fails to open. Once these users opened up Outlook in Citrix and went through the set up process (a one-time process), the VBA worked just fine from then on.
Great troubleshooting inquiry! We do use Citrix, however, these users already have Outlook running. Some steps I'm going to take are: a) walking each user through adding Developer to their ribbon, b) running the macro with and without Outlook open (for some users, the macro works when Outlook is closed...I don't have enough data yet to determine if this is a workaround for everyone experiencing the issue), and c) reach out to you intelligent and experienced folks!

Anyway, these users are in a Citrix VPN environment, yet already have completed their Outlook setup process.
 
Upvote 0
Here is a simple "overlooked" issue ... do those users have the MS Outlook reference added into the macro ?
 
Upvote 0
Great troubleshooting inquiry! We do use Citrix, however, these users already have Outlook running. Some steps I'm going to take are: a) walking each user through adding Developer to their ribbon, b) running the macro with and without Outlook open (for some users, the macro works when Outlook is closed...I don't have enough data yet to determine if this is a workaround for everyone experiencing the issue), and c) reach out to you intelligent and experienced folks!

Anyway, these users are in a Citrix VPN environment, yet already have completed their Outlook setup process.
I wish I had other ideas, but unfortunately I'm having trouble coming up with other ideas. You might have to create a little debugging subroutine that writes errors to a log file somewhere.
 
Upvote 0
I wish I had other ideas, but unfortunately I'm having trouble coming up with other ideas. You might have to create a little debugging subroutine that writes errors to a log file somewhere.
That's okay! I appreciate your help!
 
Upvote 0
Here is a simple "overlooked" issue ... do those users have the MS Outlook reference added into the macro ?
Yep. They have "Microsoft Outlook 16.0 Object Library" checked in their References. These excel docs are stored in various SharePoint sites and utilized by users across different cities and states. When I open any of them from any of the SharePoint sites, the macro works for me. It works for others, but not all users. There is no error message, but it seems to stop working at "Set OutMail = OutApp.CreateItem(0) for those users.
 
Last edited:
Upvote 0
"Set OutMail = OutApp.CreateItem(0)"

Maybe change the object term 'OutMail' to something else so Outlook doesn't get confused ? Perhaps 'OutM' ?
Just a shot in the dark ....
 
Upvote 0
"Set OutMail = OutApp.CreateItem(0)"

Maybe change the object term 'OutMail' to something else so Outlook doesn't get confused ? Perhaps 'OutM' ?
Just a shot in the dark ....
Dang! Tried and failed. I'll keep researching. I really appreciate it
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,941
Members
452,539
Latest member
delvey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top