C语言 DISPID_WORKBOOK_CLOSE 使用实例
时间: 2023-08-02 08:06:23 浏览: 123
c语言 应用实例
DISPID_WORKBOOK_CLOSE是Microsoft Office提供的一个事件,用于在关闭工作簿时触发。在C语言中使用该事件需要使用Microsoft Office提供的COM接口库。
以下是一个使用DISPID_WORKBOOK_CLOSE事件的C语言示例代码:
```c
#include <windows.h>
#include <ole2.h>
#include <oleauto.h>
#include <stdio.h>
void __stdcall OnWorkbookClose(IDispatch* pWorkbook, VARIANT_BOOL* pCancel)
{
printf("Workbook is closing...\n");
}
int main(int argc, char* argv[])
{
CoInitialize(NULL);
IDispatch* pExcelApp = NULL;
IDispatch* pWorkbook = NULL;
DISPID dispidWorkbookClose;
DISPPARAMS dispparamsNoArgs = { NULL, NULL, 0, 0 };
VARIANT_BOOL bCancel = VARIANT_FALSE;
HRESULT hr;
// 创建Excel应用程序对象
hr = CoCreateInstance(&CLSID_Application, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void**)&pExcelApp);
if (FAILED(hr)) {
printf("Failed to create Excel application object: %x\n", hr);
goto cleanup;
}
// 打开指定的工作簿
VARIANT vtFilename;
VariantInit(&vtFilename);
vtFilename.vt = VT_BSTR;
vtFilename.bstrVal = SysAllocString(L"C:\\test.xlsx");
hr = pExcelApp->Invoke(DISPID_Workbooks_Open, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, &vtFilename, NULL, NULL);
if (FAILED(hr)) {
printf("Failed to open workbook: %x\n", hr);
goto cleanup;
}
// 获取当前活动工作簿
VARIANT vtActiveWorkbook;
VariantInit(&vtActiveWorkbook);
hr = pExcelApp->Invoke(DISPID_Application_ActiveWorkbook, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparamsNoArgs, &vtActiveWorkbook, NULL, NULL);
if (FAILED(hr)) {
printf("Failed to get active workbook: %x\n", hr);
goto cleanup;
}
// 获取工作簿对象的IDispatch接口
pWorkbook = vtActiveWorkbook.pdispVal;
// 获取Workbook对象的Close事件的DispID
LPOLESTR szClose = L"Close";
hr = pWorkbook->GetIDsOfNames(IID_NULL, &szClose, 1, LOCALE_SYSTEM_DEFAULT, &dispidWorkbookClose);
if (FAILED(hr)) {
printf("Failed to get Workbook.Close DISPID: %x\n", hr);
goto cleanup;
}
// 注册Workbook对象的Close事件处理函数
IConnectionPointContainer* pConnectionContainer;
IConnectionPoint* pConnectionPoint;
IUnknown* pUnknown = (IUnknown*)pWorkbook;
hr = pUnknown->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnectionContainer);
if (FAILED(hr)) {
printf("Failed to get IConnectionPointContainer interface: %x\n", hr);
goto cleanup;
}
hr = pConnectionContainer->FindConnectionPoint(IID_WorkbookEvents, &pConnectionPoint);
if (FAILED(hr)) {
printf("Failed to find IConnectionPoint interface: %x\n", hr);
goto cleanup;
}
hr = pConnectionPoint->Advise((IDispatch*)new IDispatchImpl<OnWorkbookClose>, &dispidWorkbookClose, NULL);
if (FAILED(hr)) {
printf("Failed to advise IConnectionPoint interface: %x\n", hr);
goto cleanup;
}
// 等待用户关闭工作簿
while (bCancel == VARIANT_FALSE) {
Sleep(1000);
}
cleanup:
if (pConnectionPoint) {
pConnectionPoint->Unadvise(dispidWorkbookClose);
pConnectionPoint->Release();
}
if (pConnectionContainer) {
pConnectionContainer->Release();
}
if (pWorkbook) {
pWorkbook->Release();
}
if (pExcelApp) {
pExcelApp->Release();
}
CoUninitialize();
return 0;
}
```
以上代码演示了如何使用DISPID_WORKBOOK_CLOSE事件在C语言中处理Microsoft Excel工作簿关闭事件。代码中使用了Microsoft Office提供的COM接口库和C++ ATL库,其中IDispatchImpl是ATL库提供的一个模板类,用于实现COM接口。在实际使用中,还需要根据具体情况修改代码。
阅读全文