Trying to call a program in the Open Event; how do I determine the currently opened email mailitem m

G

Gary

Hello,

I am new to programming Outlook so forgive me. I am trying to develop
an application using the VBAProject.otm in Outlook 2000.

Sorry for the length of this message and I thank you for taking the
time to read my message.

What I would like to do is when a user opens an email message (double
clicking it for example) in their inbox, I would like to run a program
This other program would be a simple dialog box (form) with 3
buttons: Spam, Move to EmailsToSave folder, and Forward to Other
email. The "Spam" button would delete the selected or currently
opened email. The "EmailsToSave" button would just move the selected
or currently opened email to another folder named "EmailsToSave". And
the last button "Forward to Other email" would forward the email to
another email address and then delete that email. Sounds simple.

I have already designed a UserForm named UserForm1 and added the 3
buttons.

Now I would like to reference the currently opened email but I wasn't
sure how to go about doing this and in turn move this mailitem to the
EmailsToSave folder if the user clicks "EmailsToSave" button.

By the way, how do you get the open even to run when a user opens an
email to read it?

Does the outlook object model have a property such as current reading
email? I don't think so since I don't see it as one of the properties
of the mailitem object. The closest I have come up with is the
ActiveInspector but that doesn't seem to work since it just looks at
what ever the active window is. And that could be a note,
appointment, or task. I am only interested in email messages in the
inbox.

Here is a sample of the code of what I am trying to do, but
unfortunately doesn't work. I copied some of the code from the MSDN.



In the "ThisOutlookSession" module:


Dim m_folderevents As New FolderEvents ‘This is a class
Dim mItemVar As New FolderEvents

Public Sub Application_Startup()
‘runs only once at start up.
m_folderevents.InitFolders Application
End Sub


I also have a class named "FolderEvents' with the following code:


Private WithEvents m_colInboxItems As Outlook.Items
Private WithEvents MItem As Outlook.MailItem

Private Sub Class_Terminate()
Call DeRefFolders
End Sub

Public Sub InitFolders(objApp As Outlook.Application)

Dim objNS As Outlook.NameSpace
Set objNS = objApp.GetNamespace("MAPI")
‘instantiate the object and point to the inbox folder (Items
collection)
Set m_colInboxItems =
objNS.GetDefaultFolder(olFolderInbox).Items

Set objNS = Nothing

End Sub


‘This Item_Open event should run but doesn't when I double click and
open an email.
Private Sub MItem_Open(Cancel As Boolean)
UserForm1.Show

‘MsgBox "inside Open"
End Sub

Private Sub MItem_Read()
MsgBox "inside read"
End Sub



Public Sub DeRefFolders()
Set m_colInboxItems = Nothing
Set MItem = Nothing
End Sub


For the UserForm1 Form object, I have the following code

'Button

Private Sub cmdEmailstoSave_Click()
Dim objNS As Outlook.NameSpace
Dim objInbox As Outlook.MAPIFolder
Dim objQuarFolder As Outlook.MAPIFolder
On Error Resume Next
Set objNS = objItem.Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objQuarFolder = objInbox.Folders.Item("EmailsToSave")
‘if the folder is not there, create it.

If objQuarFolder Is Nothing Then
Set objQuarFolder = _
objInbox.Folders.Add("EmailsToSave")
End If
If Not objQuarFolder Is Nothing Then


‘This is where I get stuck in referencing the currently open email
where the variable objItem represents a mailitem pointing to the
currently opened email. In this code block, the object variable
objItem is attempting to refer to the currently opened email.


If objItem.Class = olMail Then
objItem.Move objQuarFolder
QuarIFrame = True
End If
End If

Set objNS = Nothing
Set objInbox = Nothing
Set objQuarFolder = Nothing
End sub

To sum up, I am using the ThisOutlookSession that declares 2 variables
of the class FolderEvents. In turn, in the open event (Item_Open()),
I have added one line to show my userform1 with the 3 buttons
(UserForm1.show). If the user clicks the EmailsTOSave button I would
like to move that currently opened email that the user is reading to
the EmailsToSave folder.

How to I trap or associated the mail item's open event with the
currently opened email message? I am not using a custom form nor am I
using VBScript. I am using Visual Basic for Application and the
default project file VBAProject.otm.

There has to be a way to run a custom application (such as my form
that either moves the currently opened email to another folder,
deletes it, or forwards it) after the user open that email.

In sum:

1. User opens an email.
2. After opening an email, I want to run a program that displays a
form with 3 buttons to either move that email to another folder
(EmailsToSave), deletes the email message, or forwards that message to
another email.

I am guessing the best place is to declare a mailitem object and
intialize it, but how to you point it (initialize) to the currently
opened email?

If you look at my above code, you will probably note that I don't
initialize the object variable mItem (data type is outlook.mailitem).
I guess the solution would be to initialized this declared object
variable to the "currently" opened email message but I wasn't sure how
to do that and where to do that.

Ex. Set mItem = m_colInboxItems.items(1) ‘This points to the first
email message or the first item in the collection. This may not be the
email the person has opened. For example, I could have opened the
26th email message in my inbox.

Trying Set mItim = the currently selected or opened item in the items
collection (more precisely the currentely opened email message)



I also tried this but again it is most likely wrong (using the
inspector window)

'If TypeName(Application.ActiveWindow) = "Inspector" Then
' Set olApp = GetObject("", "Outlook.Application")
' Set currInspect = olApp.ActiveInspector
' Set MItem = currInspect.CurrentItem
' MItem_Open False ‘force a call to the open event
'End If

Any help is most appreciated. As you can tell I am all over the place
and not sure how to proceed.

My plan is to develop the solution in Outlook Visual Basic for
Applications and then after all the bugs are fixed to copy the code
and put into a Visual Basic 6 ActiveX DLL (changing to a COM Add-in).
But, first I have to get this to work.

Thanks so much again.
 

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