问题
I have this code that is already working, but not 100%... I would like to send an email through Gmail with a pdf attached... But I don´t want a specific pdf file.
I know I can put a specific file name on the code, but my pdf files always change the name
Example:
Clientname_1.pdf, Clientmane_2.pdf, Suppliername_1.pdf, etc
The code I have is this, but does not work:
Dim objEmail, objConfig
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "xpto@gmail.com"
objEmail.To = Join(Application.Transpose(Sheets("Clients").Columns(7).SpecialCells(2)), ";")
objEmail.Subject = "Test"
objEmail.CC = ""
objEmail.AddAttachment "C:\Users\123\Desktop\*.pdf"
I need that the code go to the folder I want and send the existing pdf file(s) on it......
What is missing in this code???
回答1:
If you are only looking for the first .PDF file found then something like this would be appropriate.
dim dr as string
dr = "C:\Users\123\Desktop\"
objEmail.AddAttachment dr & Dir(dr & "*.pdf")
The Dir function could loop and find all appropriate matches but it seems that you only want the first one. If Dir cannot find a matching file it will return a zero-length string.
For additional functionality, you might want to use the Environ function to get the current user's profile folder and get their Desktop folder from there.
来源:https://stackoverflow.com/questions/31712849/vba-code-to-send-email-through-gmail-with-pdf-attachment