Hi,
I am trying to run the code below and I'm getting a run time error on this line. What I'm trying to do with this line is to send a row in the body of the email based on the index value i.
Set rng3 = Sheets("Jan. 2012").Range(cells(r,1:15).value).specialcells(xlcelltypevisible)
below is the complete code
Private Sub Workbook_Open()
Dim Sdate As String
Dim ol As Outlook.Application
Dim olMsg As Outlook.MailItem
Set objOutlook = CreateObject("outlook.application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Dim olNameSpace As Outlook.Namespace
Dim olInbox As Outlook.MAPIFolder
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
'Make Outlook visible
Set ol = New Outlook.Application
Set olNameSpace = ol.GetNamespace("MAPI")
Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)
olInbox.Display
Set rng = Sheets("Template").Range("A3:O27").SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Jan. 2012").Range("A11:O11").SpecialCells(xlCellTypeVisible)
Set rng3 = Sheets("Jan. 2012").Range(Cells(r, "1:15").Value).SpecialCells(xlCellTypeVisible)
Dim i As Integer
For i = 12 To 14
With objOutlookMsg
Set objOutlook = CreateObject("outlook.application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
.Subject = "Action Required for Top Driver " & Cells(i, 1).Value & " " & Cells(i, 2).Value
.HTMLBody = RangetoHTML(rng) & RangetoHTML(rng2) & RangetoHTML(rng3)
.To = Cells(i, 11).Value
.CC = Cells(i, 12).Value
.Send
End With
Next i
End Sub
the RangetoHTML function code is below
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
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"
'Copy the range and create a new workbook to past the data in
rng.Copy
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
.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
End Function
Bookmarks