PC Review


Reply
Thread Tools Rate Thread

Code for sending Outlook email with attachments using a list

 
 
craig
Guest
Posts: n/a
 
      13th Dec 2007
I have received the attached code which will send an email with an
attachment, using a list that contains recepient name, email address,
and attachment path. My job is using email addresses that are
contained in my global address book, and would prefer to use the name
instead of the email address. What code needs to be changed to make
this work.

Sub Send_Files()
'Working in 2000-2007
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim sh As Worksheet
Dim cell As Range, FileCell As Range, rng As Range
Dim strSubject, strBody, strNote, StrMessage

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set sh = Sheets("Sheet1")

Set OutApp = CreateObject("Outlook.Application")
'OutApp.Session.Logon

strSubject = InputBox("Please enter the subject of today's mail:",
"Message Subject Entry", "")
strNote = ""
StrMessage = InputBox("Please enter message here:", "Message
Entry", "")
strBody = strNote & Chr(10) & _

For Each cell In
sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

'Enter the file names in the C:Z column in each row
Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(olMailItem)

With OutMail
.SentOnBehalfOfName = ""
.To = "cell.Value"
.Subject = strSubject
.Body = strBody
For Each FileCell In
rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Send 'Or use Display
End With

Set OutMail = Nothing
End If
Next cell

Set OutApp = Nothing

With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      13th Dec 2007
Hi Graig

Try this instead of .To

..Recipients.Add "TheName"

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"craig" <(E-Mail Removed)> wrote in message news:1a764aa4-6f08-4635-a607-(E-Mail Removed)...
>I have received the attached code which will send an email with an
> attachment, using a list that contains recepient name, email address,
> and attachment path. My job is using email addresses that are
> contained in my global address book, and would prefer to use the name
> instead of the email address. What code needs to be changed to make
> this work.
>
> Sub Send_Files()
> 'Working in 2000-2007
> Dim OutApp As Outlook.Application
> Dim OutMail As Outlook.MailItem
> Dim sh As Worksheet
> Dim cell As Range, FileCell As Range, rng As Range
> Dim strSubject, strBody, strNote, StrMessage
>
> With Application
> .EnableEvents = False
> .ScreenUpdating = False
> End With
>
> Set sh = Sheets("Sheet1")
>
> Set OutApp = CreateObject("Outlook.Application")
> 'OutApp.Session.Logon
>
> strSubject = InputBox("Please enter the subject of today's mail:",
> "Message Subject Entry", "")
> strNote = ""
> StrMessage = InputBox("Please enter message here:", "Message
> Entry", "")
> strBody = strNote & Chr(10) & _
>
> For Each cell In
> sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
>
> 'Enter the file names in the C:Z column in each row
> Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
>
> If cell.Value Like "?*@?*.?*" And _
> Application.WorksheetFunction.CountA(rng) > 0 Then
> Set OutMail = OutApp.CreateItem(olMailItem)
>
> With OutMail
> .SentOnBehalfOfName = ""
> .To = "cell.Value"
> .Subject = strSubject
> .Body = strBody
> For Each FileCell In
> rng.SpecialCells(xlCellTypeConstants)
> If Trim(FileCell) <> "" Then
> If Dir(FileCell.Value) <> "" Then
> .Attachments.Add FileCell.Value
> End If
> End If
> Next FileCell
> .Send 'Or use Display
> End With
>
> Set OutMail = Nothing
> End If
> Next cell
>
> Set OutApp = Nothing
>
> With Application
> .EnableEvents = True
> .ScreenUpdating = True
> End With
> End Sub

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending email attachments using Outlook Boonchu Microsoft Outlook Discussion 0 13th Mar 2008 01:17 PM
Outlook 2007 lockup when sending a new email with attachments JamesM Microsoft Outlook Discussion 0 12th Feb 2008 04:08 PM
sending email attachments through microsoft outlook =?Utf-8?B?SkQ=?= Microsoft Word Document Management 1 5th Oct 2007 05:22 AM
Problem sending out email with attachments (OutLook) koh Microsoft Outlook Discussion 1 17th Jun 2004 02:43 AM
corrupt attachments when sending email with Outlook 2003 George Microsoft Outlook 4 16th Apr 2004 04:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.