Object declarations

S

Sierk

Hi,

I have gotten my customization for Outlook to work (VB) for the most part,
and I am trying to clean things up. I am challenged by the declaration of
object variables. I get an error on the following line of code (Option
Explicit is set)

Set myItem = ActiveExplorer.Selection.Item(1)

With the following declaration

Dim myItem As Outlook.Explorer

If I declare myItem as Variant it works fine. I just would like to know
what the proper declaration for these types of object variables is. I am
having similar problems with Word Document Objects. Also if there is a good
white paper describing declaration of variables in this environment I'd much
like to know its location.

Thanks
 
K

Ken Slovak - [MVP - Outlook]

Dim myItem As Outlook.Explorer

Set myItem = ActiveExplorer

The items in a Selection collection can be anything, depending on the item
and the folder. For example a contacts folder could hold contacts as well as
distribution lists. Inbox can hold meeting or task requests, NDR's, etc. in
addition to email or post items.

So usually what one does for an item like that is declare it as Object and
test it to see what type of object before early binding it to a specific
type such as MailItem. I usually test for item.Class for that.

The Object Browser is the invaluable tool for seeing object declarations,
types, method signatures, etc.
 
S

Sierk

Thanks Ken, but I think I am going to need a bit more. I checked out the
item.class object property and it returned 34. How do I find the appropriate
constants representing the values. In my case a Contact Item.

Then also, How do I access the properties like "Account" since
strAccount = myItem.Account
no longer works?
 
K

Ken Slovak - [MVP - Outlook]

Again, use the Object Browser.

If you look up say ContactItem.Class it points you to the OlObjectClass
enumeration, where you find that 34 is equal to Explorer.

Now since you can't get an Explorer from ActiveExplorer.Selection.Item(1) I
have no idea where you're reading item.Class from but it certainly is not
from Selection.Item(1).

A ContactItem is class 40 (olContact).

Account is an string property on a ContactItem object. So strAccount =
item.Account is valid only for contact items. For a MailItem you can use
item.SendUsingAccount, which returns an Account object, not a string. All of
your accounts are in the NameSpace.Accounts collection, which is a
collection of Accounts.

All of this information I just got from the Object Browser.
 
S

Sierk

Thanks Ken,
As you can see the Object browser is pretty new to me, and I find it
difficult to use especially since it only provides the cursory relationship
information. Perhaps I’ll get used to it after using it a while.

The following is what I started with, and believe it or not it works. For
clarification, I invoke the function after I have selected a contact (or made
it active) from the contact list. The idea is to create a preformatted
address text block that can be inserted in letters envelopes etc.

Sub GetContactAddressData()
Dim myItem As Outlook.ContactItem
Dim strName As String

'Select active address card (Explorer Item)
Set myItem = ActiveExplorer.Selection.Item(1)

If myItem Is Nothing Then
MsgBox "Please select a Contact item before running this function."
GoTo ExitProc
End If

'Collect personal information from the contact item
strName = myItem.Account
Etc.

I think the reason my class was returning 34 before was because I specified
an explorer object when I tried what you suggested as follows.

Dim myItem As Outlook.Explorer
Set myItem = ActiveExplorer
MsgBox myItem.Class

To access the active Contact, according to the object library I would have
to use the application class. But I am not sure how to select the active
contact window using the application class. Can you suggest how to do that?
 
K

Ken Slovak - [MVP - Outlook]

ActiveInspector is the currently active item window (Inspector).
 
S

Sierk

Ken,

If ActiveInspector is the active item window then how do I extract the
contact information. I don't see anything in the object browser that I can
use to get to the contact information. Also, It would probably help a lot
if you had some sample code so I can see the big picture. I find it
extremely challenging working in an environment that gives you nothing but
little puzzle pieces, as much as I like puzzles and games.
 
K

Ken Slovak - [MVP - Outlook]

The Object Browser help does have lots of code samples and snippets on
almost every property/method/event that's available you know. Just press F1
on a property to get to that.

For ActiveInspector. Every Inspector has a CurrentItem property that returns
the item being displayed in the Inspector. So if you only want contact items
this code snippet would work:

Dim obj As Object
Set obj = ActiveInspector.CurrentItem
If obj.Class = OlObjectClass.OlContact Then
' a contact item
Dim oContact As Outlook.ContactItem
Set oContact = obj
' now get to contact properties
Else
' not a contact
MsgBox "This is not a contact"
End If
 
S

Sierk

Thanks Ken,
Use of the object browser is beginning to make sense. The following code is
what I am using to test your suggestions. With it I get an error, '91' see
below, on the "Set obj = ActiveInspector.CurrentItem" line of code. Can you
tell me what I am missing? Also does it matter if variables, oContact in
particular, are declared in the code, after the if then as you did in the
sample you gave me, or at the beginning of the subroutine as I prefer to do?

Public Sub GetContactTitleData()
Dim obj As Object
Dim oContact As Outlook.ContactItem

Set obj = ActiveInspector.CurrentItem
If obj.Class = OlObjectClass.olContact Then
' a contact item
Set oContact = obj
MsgBox oContact.Account
Else
' not a contact
MsgBox "This is not a contact"
End If
End Sub

Run-time Error '91': Object variable or With block variable not set
 
S

Sierk

Got the answer. Found it in
http://blogs.msdn.com/synergist/arc...ok-appointment-based-on-an-email-message.aspx
in a message posted on Saturday, August 04, 2007 5:09 PM by a Gary. Per his
suggestion I did the following:

Created a Function to retrieve the current item:

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application

Set objApp = CreateObject("Outlook.Application")
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
Case Else
' anything else will result in an error, which is
' why we have the error handler above
End Select

Set objApp = Nothing
End Function
'From http://www.outlookcode.com/codedetail.aspx?id=50

Then invoked the function in my code as follows

Public Sub GetContactTitleData()
Dim oContact As Outlook.ContactItem

Set oContact = GetCurrentItem()
MsgBox oContact.Account

End Sub

I'll have to include some error trapping but I think the fundamentals are
here, and they seem to work. Thanks to all who helped, especially Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top