Send AND Save Email from Access form

  • Thread starter Thread starter Ang
  • Start date Start date
A

Ang

Guest
Hi all,

I found lots on information on how to send an Outlook email by clicking a button on a form. And I got this part working using SendOnject method. A new message window opens already addressed to the contact. (Is a contacts db)

I want to be able to have this email message (or a copy of it) saved in the contact's directory. The dir is something like: "c:\Contacts DB\Send Emails\ " & Me.CompanyName

Every contact has a folder in c:\Contacts DB\Send Emails\ which is the name of the company referenced by the CompanyName field. So I want to save emails sent to this contact in their folder for later reference.

I have looked at this problem for hours every day for about 4 days now, and I'm no closer to finding out where to or how to solve it. So ANY help is appreciated!!

Ang
 
Hi Ang,

Can you use

myMailItem.SaveAs "c:\Contacts DB\Send Emails\ " & Me.CompanyName & "\" & myMailItemName, olMsg

?

HTH,
Keith.
 
Keith,

Thanks so much for your speedy reply. What I have is:

Private Sub btnEmailContact_Click()

Dim msOApp As Outlook.Application
Dim myMailItem As Outlook.MailItem

Set msOApp = CreateObject("Outlook.Application")
Set myMailItem = msOApp.CreateItem(olMailItem)

myMailItem.To = Me.EmailName
myMailItem.SaveAs "c:\Contacts DB\Send Emails\ " & Me.CompanyName & "\" & myMailItemName, olMsg

End Sub

I plan on addin error handlinp code in once this works, but unfortunatly I am new to VB the above gives me a compile error saying 'user-defined type not defined and highlights this bit of code after Dim statement: 'msOApp = CreateObject("Outlook.Application")'

Sorry to be a pain this is probably something simple, but I can't find out what it is. Thanks heaps for your help so far!!
 
Hi,

In your VBA module, click on Tools>References. Tick the box fo something called "Microsoft Outlook 9.0 Object Library" or "Microsoft Outlook 11.0 Object Library".

That should help,
Keith.
 
Keith,

Now I have a myMailItem and use it in a DoCmd like this
DoCmd.SendObject , , myMailItem, Me.EmailName, , , , , , -1

This lets user create their own subject and message in email. And I save it like this:
myMailItem.SaveAs "C:\Contacts DB\Send Emails\Sent Email", Doc
Obviously the trouble with this is the fact that all saved emails will have the same name ie. 'Sent Email'. Also the saved file does not open with word by default, need to select it form 'open with' list.

What you suggested ->
myMailItem.SaveAs "C:\Contacts DB\Send Emails\" & myMailItemName, olMsg does not work at all, but enabling reference to outlook library did!!! (silly me.)

By the way what is myMailItemName? When I used, it always returned a runtime error saying can't write to "C:\Contacts DB\....." and check permissions, but I have, and the folder is NOT read-only.

Thanks for your help so far (I think I owe you a beer...), I'll keep plugging away at it....

Ang
 
Hi,

Glad you are making progress. My intention with the myMailItemName was that you could use a variable (with that name or any other name) which you would set in your script with a unique name for the mail item you are saving as a file. This would avoid the problem that you are always saving a file named "sent email". (myMailItemName does not have any special meaning, if you are using the code as I typed it, without a separate line to assign a value to it, then it will just default to an empty string).

Perhaps you need to decide on a naming convention for the emails you will be saving, and write some VBA code to automatically generate that name, or prompt the user for the name. That's your choice, depending on what your program is going to achieve.

Good luck,
Keith.
 
Keith,

You posts helped me a lot, i got the emails saved where i want them to save like this:

MyItem.To = Me.EmailName
MyItem.Display

MyItem.SaveAs "H:\My Documents\Email Termplates\" & Me.CompanyName _
& "\" & MyItem.Subject & ".msg", OlSaveAsType.olMSG


Beleive it or not the OlSaveAsType.olMSG as the type of the SaveAs function made all the difference. (small thing threw everyting out)

Problem is the email is not saved as the subject the user types in when creating email.... and the subject message is not saved either...

I'll plug away on it, and your help has made a huge difference (its for a humanitarian organisation and things are moving slow here, so this is in my favour!! but i WILL consider all your suggestions :) )

Thanks heaps
Ang
 

Users who are viewing this thread

Back
Top Bottom