Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

attaching a file 1

Status
Not open for further replies.

leckie

Programmer
Apr 19, 2001
65
GB
I can create an email using data from within a form and send it but can't seem to find the code to attach a file

any help muchly appreciated

 
Hi Leckie,

Hope this helps. It will take the name of multiple recipients separated by semi-colons( it'll cc those after the first ) and attach the file stored in attachment to each email. It'll then send it to everyone.

Eg,

SendEmail "Bob; Marge; Carol; Sue", "C:\Mydocuments\Pants.gif"

Let me know if this is what you want,

Jes

-------------------------------------

Private Sub SendEmail(recipient As String, attachment As String)
On Error GoTo Err_Sub
Dim line As Integer
Dim place As Integer
Dim firstdone As Boolean
Dim ccname As String
Dim rcpname As String
Dim ccstring As String

firstdone = False

Dim ol As Outlook.Application
Dim olmsg As Outlook.MailItem
Dim olrcp As Outlook.recipient
Dim olatt As Outlook.attachment

Set ol = CreateObject("outlook.application")
Set olmsg = ol.CreateItem(olMailItem)

With olmsg

place = InStr(recipient, ";")
If place = 0 Then
Set olrcp = .Recipients.Add(recipient)
olrcp.Type = olTo
Else
ccstring = recipient
Do
rcpname = Left(ccstring, place - 1)
If Len(rcpname) > 0 Then
If firstdone = False Then
Set olrcp = .Recipients.Add(rcpname)
olrcp.Type = olTo
firstdone = True
Else
Set olrcp = .Recipients.Add(rcpname)
olrcp.Type = olCC
End If
End If
ccstring = Right$(ccstring, Len(ccstring) - place)
place = InStr(ccstring, ";")
Loop Until place = 0

If Len(ccstring) > 0 Then
Set olrcp = .Recipients.Add(ccstring)
olrcp.Type = olCC
End If

End If

.Subject = "Email Subject"

.Body = "This email should contain an attachment"

Set olatt = .Attachments.Add(attachment)

For Each olrcp In .Recipients
olrcp.Resolve
Next

.Save
.Send

End With

Exit_Sub:
Set ol = Nothing
Exit Sub

Err_Sub:
MsgBox Err.Description, , "Sending Email line " + Str(line)
Resume Exit_Sub
End Sub
 
Jes,

What a star . . .

that worked a treat, I've been through the visual basic programmers guide and copied in their sample code but it falls over on the attachment element . .

thankyou . . . . . .

Leckie


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top