使用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_renameusing 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 应用程序的 CLSIDhr = 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 属性的 DISPIDhr = 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界面。