T
Tom at GSD
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:lFolderContacts;
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:lFolderRegistry;
Outlook::FormDescriptionPtr spCustomFormDescription =
spCustomContactItem->GetFormDescription();
spCustomFormDescription->PutName(btFormDescription);
spCustomFormDescription->PublishForm(Outlook:lFolderRegistry,
_variant_t(spContacts, false));
Outlook::_ItemsPtr spItems = spContacts->GetItems();
//Outlook::OlInspectorClose oic;
long ItemCount = spItems->GetCount();
int changes =0;
oic = Outlook:lSave;
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(...)
{
}
}
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:lFolderContacts;
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:lFolderRegistry;
Outlook::FormDescriptionPtr spCustomFormDescription =
spCustomContactItem->GetFormDescription();
spCustomFormDescription->PutName(btFormDescription);
spCustomFormDescription->PublishForm(Outlook:lFolderRegistry,
_variant_t(spContacts, false));
Outlook::_ItemsPtr spItems = spContacts->GetItems();
//Outlook::OlInspectorClose oic;
long ItemCount = spItems->GetCount();
int changes =0;
oic = Outlook:lSave;
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(...)
{
}
}