+ Reply to Thread
Results 1 to 5 of 5

HELP! Run Time Error '1004' for sending email via vba in excel

Hybrid View

  1. #1
    Registered User
    Join Date
    03-09-2012
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    31

    Question HELP! Run Time Error '1004' for sending email via vba in excel

    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
    Last edited by g1987; 03-15-2012 at 04:24 PM.

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: HELP! Run Time Error '1004' for sending email via vba in excel

    What is the value for r or are you referring to column R
    Range(Cells(r, "1:15").Value)
    This does not make sense - what are you referring to - column R??? If r is a variable it has not been declared or initialised
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  3. #3
    Registered User
    Join Date
    03-09-2012
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: HELP! Run Time Error '1004' for sending email via vba in excel

    I'm sorry you're right. I mis-typed. However, it still does not work. I am referring to the row index i.

    Set rng3 = Sheets("Jan. 2012").Range(Cells(i, "1:15").Value).SpecialCells(xlCellTypeVisible)

  4. #4
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: HELP! Run Time Error '1004' for sending email via vba in excel

    You are still trying to set a range with a value that not even been declared? If you want row index i and columns 1 to 15 then use
    Set rng3 = Sheets("Sheet1").Range(Cells(i, 1), Cells(i, 15)).SpecialCells(xlCellTypeVisible)
    The problem is that you cannot set the range until you have a value for i so either declare the value and initialise it earlier or place the range statement inside the loop.

  5. #5
    Registered User
    Join Date
    03-09-2012
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: HELP! Run Time Error '1004' for sending email via vba in excel

    Oh ok, thanks.

    Can I combine rng2 and rng3 so that they are pasted together as a table?

    Set rng3 = Sheets("Jan. 2012").Range("A11:S11") & Sheets("Jan. 2012").Range(Cells(i, 1), Cells(i, 19)).SpecialCells(xlCellTypeVisible)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1