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]
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:
' 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
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"?