Emailing a named range via Outlook

Mr_Doug

New Member
Joined
Mar 29, 2016
Messages
8
Hi All,

I have a minor (hopefully) issue when trying to email a named range using the code below. The code was borrowed from another post and adapted slightly.

I'm getting a "Compile error: Type mismatch" when trying to amend the sender (I have more than one mailbox on the pc) using;

objNewEmail.Sender = "<email address here>"

Thanks in advance.

Doug

Private Sub cbSend_Click()
Dim objSelection As Excel.Range
Dim objTempWorkbook As Excel.Workbook
Dim objTempWorksheet As Excel.Worksheet
Dim strTempHTMLFile As String
Dim objTempHTMLFile As Object
Dim objFileSystem As Object
Dim objTextStream As Object
Dim objOutlookApp As Outlook.Application
Dim objNewEmail As Outlook.MailItem

' Copy the named range
Range("EmailContent").Select
Set objSelection = Selection
Selection.Copy

' Paste the copied selected ranges into a temp worksheet
Set objTempWorkbook = Excel.Application.Workbooks.Add(1)
Set objTempWorksheet = objTempWorkbook.Sheets(1)

' Maintain the formatting
With objTempWorksheet.Cells(5, 1)
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteFormats
End With

With objTempWorksheet.Cells(1, 1)
.Value = "Dear " & tbRecipientName.Value & ","
End With

With objTempWorksheet.Cells(3, 1)
.Value = "Please find your quotation below"
End With

' Save the temporary worksheet as a HTML file
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempHTMLFile = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Excel" & Format(Now, "YYYY-MM-DD hh-mm-ss") & ".htm"
Set objTempHTMLFile = objTempWorkbook.PublishObjects.Add(xlSourceRange, strTempHTMLFile, objTempWorksheet.Name, objTempWorksheet.UsedRange.Address)
objTempHTMLFile.Publish (True)

' Open outlook and create a new email message
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)

' Read the HTML file data and insert into the email body
Set objTextStream = objFileSystem.OpenTextFile(strTempHTMLFile)

objNewEmail.To = tbEmailAddress.Value 'passed in from a textbox
objNewEmail.Subject = "Your subject message here: " & tbQuoteREf.Value
' objNewEmail.Sender = "me@you.com"

objNewEmail.Display

' Housekeeping - Remove the htm file and close the temporary sheet
objTextStream.Close
objTempWorkbook.Close (False)
objFileSystem.DeleteFile (strTempHTMLFile)
objNewEmail.HTMLBody = "<table align=""left"">" & objTextStream.ReadAll & "</table>" & "<p align=""left"">" & objNewEmail.HTMLBody & "</p>"
Set EmailApplication = Nothing
Set EmailItem = Nothing

Unload ufEmailDetails
Range("A1").Select
Range("EmailContent").ClearOutline

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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