send an attachment at vb runtime

chuchazoelrotazo
chuchazoelrotazo used Ask the Experts™
on
How can I send a file via email at runtime in visual basic? (it would be wonderful if the user never sees the outlook window)

thanks for any help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
Create a class module (clsOutlook)

Option Explicit

Private outlook As New outlook.Application
Private mblnTerminateOutlook As Boolean

Private Sub Class_Initialize()
  mblnTerminateOutlook = outlook.ActiveExplorer Is Nothing
End Sub

Private Sub Class_Terminate()
  If mblnTerminateOutlook Then outlook.Quit
  Set outlook = Nothing
End Sub

Public Sub SendMail(strTo As String, strSubject As String, strMessage As String, strCc As String, strBcc As String, Optional vntAttachmentPath As Variant)
    Dim i As Integer
    Dim str As String

    With outlook.CreateItem(olMailItem)
        .To = strTo
        .Subject = strSubject
        .Body = strMessage & vbCrLf & vbCrLf
       
        If strCc <> Empty Then .CC = strCc
        If strBcc <> Empty Then .BCC = strBcc
       
        If IsArray(vntAttachmentPath) Then
            For i = 0 To UBound(vntAttachmentPath)
                .Attachments.Add vntAttachmentPath(i), , Len(.Body)
            Next i
        End If
'        If you want to save the email as file
'        str = ("c:\" & strTo & "-" & strSubject & ".msg")
'        .SaveAs str
       
        .Send
    End With
End Sub


On your form where you want to send your email:
' Declaration
Private mOutlook As New clsOutlook

(replace to,subject,message,cc,bcc,attach to valid strings)

' send mail versturen with attachments
mOutlook.SendMail To, Subject, Message, CC, bcc, attaach

'mail without attachments
mOutlook.SendMail To, Subject, Message, CC, bcc

Dirk HaestProject manager

Commented:
Perhaps what easier:

Here is an example using Outlook, customize the code as neccessary:

Private Sub Command1_Click()
  Dim iOutlook As Outlook.Application
  Dim myApp As Outlook.MailItem
  Set iOutlook = New Outlook.Application
  Set myApp = iOutlook.CreateItem(olMailItem)
  temp = "ryancys"
  txtMessage = "Hello World Again!"
  AttachFile = "C:\Documents and Settings\Administrator\My Documents\My Pictures\next.gif"
  myApp.To = "abc@yahoo.com"
  myApp.Subject = "Hello World!"
  myApp.Attachments.Add AttachFile
  myApp.Body = "Hi " & temp & vbCrLf & vbCrLf & txtMessage
  'myApp.HTMLBody = ""
  myApp.Send
  'Do While iOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderOutbox).Items.Count <> 0
  '    DoEvents
  'Loop
  MsgBox "Successfully Sent", vbInformation, "Email Sent"
End Sub

Alternatively See use tools:

http://www.freevbcode.com/ShowCode.Asp?ID=109 
No comment has been added lately.
To cleanup this TA, I will recommend to moderators that this question is:

*** Answered by: Dhaest ***

Please leave any comments here within the next seven days, or finalize the question if you own it
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
peterchen
EE Cleanup Volunteer

Author

Commented:
thanks to all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial