vba emails
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

VBA Emails

resolvedResolved · High Priority · Version 2007

replyReply Tue 14 Oct 2008, 11:10Delegate Zehra said...

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

For upcoming training course dates see: Pricing & availability

replyReply Wed 15 Oct 2008, 12:47 Edited on Wed 15 Oct 2008, 12:48Trainer Roy said...

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

 

Please browse our web site to find out more about
excel 2003 training and other Microsoft training courses.

Excel tip:

Freeze Rows and Columns to keep lables displayed

You can freeze rows and columns in your worksheet so they don't move.

This allows you to keep row and column labels displayed on your screen as you move through a large worksheet.

Click below and/or to the right of the cell(s) you want to freeze. (NB. Excel freezes ALL the rows above and ALL the columns to left of the selected cell)

Click on the 'Windows' menu and selct 'Freeze Panes'.

Lines appear in your worksheet. The required rows and columns are frozen and remain on your screen as you move through your worksheet.

To unfreeze rows and columns, click on 'Window' menu and select 'Unfreeze Panes'.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard