File attachment error sending CDO mail with VBA


Board Regular
Sep 19, 2005

One last piece of the jigsaw to make the Userform/File work. I have a problem attaching a file with CDO mail.

Having used Ron de Bruin’s code for sending CDO mail, everything works fine apart from when I try attaching the newly saved excel file of a single sheet?

The code below is kept in a Module of the workbook and gets stuck at the .AddAttachment line each time.

How can I get around this please to attach the correct excel file required?

Error =
Run-time error ‘-2147024894(80070002)’:
The system cannot find the file specified.

Sub CDO_Mail_Form()

'Code to send email
Dim iMsg As Object
Dim iConf As Object
Dim iCode As String
Dim fName As String
Dim NewFile As String
Dim Flds As Variant

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

iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("") = 2
.Item("") _
= "123.456.789.0"
.Item("") = 25
End With

iCode = Worksheets("DataChangeRequestForm").Range("C4").Value
fName = ("\\FILE1\Data Change Request - " & iCode)
NewFile = fName & " " & Format(Date, "dd-mm-yyyy")

With iMsg
Set .Configuration = iConf
.To = ""
.CC = ""
.BCC = ""
.From = ""
.Subject = "Change Request: " & iCode & " - (Main Category = " & frmDataChangeRequest.cboMainCat.Value & ")"
.TextBody = "Data Change Request Form Attached - " & iCode

‘Gets stuck at this line!! I am using the NewFile for path & name. Have also tried with 'FileExtStr' removed?.
.AddAttachment NewFile & FileExtStr

End With
End Sub

‘In the form code: this is used to save the file as follows, which works fine:

Sub cmdSaveSend_Click()

'Code to save file as unique name with date stamp (iCode = Product Code)
Sheets("DataChangeRequestForm").Protect Password:="password"

iCode = Worksheets("DataChangeRequestForm").Range("C4").Value
fName = ("\\FILE\DEPT\Shared Files by User\User\Forms\Change Request\Autosave\Data Change Request - " & iCode)
NewFile = fName & " " & Format(Date, "dd-mm-yyyy")

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=NewFile
Application.DisplayAlerts = True

'Run Module code to send email

End Sub

Thank you very much for your help.


Board Regular
Sep 19, 2005

Please ignore the different fName details, they are identical in my file, just updated for posting purpose and didn't make them the same! sorry.

fName = ("\\FILE1\Data Change Request - " & iCode)

fName = ("\\FILE\DEPT\Shared Files by User\User\Forms\Change Request\Autosave\Data Change Request - " & iCode)


Board Regular
Sep 19, 2005
I have now resolved this.

Should have applied ALL the code together in the module!

works a treat now.

Thanks for looking


Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...