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

Adding an E-mail attachment

Status
Not open for further replies.

Arob

Technical User
Jul 25, 2001
55
US
I have some code where the end user can automatically send out a report through MS outlook, by clicking a command button. However I was wandering, if instead of automatically sending the E-mail, can you get the code to open outlook if the user needs to send any additional attachments. Below is the code I am using.

Public Sub ExportGraph()

On Error GoTo ExportGraph_Err
Dim strErrMsg As String 'For Error Handling
Dim oleGrf As Object
Dim strFileName As String
Dim strFilter As String
Dim lngFlags As Long
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olMail As Outlook.MailItem

Set olNameSpace = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)
DoCmd.OpenReport "InitialReportForm", acViewPreview
Set oleGrf = Reports("InitialReportForm")

strFileName = ahtCommonFileOpenSave(Flags:=lngFlags, InitialDir:="C:\", _
Filter:="Rich Text Format Files (*.rtf)", FilterIndex:=1, DefaultExt:="rtf", FileName:="MyReport", _
DialogTitle:="Save the Report", OpenFile:=False)

DoCmd.OutputTo acOutputReport, "InitialReportForm", _
acFormatRTF, strFileName, False
MsgBox vbCrLf & "The Report " & strFileName & " has been exported", _
vbInformation + vbOKOnly, "Report exported :"

With olMail
.To = "aroberts@guidemail.com"
.Subject = "Report Info: " & Format(Now(), "dd mmm yyyy hh:mm")
.Attachments.Add strFileName
.ReadReceiptRequested = False
.Send
End With
Kill strFileName

MsgBox vbCrLf & "Chart has been E-Mailed", _
vbInformation + vbOKOnly, "Chart Exported :"


ExportGraph_Exit:
Set olApp = Nothing
Set olMail = Nothing
Set oleGrf = Nothing
Exit Sub
 
Hi. This is how I coded my Automated Email program, it does add previously determined attachment to the email, but I don't know how to make it prompt the user for which file to attach.

If (Not lstrTo = vbNullString) Then
'start outlook application
Set lobjOutlook = CreateObject(Outlook.application")
Set lobjMail = lobjOutlook.createitem(0)

Let lobjMail.subject = lstrSubject
Let lobjMail.HTMLBody = lstrBody 'add body
Let lobjMail.to = lstrTo 'apply To list
'Let objMail.cc = lstrCc 'apply CC list
'Let lobjMail.bcc = lstrBCC 'apply bcc list
Set lobjAttachment = lobjMail.attachments 'add attachments
lobjAttachment.Add &quot;<file path>&quot;
lobjMail.send
lobjOutlook.Quit
End If

I don't know if this helps you or not, I sure hope so. But since you used the SendObject method, I guess it's a bit different. I'll be sure to let u know, if I figure out how to make it promp the users for attachment. The problem is that it's automatic, if you just want it to open Outlook, and then the user does whatever he wants with it, then I guess all u need to do is to open Outlook.
Well, good luck.
Merlin
 
Instead of placing .send at the end of the code, is there a command which would simply open outlook?
 
Hi.
Um, I thought it would have a method like that to just leave it open too, but apparently I can't find any, the .send method is the only one I know what works. So, sorry, can't help u there. I was thinking, it can be done that we can code it so that it asks if the user needs to attach a file, and then pass a boolean to a new method, where we can have the user enter the path. arrrr, this is a little dumb, I don't know, man, just a thought. Sorry I'm not much of help. Hope u find out how to work this. And if u do, plz let me know? thanks. -- Merlin
 
Arob,

I have done this in Groupwise using a YesNo message box and a CommonDialog control to get the path name which is then added to the Attachments collection. Tho the syntax will be different for Outlook, the principle could be the same.

So instead of simply....

.Attachments.Add strFileName

you would have

.Attachments.Add strFileName
Do Until MsgBox(&quot;Do you want to add more attachments?, vbYesNo)=vbNo
NameOfYourCommonDialog.ShowOpen
strFileName=NameOfYourCommonDialog.fileName
If strFileName <> &quot;&quot; then
.Attachments.Add strFileName
End If
NameOfYourCommonDialog.fileName=&quot;&quot;
Loop

Hope that helps!

Craig
 
Hey man, I found out how to do this, it's sooooo ez :)
Um basically the only change we need to make to my previous coding is just use .display method instead of .send method, and get rid of the .quit method at the end. This way, the object will only open an Outlook application, and you can either make it automatically input the e-mail messages or subject and all that, or simply just open application and type those fields in there. But either way, you have the choice of doing whatever you want with the application before you send the e-mail, and that obviously include selecting attachments. This would be so much easier than coding your way around it.
Hope this helps. --- Merlin
 
That did exactly what I wanted it to do. Thanks a lot for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top