+ Reply to Thread
Results 1 to 4 of 4

Adding attachments to outlook - "Run-time error 91: Object variable or With block variable

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2019
    Posts
    57

    Adding attachments to outlook - "Run-time error 91: Object variable or With block variable

    What is wrong with my code? I received "Run-time error 91: Object variable or With block variable not set". My source_file is able to get the correct file location as string but the my_attachments just shows nothing in the locals. The source_file shows 2 locations:

    C:\Users\User\Desktop\test.txt; C:\Users\User\Desktop\test2.txt

    Locals.JPG

    Sub SendEmail(what_address As String, CC_address As String, subject_line As String, mail_body As String, my_attachments As Object)
    
    Dim olapp As Outlook.Application
    Set olapp = CreateObject("Outlook.Application")
    Dim olMail As Outlook.MailItem
    Set olMail = olapp.CreateItem(olMailItem)
    
         olMail.Attachments = my_attachments
    
    End Sub
    
    Sub SendMassEmail() Dim source_file As String Dim my_attachments As Object source_file = ws.Range("G4") my_attachments.Add source_file Call SendEmail(ws.Range("A" & row_number), ws.Range("B" & row_number), mail_subject, mail_body_message, my_attachments) End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Adding attachments to outlook - "Run-time error 91: Object variable or With block vari

    "SendEmail" code should add the attachment
         olMail.Attachments.add  my_attachments
    You may have to change "my_attachments" into a string in the "SendMassEmail" code

  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,047

    Re: Adding attachments to outlook - "Run-time error 91: Object variable or With block vari

    I'm not sure you need the my_attachments object. Just pass the source_file to a string parameter.

     olMail.Attachments.Add VariableContainingFilePath

  4. #4
    Registered User
    Join Date
    09-20-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2019
    Posts
    57

    Re: Adding attachments to outlook - "Run-time error 91: Object variable or With block vari

    Thanks. It works.

+ 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. Replies: 12
    Last Post: 12-28-2019, 12:29 PM
  2. [SOLVED] Run-time error '91': Object variable or With block variable not set when closing userform
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2016, 08:59 AM
  3. [SOLVED] Object variable or with block variable not set "Run time error 91"
    By imzhakmaya in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2014, 06:54 AM
  4. Replies: 2
    Last Post: 08-22-2014, 03:55 AM
  5. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  6. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  7. Object variable or With block variable not set" error message
    By JoeS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-22-2009, 11:51 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