Run time 13 errors on an email notification

Confused_UK

Board Regular
Joined
Dec 19, 2015
Messages
56
Hi all

I'm looking for some guidance. I have done a script to send an email notification when a database is updated.

"EmailTo" is a named range of email addresses
"EmailSubject" is a named cell with the subject header
"EmailBody" is a named cell with the body of the email

Code:
Sub EmailNotification()


    Dim Mailaddress As String
    Dim olApp As Object
    
    If Range("EmailTo").Value <> "" Then
        Set olApp = CreateObject("Outlook.Application")
        
        With olApp.CreateItem(0)
            .To = Range("EmailTo").Value
            .Subject = Range("EmailSubject").Text
            .Body = Range("EmailBody").Text
            .Display
            .Send
        End With
    End If
    
End Sub

However, when I run it, I get a type mismatch run time error 13 against the line

Code:
If Range("EmailTo").Value <> "" Then

What have I missed? Any help would be greatly appreciated.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

The address list needs to be a String with Delimiters and not a Range. Try this:
Code:
Sub EmailNotification()
    Dim olApp       As Object
    Dim Addresses   As Variant
    Dim EmailTo     As String
    Dim delim       As String
    Dim ele         As Variant
    
    Addresses = Range("EmailTo").Value
    delim = ""
    For Each ele In Addresses
        EmailTo = EmailTo & delim & ele
        delim = ";"
    Next
    
    If EmailTo <> "" Then
        Set olApp = CreateObject("Outlook.Application")
        With olApp.CreateItem(0)
            .To = EmailTo
            .Subject = Range("EmailSubject").Text
            .Body = Range("EmailBody").Text
            .Display
            .Send
        End With
    End If
End Sub
It reads the Range of addresses into a variant then concatenates all the elements using a delimiter.


Regards,
 
Last edited:
Upvote 0
I really like working with VBA but you have found the most ill thought out aspect of VBA and Excel the range. Welcome no nonsense land and it gets worse. Using the unscoped "Range" will cause errors when done correctly sometimes and always when not used correctly. First is the named range you are looking for a sheet scoped or workbook scoped range? This may be your issue or not. To avoid this issue you can convert to either

Code:
ThisWorkbook.Names("EmailTo").RefersToRange.Value

Or

MySheetAsObject.Names("EmailTo").RefersToRange.Value

This resolves the issue of VBA not having a workbook scoped "Range" because you do have workbook scoped "Name" yay! They also seem to be more stable over older versions of excel just do not try to set non-continuous ranges with them awww snap thought you were in the clear. Now you may also have an issue caused by merged cells if that is the case then edit your range names to only include the top left cell in your group of merged cells or use this so you are only working with the top left cell of your range that has multiple cells.

Code:
ThisWorkbook.Names("EmailTo").RefersToRange.Cells(1,1).Value

Or

MySheetAsObject.Names("EmailTo").RefersToRange.Cells(1,1).Value

I consider myself to be reasonably smart but this one makes my eyes go crossed good luck hope that helps
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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