VBA email loop works on my computer but not a colleagues

picklechips

New Member
Joined
Jun 22, 2018
Messages
21
Hi all,

I have a macro that creates multiple outlook emails from a list of email addresses, attaches pdf's and a powerpoint picture in the email body and sends. (see below)

The macro works perfect on my computer, however on my colleague's it only sends the first email then an error appears before sending the next email (ERROR: INVALID PROCEDURE CALL OR ARGUMENT)


Ive been trying to figure it out but no idea why it would work for the first email then bug out on the 2nd when it works fine for me.

Thanks in advance!
Pickles

Code:
Option Explicit


Sub Emailmacro()
Dim TempFile As String
    Dim SigString As String
    Dim SigName As String
    Dim Signature As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim EmailTo As String
    Dim Ratesheetpdf As Variant
    Dim Brochurepdf As Variant
    Dim subj As String
    Dim body As String
    Dim LastRw As Long
    Dim i As Integer
    Dim wb As Workbook
    
    Set wb = ThisWorkbook
    
 On Error GoTo ErrorHandler




    'Ratesheet pdf attachment
    MsgBox ("SELECT RATESHEET PDF - EMAIL ATTACHMENT")
    Ratesheetpdf = Application.GetOpenFilename("PDF Files (*.pdf), *.pdf")
    
    MsgBox ("SELECT BROCHURE PDF - EMAIL ATTACHMENT")
    Brochurepdf = Application.GetOpenFilename("PDF Files (*.pdf), *.pdf")
    
    'ppt slide attachment
    MsgBox ("SELECT POWERPOINT FILE - EMAIL BODY")
    Dim strFileName As String
    
    strFileName = Application.GetOpenFilename( _
        FileFilter:="PowerPoint Files (*.pptx), *.pptx", _
        Title:="Open", _
        ButtonText:="Open")
        
    If strFileName = "False" Then Exit Sub
    
    Dim pptApp As Object
    Dim pptPres As Object
    Dim pptSlide As Object
    
    Set pptApp = CreateObject("PowerPoint.Application")
    Set pptPres = pptApp.Presentations.Open(strFileName)
    Set pptSlide = pptPres.Slides(1)
    Set OutApp = CreateObject("Outlook.Application")
    
    'Get the text that will go on the email subject
    subj = Sheets(1).Range("f2")




    wb.Activate
    


    
    'Assign the file name for the temporary image file of the PowerPoint slide
    TempFile = "temp.jpg"
    
    'Export slide from PowerPoint presentation to temporary file
    pptSlide.Export Filename:=Environ("temp") & "\" & TempFile, FilterName:="JPG"
   
    'Create email loop
    LastRw = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRw Step 100




        EmailTo = Join(Application.Transpose(Sheets(1).Range("A" & i & ":A" & WorksheetFunction.Min(i + 99, LastRw)).Value), ";")
    
       Set OutMail = OutApp.CreateItem(0)
      Set OutMail.SendUsingAccount = Session.Accounts.Item(5)
       
       With OutMail
            .Display
            .To = ""
            .CC = ""
            .BCC = EmailTo
            .Subject = subj
            .body = ""
            .SendUsingAccount = OutApp.Session.Accounts.Item(5)
            .Attachments.Add Environ("temp") & "\" & TempFile
            .HTMLBody = "<img src=""cid:" & TempFile & """ width=""150%"">"
            .HTMLBody = .HTMLBody
            .Attachments.Add Ratesheetpdf
            .Attachments.Add Brochurepdf
            .Send
             End With
  
    Next i
    On Error GoTo 0
    
    'Delete the temporary image file of the PowerPoint slide
    Kill Environ("temp") & "\" & TempFile




    pptPres.Close
    pptApp.Quit
    Set pptApp = Nothing
    Set pptPres = Nothing
    Set pptSlide = Nothing
    Set OutMail = Nothing
    Set OutApp = Nothing




Exit Sub




ErrorHandler:
pptPres.Close
pptApp.Quit
MsgBox "Error:" & vbNewLine & vbNewLine & Err.Description & Err.Number & Err.Source & Err.HelpFile & Err.HelpContext
OutMail.Close 1




End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
To run email from excel, they need to open vba then tools/references.../outlook object library.

Because I could not get all my users (office 365) to do that, I use this

Code:
Sub ObjectLivraryActivation()  
On Error Resume Next
    'Activate Outlook 16.0 object library
      ThisWorkbook.VBProject.References.AddFromGuid _
        GUID:="{00062FFF-0000-0000-C000-000000000046}", _
        Major:=0, Minor:=0 'Use zeroes to default to latest version
  On Error GoTo 0
End Sub

I got the file thanks to

Code:
 Sub ObjectLibraries() 'Those are the object libraries present on this computer
 'Need to go in macro settings and trust access to projet object model
 Dim ref As Variant
 For Each ref In ThisWorkbook.VBProject.References
    Debug.Print "Reference Name: ", ref.Name
    Debug.Print "Path: ", ref.FullPath
    Debug.Print "GUID: " & ref.GUID
    Debug.Print "Version: " & ref.Major & "." & ref.Minor
    Debug.Print " "
  Next ref
 End Sub
 
Last edited:
Upvote 0
Thank you both for responding! The added code from Kamolga seemed to have done the trick :)

I did previously try adding in the object library to my colleague's excel but perhaps it was the wrong one as there are so many to chose from.


THanks again!
Pickles
 
Upvote 0

Forum statistics

Threads
1,223,728
Messages
6,174,150
Members
452,548
Latest member
Enice Anaelle

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