Referencing unsent mail item from a form

  • Thread starter Thread starter Ron Abramson
  • Start date Start date
R

Ron Abramson

I want to pop a form in response to an ItemSend event and then take data
entered in the form and insert it into the body of the (unsent) item,
i.e., the item that opened the form.

How do I reference that item from the Click() routine of the form's
Submit button? If I just try to assign a string value to Item.Body I get
an 'Object Required' error. Obviously the unqualified reference to Item
is out of scope...
 
I want to pop a form in response to an ItemSend event and then take data
entered in the form and insert it into the body of the (unsent) item,
i.e., the item that opened the form.

How do I reference that item from the Click() routine of the form's
Submit button? If I just try to assign a string value to Item.Body I get
an 'Object Required' error. Obviously the unqualified reference to Item
is out of scope...

Answered myself from p. 264 of Sue Mosher's book...
Dim objApp As Outlook.Application
Dim objItem As Object
Set objApp = CreateObject("Outlook.Application")
Set ObjItem = objApp.ActiveInspector.CurrentItem
 
Hi Ron,

if the user changes the current item while your form is be displaying
then maybe your code won´t treat the proper item.

If you like to ensure the treatment of that item, the event corresponds
to, so you just need to pass it to the form.

Here is an example with a Show method in the form:

<YourUserForm>
Private m_oMail as Outlook.MailItem

Public Sub ShowForm(oMail As Outlook.MailItem)
Set m_oMail = oMail
Show vbModal
End Sub

Private Sub cmdOK_Click()
' [Here´s your code]
Unload Me
End Sub
</YourUserForm>

<ThisOutlookSession>
Private Sub Application_ItemSend(ByVal Item As Object, _
Cancel As Boolean)
Dim fmYourForm As YourUserForm

If TypeOf Item Is Outlook.MailItem Then
Set fmYourForm = New YourUserForm
fmYourForm.ShowForm Item
End If
End Sub
</ThisOutlookSession>
 
Another approach: ItemSend passes an Item object that represents the item
being sent. Your class module that invokes the form could also expose that
item as a property of the class.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Yes, this looks more robust. The scenario of the user changing the
current item while filling out the form is bound to happen sooner or
later, I agree.
 
Back
Top