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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to add attachemnts to outlook email from values in form textbox

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
586
GB
Hello,

I am wishing to add a number of attachments to an email.

I have the code below which sends the email OK, but does not attach the files. The path for the file is storeed in textboxs on the form. Many thanks Mark

Code:
  Dim bStarted As Boolean
  Dim oOutlookApp As Outlook.Application
  Dim oItem As Outlook.MailItem

On Error Resume Next


 'Get Outlook if it's running
 Set oOutlookApp = GetObject(, "Outlook.Application")
 If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
 End If

 'Create a new mailitem
 Set oItem = oOutlookApp.CreateItem(olMailItem)

 With oItem
    'Set the recipient for the new email
   .To = Me.txt_Email_To
   .Subject = Me.txt_Email_Subject
   .Body = Me.txt_Email_Body
   .Attachments.Add = Me.txt_Email_Attachment_1
   .Attachments.Add = Me.txt_Email_Attachment_2
   .Attachments.Add = Me.txt_Email_Attachment_3
            
   .Send
 End With

 Me.chk_email_sent = True
 
 'If bStarted Then
 '    'If we started Outlook from code, then close it
 '   oOutlookApp.Quit
 'End If

'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
 
Based on this site, this is my guess:

Code:
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem[blue]
Dim myAttachments As Outlook.Attachments [/blue]

On Error Resume Next
[green]
 'Get Outlook if it's running[/green]
 Set oOutlookApp = GetObject(, "Outlook.Application")
 If Err <> 0 Then[green]
    'Outlook wasn't running, start it from code[/green]
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
 End If
[green]
 'Create a new mailitem[/green]
 Set oItem = oOutlookApp.CreateItem(olMailItem)
[blue]Set myAttachments = oItem.Attachments [/blue]

 With oItem
    [green]'Set the recipient for the new email[/green]
   .To = Me.txt_Email_To
   .Subject = Me.txt_Email_Subject
   .Body = Me.txt_Email_Body[blue]
   myAttachments.Add Me.txt_Email_Attachment_1 
   myAttachments.Add Me.txt_Email_Attachment_2 
   myAttachments.Add Me.txt_Email_Attachment_3 [/blue]
[green]'   .Attachments.Add = Me.txt_Email_Attachment_1
'   .Attachments.Add = Me.txt_Email_Attachment_2
'   .Attachments.Add = Me.txt_Email_Attachment_3
       [/green]     
   .Send
 End With

 Me.chk_email_sent = True
 [green]
 'If bStarted Then
 '    'If we started Outlook from code, then close it
 '   oOutlookApp.Quit
 'End If

'Clean up[/green]
Set oItem = Nothing
Set oOutlookApp = Nothing


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top