File attachment error sending CDO mail with VBA

mexmanz

Board Regular
Joined
Sep 19, 2005
Messages
72
Hello,

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("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
= "123.456.789.0"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
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 = "contact@mail.com"
.CC = ""
.BCC = ""
.From = "contact2@mail.com"
.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

.Send
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").Select
Sheets("DataChangeRequestForm").Protect Password:="password"
Sheets("DataChangeRequestForm").Copy

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
CDO_Email_Change_Request_Form.CDO_Mail_Form

End Sub
‘----------------------------




Thank you very much for your help.
 

mexmanz

Board Regular
Joined
Sep 19, 2005
Messages
72
Note:

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)
 

mexmanz

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

Should have applied ALL the code together in the module!

works a treat now.

Thanks for looking

regards
 

Forum statistics

Threads
1,082,269
Messages
5,364,148
Members
400,783
Latest member
sambills

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...
Top