Вы находитесь на странице: 1из 2

4/19/2011

Control Outlook from Excel using VBA i…

4/19/2011 Control Outlook from Excel using VBA i… 9 : 54 | Tuesday 19 t h

9 : 54

|

Tuesday 19 th April 2011

Compare Excel tables

Powerful and handy add-on for Excel 2000-2007 files comparison. www.office- excel.com

Ads by Google

Home

About

Excel Tip

Excel 2007 Tutorials

Books

Training

Consulting

Forum

News

Advertise with us

Ads by Google

Access VBA Outlook

Excel VBA Tutorial

Excel Macro

Excel Programming

Microsoft Excel Help

» Control Outlook from Excel using VBA in Microsoft Excel

VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions

CATEGORY - Applications - Word, Outlook in VBA

VERSION - All Microsoft Excel Versions

The two example macros below demonstrates how you can send information to Outlook (e.g. sending an e-mail message) and how you can retrieve information from Outlook (e.g. retrieving a list av all messages in the Inbox).

Note! Read and edit the example code before you try to execute it in your own project!

' requires a reference to the Microsoft Outlook 8.0 Object Library Sub SendAnEmailWithOutlook() ' creates and sends a new e-mail message with Outlook Dim OLF As Outlook.MAPIFolder, olMailItem As Outlook.MailItem Dim ToContact As Outlook.Recipient Set OLF = GetObject("", _ "Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) Set olMailItem = OLF.Items.Add ' creates a new e-mail message With olMailItem .Subject = "Subject for the new e-mail message" ' message subject Set ToContact = .Recipients.Add("name@domain.com") ' add a recipient Set ToContact = .Recipients.Add("name@company.com") ' add a recipient ToContact.Type = olCC ' set latest recipient as CC Set ToContact = .Recipients.Add("name@org.net") ' add a recipient ToContact.Type = olBCC ' set latest recipient as BCC .Body = "This is the message text" & Chr(13)

' the message text with a line break

.Attachments.Add "C:\FolderName\Filename.txt", olByValue, , _ "Attachment" ' insert attachment

olByValue, , _ "Attachment" ' insert attachment Ads by Google MS Excel Tips Excel Macro Sorting
olByValue, , _ "Attachment" ' insert attachment Ads by Google MS Excel Tips Excel Macro Sorting

Ads by Google

MS Excel Tips Excel Macro

Sorting in Excel Excel Formula Help

Excel Tips Excel Macro Sorting in Excel Excel Formula Help ' .Attachments.Add "C:\FolderName\Filename.txt",

' .Attachments.Add "C:\FolderName\Filename.txt", olByReference, , _ "Shortcut to Attachment" ' insert shortcut

' .Attachments.Add "C:\FolderName\Filename.txt", olEmbeddedItem, , _ "Embedded Attachment" ' embedded attachment

' .Attachments.Add "C:\FolderName\Filename.txt", olOLE, , _ "OLE Attachment" ' OLE attachment .OriginatorDeliveryReportRequested = True ' delivery confirmation .ReadReceiptRequested = True ' read confirmation '.Save ' saves the message for later editing

.Send ' sends the e-mail message (puts it in the Outbox) End With Set ToContact = Nothing Set olMailItem = Nothing Set OLF = Nothing End Sub

Sub ListAllItemsInInbox() Dim OLF As Outlook.MAPIFolder, CurrUser As String

Dim EmailItemCount As Integer, i As Integer, EmailCount As Integer Application.ScreenUpdating = False Workbooks.Add ' create a new workbook

' add headings

Cells(1, 1).Formula = "Subject" Cells(1, 2).Formula = "Recieved" Cells(1, 3).Formula = "Attachments" Cells(1, 4).Formula = "Read" With Range("A1:D1").Font .Bold = True .Size = 14 End With Application.Calculation = xlCalculationManual Set OLF = GetObject("", _

"Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) EmailItemCount = OLF.Items.Count i = 0: EmailCount = 0

' read e-mail information

While i < EmailItemCount

i

If i Mod 50 = 0 Then Application.StatusBar = "Reading e-mail messages " & _

= i

+ 1

4/19/2011

Control Outlook from Excel using VBA i…

Format(i / EmailItemCount, "0%") & " With OLF.Items(i) EmailCount = EmailCount + 1 Cells(EmailCount + 1, 1).Formula = .Subject Cells(EmailCount + 1, 2).Formula = Format(.ReceivedTime, "dd.mm.yyyy hh:mm") Cells(EmailCount + 1, 3).Formula = .Attachments.Count Cells(EmailCount + 1, 4).Formula = Not .UnRead End With

"

Wend Application.Calculation = xlCalculationAutomatic Set OLF = Nothing Columns("A:D").AutoFit

Range("A2").Select

ActiveWindow.FreezePanes = True ActiveWorkbook.Saved = True Application.StatusBar = False End Sub

Book Store:

Excel VBA books= True Application.StatusBar = False End Sub Book Store: Accounting books Business Plans MS Office books

Accounting books= False End Sub Book Store: Excel VBA books Business Plans MS Office books Taxes books

Business Plans= False End Sub Book Store: Excel VBA books Accounting books MS Office books Taxes books

MS Office booksBook Store: Excel VBA books Accounting books Business Plans Taxes books VIEW ALL BOOKS Recommended Books:

Taxes booksVBA books Accounting books Business Plans MS Office books VIEW ALL BOOKS Recommended Books: The Fall

VIEW ALL BOOKS

Recommended Books:

The Fall of Advertising and the Rise of PROffice books Taxes books VIEW ALL BOOKS Recommended Books: Accounting for Dummies Accounting and Financial Fundamentals

Accounting for DummiesBooks: The Fall of Advertising and the Rise of PR Accounting and Financial Fundamentals for Nonfinancial

Accounting and Financial Fundamentals for Nonfinancial Executivesof Advertising and the Rise of PR Accounting for Dummies Investing in Real Estate, Fourth Edition

Investing in Real Estate, Fourth Editionand Financial Fundamentals for Nonfinancial Executives Mortgages For Dummies® Understanding Financial Statements

Mortgages For Dummies®Executives Investing in Real Estate, Fourth Edition Understanding Financial Statements Email Tip Print Tip

Understanding Financial Statementsin Real Estate, Fourth Edition Mortgages For Dummies® Email Tip Print Tip   Add to My

Email Tip

Print Tip

 

Add to My Tips

Rate This Tip

 
 
 
 
 
 

1

2

3

4

5

Rating: 3.93

Views: 139968

 

Gambling Online 2006 Bill Hallo you! Great web site. I found exactly the information I was looking for. I

will recommend your page to all my friends!

Tableware Store

Dobraya

hi

Why I can not insert the image into my message?

Almost there Will "requires a reference to the Microsoft Outlook 8.0 Object Library"

How do I set that up? I'm actually using Outlook 2003. Can I get modified code to work with that?

Re: Almost There Nate In your VBA editor (Excel main page, hit Alt + F11) select Tools > References, scroll down to find the highest numbered Microsoft Outlook Object Library, for 2003 it is "Microsoft Outlook 11.0 Object Library".

Click here to post comment

Click here to Regiser

Name

For Registered Users

For Guest

Click here to Regiser Name For Registered Users For Guest Comment Title Comments Terms and Conditions

Comment Title

to Regiser Name For Registered Users For Guest Comment Title Comments Terms and Conditions of use

Comments

Name For Registered Users For Guest Comment Title Comments Terms and Conditions of use The applications/code

Terms and Conditions of use The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Copyright © 2003 ExcelTip.com Microsoft, Microsoft Ex cel is a U.S. registered trademark of Microsoft Corporation