PDA

View Full Version : Attach File To An Email Using VB Code



majdi.marmas
08-12-2014, 11:38 PM
Hi,

I have the code below and I need help to modify it by
1) adding a line telling the macro to attach a cetain file from my desktop ( for example C:\test.pdf )
2) I would like to change the charecters inside the mails for some lines to bold

Can anyone help please. Thank you in advance


Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Sub Notification()
Dim Email As String, Subj As String
Dim Msg As String, URL As String
Dim r As Integer, x As Double

NumOfRows = Sheets("mail template").Cells(Rows.Count, 2).End(xlUp).Row
For r = 2 To NumOfRows


Email = Cells(r, 3)

Subj = "Assigned to You"

Msg = "" & vbCrLf & vbCrLf
Msg = Msg & "Dear XY," & vbCrLf & vbCrLf

Msg = Msg & "Your are requested to contact you department's manager " & Cells(r, 15) & " for review." & vbCrLf & vbCrLf

Msg = Msg & "Please contact the interviewer promptly" & vbCrLf & vbCrLf

Msg = Msg & "Candidate: " & Cells(r, 3) & vbCrLf
Msg = Msg & "Capability: " & "forums" & vbCrLf


Subj = Application.WorksheetFunction.Substitute(Subj, " ", "%20")
Msg = Application.WorksheetFunction.Substitute(Msg, " ", "%20")

Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, "%0D%0A")

URL = "mailto:" & Email & "?cc=mail@mail.com" & "?subject=" & Subj & "&body=" & Msg

ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"
Next r
End Sub

Excel Fox
08-20-2014, 11:22 PM
majdi.marmas, welcome to the ExcelFox community.

Why don't you use one of the more conventional methods of sending a mail, using VBA. You can find a lot of links in this forum (and on the internet generally)

Some examples are

http://www.excelfox.com/forum/f2/send-mail-using-vba-in-excel-and-attach-files-498/

http://www.excelfox.com/forum/f2/excel-vba-to-attach-file-and-automatically-send-email-556/

Excel Fox
08-20-2014, 11:23 PM
Note that these are for sending email via Outlook. If you are using Lotus Notes, or if you are not able to figure out the codes in the threads suggested above, please post back.

majdi.marmas
08-21-2014, 12:30 AM
Thank you very much for your replay, I have find some solution to add to my existing code. I'd like to share it in case someone would like to use it.


URL = "mailto:" & Email & "?Attach=C:\test1.pdf?CC=" & cpy & "?subject=" & Subj & "&body=" & Msg