vba emails

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

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA Emails

VBA Emails

resolvedResolved · High Priority · Version 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:

Seeing named ranges as part of the zoom

If you have large areas of named ranges this works better.

If you zoom down to 39% you will see your named range.

View all Excel hints and tips


Server loaded in 0.08 secs.