赞
踩
使用Office Automation,开发者可以利用编程语言(如Visual Basic for Applications(VBA)、C#、Python等)来与Office应用程序进行交互。这些编程语言提供了丰富的API(应用程序编程接口),允许开发者操作文档、工作表、幻灯片等各种对象,以及执行各种操作,比如创建、读取、修改和删除内容。
示例:创建一个新的Word文档,向其中插入了一行文本,然后将文档保存为"example.docx"
#include <windows.h> #include <iostream> #import "C:\Program Files\Microsoft Office\Root\VFS\ProgramFilesCommonX64\Microsoft Shared\OFFICE16\MSO.dll" using namespace Office; #import "C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB" raw_native_types auto_rename #import "C:\Program Files\Microsoft Office\root\Office16\MSPPT.OLB" raw_native_types #import "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE" raw_native_types auto_rename using namespace std; int main() { // 初始化COM库 HRESULT hr = CoInitialize(NULL); if (FAILED(hr)) { cout << "Failed to initialize COM library" << endl; return 1; } try { // 创建 Word 应用程序对象 Word::_ApplicationPtr pWordApp; hr = pWordApp.CreateInstance(__uuidof(Word::Application)); if (FAILED(hr)) { cout << "Failed to create Word application instance" << endl; return 1; } // 设置可见性 pWordApp->Visible = VARIANT_TRUE; // 添加新文档 Word::DocumentsPtr pDocs = pWordApp->Documents; Word::_DocumentPtr pDoc = pDocs->Add(); // 在文档中插入文本 Word::RangePtr pRange = pDoc->Content; pRange->Text = _bstr_t("Hello, World from C++ using Microsoft Office Automation!"); // 保存文档 _variant_t filename("D:\\example.docx"); pDoc->SaveAs(&filename); // 关闭文档 pDoc->Close(); // 退出 Word 应用程序 pWordApp->Quit(); } catch (_com_error& e) { cout << "COM Error: " << e.ErrorMessage() << endl; CoUninitialize(); return 1; } // 释放COM库 CoUninitialize(); return 0; }
参考文档:
· What is the correct, modern, c++ to call the Office automation COM APIs
还有一种不用import库的方式
#include <iostream> #include <windows.h> #include <atlbase.h> #include <atlcom.h> int main() { // 初始化 COM 库 HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (FAILED(hr)) { std::cerr << "Failed to initialize COM library" << std::endl; return 1; } // 创建 Word 应用程序对象指针 IDispatch* pWordApp = NULL; CLSID clsWordApp; // 获取 Word 应用程序的 CLSID hr = CLSIDFromProgID(L"Word.Application", &clsWordApp); if (FAILED(hr)) { std::cerr << "Failed CLSIDFromProgID" << std::endl; CoUninitialize(); return 1; } // 创建 Word 应用程序实例 hr = CoCreateInstance(clsWordApp, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pWordApp); if (FAILED(hr)) { std::cerr << "Failed to create Word application instance" << std::endl; CoUninitialize(); return 1; } // 获取文档集合对象 IDispatch* pDocuments = NULL; { DISPPARAMS params = { NULL, NULL, 0, 0 }; VARIANT result; VariantInit(&result); DISPID dispid; CComBSTR methodName = L"Documents"; // 获取 Documents 属性的 DISPID hr = pWordApp->GetIDsOfNames(IID_NULL, &methodName, 1, LOCALE_SYSTEM_DEFAULT, &dispid); if (SUCCEEDED(hr)) { // 调用 Documents 属性获取文档集合对象 hr = pWordApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL); if (SUCCEEDED(hr)) { pDocuments = V_DISPATCH(&result); } } } // 获取文档数量 long docCount = 0; if (pDocuments != NULL) { { DISPID dispid; CComBSTR szMember = L"Count"; hr = pDocuments->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid); if (SUCCEEDED(hr)) { VARIANT result; VariantInit(&result); DISPPARAMS dispParams = { NULL, NULL, 0, 0 }; hr = pDocuments->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dispParams, &result, NULL, NULL); if (SUCCEEDED(hr)) { docCount = V_I4(&result); } } } } // 打印文档数量 std::cout << "Number of open Word documents: " << docCount << std::endl; // 释放 COM 对象 if (pDocuments != NULL) pDocuments->Release(); if (pWordApp != NULL) pWordApp->Release(); // Uninitialize COM 库 CoUninitialize(); std::cout << "The End" << std::endl; return 0; }
无论哪种方式都只能获取程序中打开的word文档数量,无法获取手动打开的word文档数量。
第2种方法,只是在后台运行,即使代码中加入word可见的设置,依然无法看到word界面。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。