Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
| Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 26th, 2004, 02:10 PM
Authorized User
Join Date: Jul 2004
Location: New Providence, NJ, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help w/ "WITHEVENTS" & the Outlook Obj

Beginner here....

I am working in Office 2003 Prof & Excel VBA, and am trying to write a procedure to open Outlook. I have written procedures to open just about any other application I want, but have hit a wall w/ Outlook.

Despite what some of the Outlook & MSDN documentation seems to indicate, it is NOT as simple as:

Dim myOlApp as New Outlook.Application, _
     mySpace as Outlook.NameSpace
Set mySpace = myOlApp.GetNameSpace("MAPI")


Dim myOlApp as Outlook.Application
Set myOlApp = CreateObject("Outlook.Application")

When I run code along these lines I get a Visual Basic pop-up which says:

    Run-time error [long # follows]

    Automation error
    The specified module could not be found

The error message is always generated at the first instance (after DIM statements) of code which employs the "myOlApp" variable. (FYI, yes, I HAVE referenced the MSFT Outlook 11.0 object library under the VBA Editor tools menu.)

In digging and digging and digging on this I have run across some documentation which indicates that in order to fire up Outlook I must first create a class module which employs the WITHEVENTS statement.

So.... I have dutifully created a class module & cleverly named it Outlk_Class. The first (and so far only) line of code in this module is:

     Public WithEvents OlApp As Outlook.Application

I have then used the following procedure in my regular module:

Sub OpenOutlook()

' Declare variables...
Dim myOlClass As New Outlk_Class, _
mySpace As Outlook.Namespace, _
myMapi As Outlook.MAPIFolder, myOlFolders As Outlook.Folders

' Have tried two ways to _
initialize/instantiate/fire up objects/variables _
(I am not sure of the right terminology here)...
Set myOlClass.OlApp = New Outlook.Application '1st method
Set myOlClass.OlApp = CreateObject("Outlook.Application") '2nd method

End Sub

Under either method, though, I get the same error message as above, i.e., "the specified module could not be found". This error message occurs even though I am prompted with "OlApp" in the VB Editor after I type "myOlClass" and then a period.

My guess is that the problem lies with how I should be coding the Outlk_Class class module. I have again done a huge amount of digging through the MSDN, my Wrox book, and a book I have on Excel VBA. However, I am totally mystified and frustrated here.

After entering my WITHEVENTS statement in the class module, I of course then have "Class" and "OlApp" appear in the code editor's Object drop-down list. Clicking on "Class" generates an empty "Class_Initialize ()" procedure, and clicking on "OlApp" allows me to generate up to 10 empty procedures. I assume the most meaningful of of those for me would be the empty "OlApp_Startup()" procedure.

My questions then boils down to:

1) Am I on the right track about needing a class module and a WITHEVENTS statement in order to automate Outlook?
2) If so, what *(#%@-ing code should I include in the empty procedures in my class module?
3) Given all that, how do I write my code so that Outlook is launched using, for example, a profile entitled "Art", a pst file named "Art_Olk.pst" and a password of "somedumbpassword"?


Old October 27th, 2004, 03:50 AM
Authorized User
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

the keyword "WithEvents" is used to allow you to respond to any events your COM object exposes, eg. in Outlook you may want to perform some checks each time a new mail message is created, in which case you would implement the NewMail event of the Outlook Application object.

Bearing that in mind, yes: you are barking up the wrong tree. Your original attempt at creating the Outlook Application object (using CreateObject and/or the New keyword) is correct. Not sure why it's failing: perhaps a bad install?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Friend WithEvents kscase Visual Basic 2005 Basics 1 May 3rd, 2007 03:16 PM
Handles clause requires a WithEvents on BUTTON?!?! Ron Howerton ASP.NET 2.0 Basics 1 April 24th, 2007 03:24 PM
ASP & Outlook Web Access jolo98 Classic ASP Basics 0 October 8th, 2004 04:43 AM
Access & Outlook Ailmanki Access VBA 1 April 7th, 2004 08:31 AM
Loading Images & Interact with Outlook wasaan SQL Server ASP 1 August 4th, 2003 06:35 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.