Microsoft Outlook 加载项,带对话框和数据库连接






3.67/5 (8投票s)
2004年10月4日
3分钟阅读

60065

667
带对话框和数据库连接的 Microsoft Outlook 加载项。
先决条件
请先阅读我之前的加载项教程,然后再继续。在继续之前,还应了解 ADODB 概念。
引言
以前,我发布了两个关于加载项的教程,现在是时候更深入地研究了,也就是使用其他技术。在本教程中,我们将把一些详细信息添加到 Access 数据库中,并通过加载项查看和添加该数据库中的详细信息。我们使用 ADODB 连接。
由于本教程展示了如何在数据库中执行读取和查找特定值的操作,因此同样的操作也可以应用于删除和修改。也许很快就会添加。本教程还告诉您如何调用 Inspector 事件。
ADODB
ADODB 库是一个小的、轻量级的库,它包含核心对象,并提供建立连接、发出命令和检索记录集的基本功能,它还支持记录集导航。您可以使用它来执行基本维护任务,例如修改、添加和删除记录。该库的非分层设计使初学者易于使用。
使用代码
数据库
数据库在 CMyDatabase
类中实现。
- 要使用 ADODB 连接,首先我们必须将 ADODB 导入到我们的程序中,因此负责此操作的行是
#import "C:\Program Files\Common Files\System\ADO\msado15.dll " no_namespace name ("EOF","EndOfFile")
路径可能因系统而异。搜索 msado15.dll,如果出现错误,请将路径放在那里。
- ADODB 中需要的两个基本要素是连接指针和记录集指针。
_ConnectionPtr m_pCon; _RecordsetPtr m_pRs;
- 现在我们必须建立与数据库的连接。
GetDefaultConnect()
执行此操作。void CMyDatabase::GetDefaultConnect() { //create an instance for ADODB connection HRESULT hr=m_pCon.CreateInstance("ADODB.Connection"); if( SUCCEEDED(hr) ) { //open the Database hr=m_pCon->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Address.mdb;", "","",adModeUnknown); } }
- 现在打开后,我们可以按如下方式读取值
void CMyDatabase::Read(CStringArray* values) { //connect the database GetDefaultConnect(); //variables _bstr_t bstrQuery; _variant_t isAffected; _variant_t vEmail; _variant_t vName; values->RemoveAll(); //the query we want to execute bstrQuery="SELECT * FROM Addr"; //Execute the query and it will return //a RecordSet so that we can scroll //through the result one by one m_pRs=m_pCon->Execute(_bstr_t(bstrQuery), &isAffected, adOptionUnspecified); //traverse the recordset till the end of file while( !m_pRs->GetEndOfFile() ) { //collect the values by the order //of the field names (i.e)Coulmn names vEmail=m_pRs->GetCollect(L"Email"); vName=m_pRs->GetCollect(L"Name"); //store them in stringarray values->Add( (char*) ((_bstr_t)vEmail) ); values->Add( (char*) ((_bstr_t)vName) ); //move to next record m_pRs->MoveNext(); } //close the recordset m_pRs->Close(); }
- 将新数据添加到数据库的方法如下
void CMyDatabase::Add(PSTR pMail, PSTR pName) { //Connect to the database GetDefaultConnect(); //variables _variant_t vNull; vNull.vt=VT_ERROR; vNull.scode=DISP_E_PARAMNOTFOUND; _bstr_t bstrQuery("SELECT * FROM Addr"); long rIndices[2]; //create the recordset m_pRs.CreateInstance(__uuidof(Recordset)); //put our Active Database Connection to our Connection Pointer m_pRs->PutRefActiveConnection(m_pCon); //open the connection successfully with the query m_pRs->Open(_variant_t(bstrQuery),vNull,adOpenForwardOnly, adLockOptimistic,adCmdText); //first add the field names which is in our database COleSafeArray vaFields; vaFields.CreateOneDim(VT_VARIANT,2); rIndices[0]=0; vaFields.PutElement(rIndices,&(_variant_t("Email"))); rIndices[0]=1; vaFields.PutElement(rIndices,&(_variant_t("Name"))); //next add the values for the above added field names respectively COleSafeArray vaValues; vaValues.CreateOneDim(VT_VARIANT,2); rIndices[0]=0; vaValues.PutElement(rIndices,&(_variant_t((CString)pMail))); rIndices[0]=1; vaValues.PutElement(rIndices,&(_variant_t((CString)pName))); //now add the field name and it's respective value to the database m_pRs->AddNew(vaFields,vaValues); //close the connection m_pCon->Close(); AfxMessageBox("Details Added"); }
- 如果使用 "
WHERE
" 选项更改查询,则可以执行搜索操作。
检查器事件
Microsoft Outlook 中有两个窗口
- 资源管理器窗口
- 检查器窗口
两者都有各自的回调和事件。在收件箱或发件箱中单击邮件后,打开的窗口称为检查器窗口。
想想跟踪这个新的检查器事件会多么好??
程序如下
- 我们的加载项类应该实现 InspectorEvents 来跟踪它们,所以在 Implements 部分添加这行代码
CAddin : public IDispEventSimpleImpl<1,CAddin,&__uuidof(Outlook::InspectorsEvents)>
typedef
使工作更简单typedef IDispEventSimpleImpl</*nID =*/ 1,CAddin, &__uuidof(Outlook::InspectorsEvents)> MyInspectorsEvents;
- 将其添加到 Sink Entry 中
BEGIN_SINK_MAP(CAddin) SINK_ENTRY_INFO(1, __uuidof(Outlook::InspectorsEvents), /*dispid*/ 0xf001, NewInspector, &OnInspectorInfo)
OnInspector
函数是一个外部函数,它将保存有关我们的 Inspector 函数的信息,并编写为extern _ATL_FUNC_INFO OnInspectorInfo;
NewInspector
是我们的实际函数,它将在发生新的 Inspector 事件时被调用。 - 从我们的
Application
对象中,我们得到Inspectors
对象spApp->get_Inspectors(&spInspectors);
- 最后,建议 Inspector 事件
HRESULT hr=MyInspectorsEvents::DispEventAdvise((IDispatch*)spInspectors);
- 事件函数信息如下
_ATL_FUNC_INFO OnInspectorInfo ={CC_STDCALL,VT_EMPTY,1,{VT_DISPATCH}};
- 事件函数如下
void __stdcall CAddin::NewInspector(IDispatch* pdispInspector) { AfxMessageBox("New Inspector Event Occured"); }
就是这样!...我们已经完成了另一个带有数据库连接的教程!
参考
Microsoft Developer Network (MSDN)
我的联系方式
注意
本教程已在 Office XP 中检查过,如果按照以前的教程更改导入文件,它可以在 Office 2000 中运行。我不能保证它是否能在 Office 2003 中运行。
如果有人有为 Office 2003 编写的加载项,请发送给我。我尝试了 CodeProject 中给出的教程,但它不起作用!!!!