Add Attachment to Outlook VBA

Neilster

Registered User.
Local time
Yesterday, 19:41
Joined
Jan 19, 2014
Messages
218
Hi Guy's

How do i add an attach file to this piece of code.:D:D

Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem

Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\User\documents\Template.msg")

MyItem.Subject = Nz(Me.txtCustomerEmailAddress1)
MyItem.To = Nz(Me.TxtContactName)
MyItem.Display

End With
 
myItem.Attachments.Add "c:\folder\file.xls", olByValue, 1
 
Try
MyItem.attachments.Add (Variable_AttachmentFile)
 
Thanks guy's ive sorted it, cheers for your help
 
What i need is a the file path for and desk top user.

Hope you can help.
 
Google vba environment variables
 
I'm trying this but not working..

Dim User As String

Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\" & User & "\documents\Template.msg")
 
Where are you getting the value for User ?

You need to set it, and I would avoid using User as your variable name.

Something like sUserName = CreateObject("wscript.network").username
 
Ultamitly the database will be set on a shared network and then the Template.msg will be set in each users documents
 
Hi Guys i have tried this. but i get a compile error sub or function not defined. on thi line - Call writeTemplate(filesPath)

Dim myOlApp As Outlook.Application
Dim myitem As Outlook.MailItem
Dim n As Integer
Dim sUsername As String

sUsername = Environ$("username")
Dim filesPath As String
Set myOlApp = CreateObject("Outlook.Application")
filesPath = Environ$("USERPROFILE")
Call writeTemplate(filesPath)
Set myitem = myOlApp.CreateItemFromTemplate(filesPath & "\Template.msg")

With myitem

For n = 0 To Me.EmailList.ListCount - 1
.Attachments.Add (Me.EmailList.ItemData(n))
Next n

.Display

myitem.Subject = Nz("")
myitem.To = Nz(Me.txtCustomerEmailAddress1)
myitem.Display

End With
 
Last edited:

Users who are viewing this thread

Back
Top Bottom