Results 1 to 9 of 9

Thread: Excel Macro - concatenate filename with text in a cell

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Seeker Alexk's Avatar
    Join Date
    Sep 2020
    Posts
    7
    Articles
    0
    Excel Version
    2010

    Excel Macro - concatenate filename with text in a cell

    I have a sheet where in column A I have email addresses and in column B the client IDs. I would like to use the client IDs to match the file name,
    so the right file can be grabbed from a folder and then be attached to an email, using the email address in column A. here is a sample data:

    Column A | Column B
    client1@yahoo.com | 12345
    client2@gmail.com | 67890

    The files in the c:\PDF Files\Invoice folder: Invoice12345.pdf Invoice67890.pdf

    I am trying to use the following macro to attach Invoice12345.pdf to an email using outlook and send it to client1@yahoo.com and repeat it until
    there are no more emails in column A. for some reason, sometimes it works but only creates one email and crashes when it tries to get to the second record.
    here is the error message: "Run-time error'-2147024894 (80070002)': Cannot find this file. Verify the path and file name are correct."

    Thank you in advance for your help.

    Code:
    Sub Email()
    
    Dim BlankFound As Boolean Dim x As Long
    
    Do While BlankFound = False
    
      x = x + 1
      If Cells(x, "A").Value = "" Then
      BlankFound = True
      End If
    
    Dim Mail_Object, Mail_Single As Variant
    
        Email_Subject = "Booking Invoice"
        nameList = Sheets("Sheet1").Cells(x, "A").Value
        Email_Send_To = nameList
    
        Email_Cc = ""
        Email_Bcc = ""
        Email_Body = "Here's your Invoice"
    
        Set Mail_Object = CreateObject("Outlook.Application")
        Set Mail_Single = Mail_Object.CreateItem(o)
    
        With Mail_Single
            .Subject = Email_Subject
            .To = Email_Send_To
            .CC = Email_Cc
            .BCC = Email_Bcc
            .Body = Email_Body
            .Attachments.Add ("c:\PDF Files\Invoice\Invoice" & Cells(x, "B") & ".pdf")
       
            .Display
           ' .send
    
    End With ' MsgBox "E-mail successfully sent" ' Application.DisplayAlerts = False
    
    Loop
    End Sub
    Last edited by p45cal; 2020-09-29 at 03:56 PM.

  2. #2
    Magician NoS's Avatar
    Join Date
    Jan 2013
    Location
    British Columbia
    Posts
    824
    Articles
    0
    Excel Version
    Excel 2010
    You are missing the \ between the path and the file name

  3. #3
    Seeker Alexk's Avatar
    Join Date
    Sep 2020
    Posts
    7
    Articles
    0
    Excel Version
    2010
    Quote Originally Posted by NoS View Post
    You are missing the \ between the path and the file name
    Thanks for your reply.
    the path is c:\PDF Files\Invoice\
    and the file name is Invoice12345.pdf

    so where am I missing a \ ?

  4. #4
    Magician NoS's Avatar
    Join Date
    Jan 2013
    Location
    British Columbia
    Posts
    824
    Articles
    0
    Excel Version
    Excel 2010
    between the path and the file name
    should be like this

    Code:
     .Attachments.Add ("c:\PDF Files\Invoice\Invoice" & "\" & Cells(x, "B") & ".pdf")

  5. #5
    Seeker Alexk's Avatar
    Join Date
    Sep 2020
    Posts
    7
    Articles
    0
    Excel Version
    2010
    .Attachments.Add ("c:\PDF Files\Invoice\Invoice"& Cells(x, "B") & ".pdf")

    The section I underlined is the file name. it's not part of the the path. I am trying to change the file name every time it loops through. for example the first time, the path is
    c:\PDF Files\Invoice\ and the file name is "Invoice12345.pdf". the second time (which seems to causing the issue) the path is the same (c:\PDF Files\Invoice\) but the file name is "Invoice67890.pdf"

  6. #6
    Magician NoS's Avatar
    Join Date
    Jan 2013
    Location
    British Columbia
    Posts
    824
    Articles
    0
    Excel Version
    Excel 2010
    maybe leading spaces in the cell ?
    try
    Code:
    .Attachments.Add ("c:\PDF Files\Invoice\Invoice"& trim(Cells(x, "B")) & ".pdf")

Tags for this Thread

Posting Permissions

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