+ Reply to Thread
Results 1 to 5 of 5

Problems Emailing Attachment

  1. #1
    Registered User
    Join Date
    06-04-2010
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Problems Emailing Attachment

    Hi all,

    I have a fairly basic understanding of VBA and managed to put the below code together, to enable me to automatically send e mails dependant on different outcomes. The code worked fine when I specified a specific document that I wanted it to attach to an e mail, which is great. The problem is that the name of the document I wish to attach is variable. When the code gets to the section highlighted below it errors. The strange thing is that when I step into the code it is recognising the file I want it to attach.

    Does anyone have any idea how I can overcome this issue?

    Sub CDO_Mail_Small_Text()
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant
    Dim TARGETNAME As String
    TARGETNAME = Format(Now - 1, "yyyy-mm-dd") & ".xls"


    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1 'CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "tgrex0001"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
    End With

    With iMsg
    Set .Configuration = iConf
    .To = "[email protected]"
    .CC = "[email protected]"
    .BCC = ""
    .From = """White, Lee""<[email protected]>"
    .Subject = "TEST VBA"
    .HTMLBody = "<Font Face =Arial> <Font Size = 2><p>Dear all,</p>" & _
    "<p>Please find attached a copy of the lastest CCS Outgoing One Pager.</p>" & _
    "<p>A copy of this report can also be found in G:\Customer Contact Services\Reporting\Outgoing MI.</p>" & _
    "<p>Kind regards,<br>" & _
    "CCS Automation</br>"
    .Attach = "G:\Customer Contact Services\Reporting\Outgoing MI\Daily Reports\" & TARGETNAME
    .Send
    End With

    End Sub
    Last edited by lee2k60; 06-11-2010 at 08:10 AM.

  2. #2
    Registered User
    Join Date
    06-04-2010
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Problems Emailing Attachment

    Apologies, post edited.

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Problems Emailing Attachment

    Hi lee2k60,
    the path and file name are correct?
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Registered User
    Join Date
    06-04-2010
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Problems Emailing Attachment

    Hi pike,

    I managed to get around the problem. It seems that the code didn't like me entering the file path and declaring the file name. I've changed it to the below and it now works.

    Thanks guys,

    Sub CDO_Mail_Small_Text()
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant
    Dim TARGETNAME As String
    TARGETNAME = Format(Now - 1, "yyyy-mm-dd") & ".xls"
    Dim FILEPATH As String
    FILEPATH = "G:\Customer Contact Services\Reporting\Outgoing MI\Daily Reports\"



    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1 'CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "tgrex0001"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Update
    End With

    With iMsg
    Set .Configuration = iConf
    .To = "[email protected]"
    .CC = "[email protected]"
    .BCC = ""
    .From = """White, Lee""<[email protected]>"
    .Subject = "TEST VBA"
    .HTMLBody = "<Font Face =Arial> <Font Size = 2><p>Dear all,</p>" & _
    "<p>Please find attached a copy of the lastest CCS Outgoing One Pager.</p>" & _
    "<p>A copy of this report can also be found in G:\Customer Contact Services\Reporting\Outgoing MI.</p>" & _
    "<p>Kind regards,<br>" & _
    "CCS Automation</br>"
    .Attach = FILEPATH & TARGETNAME
    .Send
    End With
    End Sub

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Problems Emailing Attachment

    Thanks for taking the time to read and comply with the rule .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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