Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

vba emails

ResolvedVersion 2007

Zehra has attended:
Access Intermediate course
Access Advanced course

VBA Emails

Hi
I need to generate an email from a command button on my worksheet.

I need the button to automatically generate an email to a predetermine person and group of persons.
I need the document to be automatically attached but only the active worksheet not the whole workbook.

Thanks

Edited on Wed 15 Oct 2008, 12:48

RE: VBA Emails

Zehra,
This is a bit complicated...

1) Use a Form or a Control-Panel style of worksheet, with recipient names, worksheet-to-send name, and command button.

2) To send a single worksheet, I think you'll need to either
-- create a new workbook, copy the selected worksheet to it, and save, or
-- activate the selected sheet and print it to PDF, if the receipients only need a read-only copy.

3) The actual mailing needs the Outlook library. The code to send an email will look something like this:

Dim olApp As New Outlook.Application
'Picks up running Outlook application
Dim olMsg As Outlook.MailItem
Dim olRec As Outlook.Recipient
Dim olAtt As Outlook.Attachment
...

Set olMsg = olApp.CreateItem(olMailItem)
With olMsg
.Subject = ...
.Body = ...
End With

Set olRec = olMsg.Recipients.Add(addr)
olRec.Type = olTo

Set olAtt = olMsg.Attachments.Add(<filepath>, olByValue)

olMsg.Send

This generates the messages and puts them in your Outbox, but doesn't actually force the sending.

4) In Outlook 2007, go to
>Tools >Trust Center >Programmatic Access
and check the first of the three options. This suppresses warnings provided the Windows Security Center thinks your virus protection is valid and up-to-date. (Note that this doesn't apply to earlier versions of Office).

Hope this helps
/Roy MacLean

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Select blank cells automatically

Get Excel to find any blank (empty) cells in a region for you by:

1. Selecting the appropriate region from your spreadsheet.

2. On the menu bar, go to Edit - Go to.

3. Click the 'Special' button, then select Blanks and click OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.16 secs.