Adding/Using a custom form during the NewInspector event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have tried several ways (lates code snippet is at the end of the message)
and have observered many questions all over the web - but none of the code
snippets seem to apply to my environment exactly. The problem is simple
....when I "open" an "existing" contact I want to add a couple text boxes and
command buttons to the "general" tab. So I figured I would design a custom
contacts form and save it which I did. Ultimately I want to capture the new
inspector event and replace the default contacts form (IPM.Contact) with my
form and populate the existing data into my new form, plus add new
information to the new text boxes that I created.

I found some sample code at http://www.codeproject.com/com/OutlookForms.asp
and I thought I would be able to use that code within the "OnNewInspector"
event. No matter what I do I cannot get the new form to load. the default
form keeps loading. I do want to use the default form for creating NEW
contacts, however to open or view the contact I want to use my custom form.

I do know that deleting or saving a form greatly affects the Outlook
defaults and I truly do not believe this is the correct method to employ.

BTW - if I manually - as a user - Publish the form and make my custom form
the default form it does load my custom form when we want to create a new
contact. So the form does load for a new contact.

The following is a code snippet that I have been playing with all day and I
was wondering if anyone out there knows how to properly replace the contacts
form with a custom form just for the opening and viewing and existing contact?

void __stdcall CConnect::OnNewInspector(IDispatch * /*Outlook::_Inspector**/
Ctrl)
{
try
{
CComQIPtr<Outlook::_Inspector> pInspector(Ctrl);
IDispatchPtr spDisp = pInspector->GetCurrentItem();


Outlook::_ContactItemPtr spContactItem = spDisp;

if(spContactItem != NULL)
{
bstr_t btName(_T("MAPI"));
CComVariant myFolder(DISP_E_PARAMNOTFOUND, VT_ERROR);
Outlook::_ApplicationPtr spApp = pInspector->GetApplication();
Outlook::_NameSpacePtr spNameSpace = spApp->GetNamespace(btName);

Outlook::MAPIFolderPtr spContacts;
Outlook::OlDefaultFolders enumODF = Outlook::olFolderContacts;
spContacts = spNameSpace->GetDefaultFolder(enumODF);

_bstr_t
btCustomFormLocation(_T("C:\\MyCustomForms\\OutlookForm\\MyContact.oft"));

Outlook::_ContactItemPtr spCustomContactItem =
spApp->CreateItemFromTemplate(btCustomFormLocation, myFolder);

Outlook::OlInspectorClose oic;
CComVariant varContacts(spContacts);

_bstr_t btFormDescription(_T("IPM.Contact.MyContact"));

Outlook::OlFormRegistry ofr = Outlook::olFolderRegistry;
Outlook::FormDescriptionPtr spCustomFormDescription =
spCustomContactItem->GetFormDescription();

spCustomFormDescription->PutName(btFormDescription);
spCustomFormDescription->PublishForm(Outlook::olFolderRegistry,
_variant_t(spContacts, false));

Outlook::_ItemsPtr spItems = spContacts->GetItems();

//Outlook::OlInspectorClose oic;

long ItemCount = spItems->GetCount();

int changes =0;
oic = Outlook::olSave;

for(int n = 1; n <= ItemCount; ++n)
{

CComVariant nItem(n);
CComQIPtr <Outlook::_ContactItem> spItem = spItems->Item(nItem);

if(spItem == NULL)
continue; //IPM.DistList

_bstr_t curMsgClass = spItem->GetMessageClass();

TString curMsgClassStr = (const TCHAR*)curMsgClass;
TString oldMsgClassStr = ADDIN_OLDMSGCLASS;
if(curMsgClassStr.compare(oldMsgClassStr) ==0)
{
spItem->PutMessageClass(_bstr_t(ADDIN_NEWMSGCLASS));
spItem->Save();

Outlook::_ContactItemPtr spNewContactItem = spItem->Copy();
spNewContactItem->Close(oic);
spItem->Delete();
changes++;
}
}
}
}
catch(...)
{
}
}
 
First you must publish your custom form with its custom message class. Then
in NewInspector or really more properly in the first Activate event for that
Inspector you can set the message class of the opened item to your custom
message class. You can test for a new item by using
Inspector.CurrentItem.Size which should be 0 for new items or by using
..EntryID, which I prefer and will be a null string until the item has been
saved.

Don't test for that in NewInspector, in that event test for MessageClass or
Class only, most properties of items in the Inspector aren't there or valid
in NewInspector. Once Activate fires you can test for properties of the
item.
 
Hi Ken,

Thanks so much for your reply. My thinning hairline thanks you also. ;-) I
do have a follow-up question if you do not mind and have the time to answer.
I have read so much about saving, deleting and copying contact data that I am
really confused about the proper way to substitute the form. Could you
elaborate on the steps that should happen once the form has been published? I
have read about 4 or 5 different articles that all differ on the approach and
most of the articles will end with statements something like it worked for
Outlook 2002 but not 2003. Or this worked until I restarted Outlook. So
please could you briefly outline the steps necessary during the NewInpsector
event.

Thank you for your help!
 
Back
Top