Hi Guys,
Hoping you can help as I'm really struggling with this.
I'm trying to email a range from excel into the body of an email, all using Access VBA.
I've used Ron De Bruin's excellent VBA code to do this, however, I keep getting the error that 'The selection is not a range or the sheet is protected".
The sheet is not protected and stepping through the code I can see that the range is being selected (highlighted) so I'm stumped as to why I'm receiving this error.
If I comment out the error capture code, the procedure continues to run through but nothing is pasted into the mail body in Outlook.
Below is the code I'm using:
Sub Mail_Selection_Range_Outlook_Body()
'Working in Excel 2000-2013
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
Set rng = excel.Sheets("Sheet1").Range("A1:F35").Select.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With excel.Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "me@me.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML(rng)
.Display
End With
On Error GoTo 0
With excel.Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Bookmarks