+ Reply to Thread
Results 1 to 5 of 5

E-Mail Using Excel VBA & Outlook Error

Hybrid View

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    E-Mail Using Excel VBA & Outlook Error

    Hi,

    I have set up a code to send E-mail using Outlook & Excel VBA. The Code is as below -

    Sub SendEMail()
    
        Dim OLApp As Outlook.Application
        Dim OLEMail As Outlook.MailItem
        Dim Adressbook As Worksheet, MailTo As String, Code As String, Counter As Integer, LR As Integer, Path As String
        
        
        Set OLApp = New Outlook.Application
        Set OLEMail = OLApp.CreateItem(olMailItem)
        Set Adressbook = ThisWorkbook.Worksheets("Adress Book")
    
        LR = Adressbook.Cells(Rows.Count, "A").End(xlUp).Row
        
        For Counter = 2 To LR
        
        MailTo = Adressbook.Cells(Counter, "C").Value
        Code = Adressbook.Cells(Counter, "A").Value
        Path = "C:\Users\J\Desktop\Send Dispatch Details\Attachments\" & Code & ".xlsm"
        
            If MailTo <> "" Or MailTo = Null Then
                'On Error GoTo Jumper
                With OLEMail
                    .Display
                    .BodyFormat = olFormatPlain
                    .To = MailTo
                    .Subject = "ZFI/" & Code & " - Dispatch Details"
                    .Attachments.Add Path
                    .Send                
                End With
            End If
            
    'Jumper:
        Next Counter
        
        Set OLApp = Nothing
        Set OLEMail = Nothing
        Set Adressbook = Nothing
        
    MsgBox "Dispatch Details E-Mailed!", vbOKOnly, "E-Mail"
        
    End Sub
    The Code Runs OK the 1st time, however during the second time it gives me the following error -

    Run-time error '462': The remote server machine does not exist or is unavailable
    Can anyone guide me how I can get rid of this?

    Helpful Links -

    http://support.microsoft.com/kb/189618

    http://support.microsoft.com/kb/178510
    Last edited by NeedForExcel; 07-19-2014 at 05:20 AM.
    Cheers!
    Deep Dave

  2. #2
    Forum Contributor
    Join Date
    05-10-2012
    Location
    Paris, France
    MS-Off Ver
    2016/365
    Posts
    123

    Re: E-Mail Using Excel VBA & Outlook Error

    Hi

    The correct code is
    Sub SendEMail()
      Dim OLApp As Outlook.Application
      Dim OLEMail As Outlook.MailItem
      Dim Adressbook As Worksheet, MailTo As String, Code As String, Counter As Integer, LR As Integer, Path As String
      '
      Set OLApp = New Outlook.Application
      Set Adressbook = ThisWorkbook.Worksheets("Adress Book")
      LR = Adressbook.Cells(Rows.Count, "A").End(xlUp).Row
      '
      For Counter = 2 To LR
        ' Initialise Object Mail here
        Set OLEMail = OLApp.CreateItem(olMailItem)
        '
        MailTo = Adressbook.Cells(Counter, "C").Value
        Code = Adressbook.Cells(Counter, "A").Value
        Path = "C:\Users\J\Desktop\Send Dispatch Details\Attachments\" & Code & ".xlsm"
    
        If MailTo <> "" Or MailTo = Null Then
          'On Error GoTo Jumper
          With OLEMail
            .Display
            .BodyFormat = olFormatPlain
            .To = MailTo
            .Subject = "ZFI/" & Code & " - Dispatch Details"
            .Attachments.Add Path
            .Send
          End With
        End If
        ' Release object
        Set OLEMail = Nothing
        'Jumper:
      Next Counter
      ' Release other object
      Set OLApp = Nothing
      Set Adressbook = Nothing
      '
      MsgBox "Dispatch Details E-Mailed!", vbOKOnly, "E-Mail"
    End Sub
    A+

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: E-Mail Using Excel VBA & Outlook Error

    Hi!

    Thank you for the reply.

    However, the code ran for the 1st time well.. After that I am again getting error.

    Error - Runtime Error 2147023170 (800706be): Automation Error TheRemoteProcedureCallFailed
    Unable to figure out why.

    Can you please help?

    Cheers,

    Deep
    Last edited by NeedForExcel; 07-19-2014 at 11:27 PM.

  4. #4
    Forum Contributor pareshj's Avatar
    Join Date
    05-20-2014
    Location
    MUMBAI
    MS-Off Ver
    2007 & 2010
    Posts
    447

    Re: E-Mail Using Excel VBA & Outlook Error

    Hi msexcelathome,

    I think excel address referencing is the issue(reference to Excel has been created and has not been released).

    Try to change your code:

    Like

    Set Adressbook = ThisWorkbook.Worksheets("Adress Book")
    To

    Set Adressbook = ThisWorkbook.Sheets("Adress Book")

    And,

    MailTo = Adressbook.Cells(Counter, "C").Value
    To

    MailTo = Adressbook.Cells(Counter, 3).Value

    And check similar lines of code. Hope this will help you.



    Regards,
    Paresh J
    Click on "* Add Reputation" as a way to say thanks

  5. #5
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: E-Mail Using Excel VBA & Outlook Error

    Hi!

    I figured it out on my own somehow.

    Thank you for the help anyway..

    Reputation Added!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Intermittent error with email generation macro - pasting from excel to outlook mail
    By oenleunc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-15-2014, 05:46 AM
  2. Send e-mail with image on body (.html) on e-mail manager <> outlook
    By mariotnc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2012, 09:28 PM
  3. code to attach the draft mail in new compose mail as attachment in outlook 2010
    By priya1987 in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 08:38 AM
  4. Print excel to PDF and e-mail the PDF with Outlook
    By Isadora in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2010, 11:16 AM
  5. [SOLVED] e-mail from excel when outlook is not in use
    By Jignesh Khandwala in forum Excel General
    Replies: 5
    Last Post: 04-07-2006, 05:00 AM

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