+ Reply to Thread
Results 1 to 14 of 14

MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Question MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Hello everyone,

    Just trying to send an email (gmail) using VBA CDO in excel and my problem is in the title.

    I'm pretty sure that everyone gets it. I want my mail attachment to be selected in a specific cell (that is doing a Vlookup on a customer name) in one of my sheets:

    MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text)
    Here is the code:
    Private Sub btnSendEmail_Click()
        
        Dim MAIL As New Message
        Dim config As Configuration
        Set config = MAIL.Configuration
        
        config(cdoSendUsingMethod) = cdoSendUsingPort
        config(cdoSMTPServer) = "smtp.gmail.com"
        config(cdoSMTPServerPort) = 25
        config(cdoSMTPAuthenticate) = cdoBasic
        config(cdoSMTPUseSSL) = True
        config(cdoSendUserName) = "email@email.com"
        config(cdoSendPassword) = "Password"
        config.Fields.Update
        
        Call createJpg("MAIL", "A5:I56", "image")
        tempfilepath = Environ$("temp") & "\"
            MAIL.AddAttachment tempfilepath & "image.jpg", olbyvalue, 1
                
        MAIL.To = Range("N18").Text
        MAIL.CC = Range("N19").Text
        MAIL.From = config(cdoSendUserName)
        MAIL.Subject = "text" + Range("E21").Text + " text // " + Range("N8").Text
        MAIL.HTMLBody = "<span LANG=FR><p class=style2 p align=justify p style='width='850' height='1500'><span LANG=FR><font FACE=Calibri SIZE=3>" & _
        "<p>Hello,</p>"
        
        MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text)
        
        On Error Resume Next
        
        If MsgBox("Do you really want to send this email?", vbYesNo) = vbNo Then Exit Sub
        
        MAIL.Send
        
        If Err.Number <> 0 Then
            MsgBox Err.Description, vbCritical, "There was an error"
            Exit Sub
            
        End If
        
        MsgBox "your email has been sent", vbInformation, "sent"    
    End Sub
    
    
    
    Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)
        ThisWorkbook.Activate
        Worksheets(Namesheet).Activate
        Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange)
        Plage.CopyPicture
        With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
            .Activate
            .Chart.Paste
            .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
        End With
        Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
    Set Plage = Nothing
    End Sub
    Last edited by JBeaucaire; 11-04-2014 at 11:40 AM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    anybody can help?

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Hello VBAzr,

    Have you verified that the contents of Sheets("MAIL").Range("N17").Text is a fully qualified file path like "C:\Users\Owner\My file.txt" ?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Hello!

    Yes I have! And if I put directly the path inside the code it works... of course...

    MAIL.AddAttachment "C:\Users\Owner\My file.txt"

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Hello VBAzr,

    Do receive any error messages?

  6. #6
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Leith,

    it just sends a No Name file...

    so two files are attached in the mail (the JPG I create inside the sheet + the not-working NO NAME attachement)

  7. #7
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Leith,

    Sorry, there is definitly an error message.

    Run-Time error'-2147024894 (80070002)':
    the specified file is not found

  8. #8
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Maybe it is because my file is on a server and not on the "C:\"

    Actually, we named it "Z:\" and like I said, the files are on a server... not on the computer.

  9. #9
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Add check:
    If dir(Sheets("MAIL").Range("N17").Text, 16) = "" Then
    msgbox "Invalid Attachment!", vbcritical
    exit sub
    End if
    And try use: Sheets("MAIL").Range("N17").Value
    I'm sorry my english...

  10. #10
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    still not working

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Try using the full UNC path, not the local path. So \\ServerName\Share\Folder\MyFile.txt rather than Z:\MyFile.txt

  12. #12
    Forum Contributor
    Join Date
    06-04-2013
    Location
    Moscow
    MS-Off Ver
    Office 365
    Posts
    100

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Try instead the disk name to use IP of the disk: "\\192.168.1.1\Owner\My file.txt"

  13. #13
    Registered User
    Join Date
    11-04-2014
    Location
    Paris
    MS-Off Ver
    2010
    Posts
    8

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    So I put my attachment on my desktop to make a test... and it still don't work

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: MAIL.AddAttachment (Sheets("MAIL").Range("N17").Text) <---- Why is it not working?

    Hello VBAzr,

    Try changing the SMTP port numberfrom 25 to 445.
        config(cdoSMTPServerPort) = 445

+ 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. [SOLVED] Range("A1") = Sheets("Sheet2").CenterHeader.Text doesnt work.
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-27-2013, 07:06 AM
  2. Replies: 0
    Last Post: 11-20-2012, 10:22 AM
  3. Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)" not working
    By redders in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2011, 03:52 PM
  4. "Send to Mail Recipient" icon not working
    By Lynde in forum Excel General
    Replies: 7
    Last Post: 06-23-2005, 12:05 PM
  5. [SOLVED] Sending macro based e-mail with built-in "Heading" and "Text"
    By Prabha in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2005, 11:06 AM

Tags for this Thread

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