void CmdeskDlg::OnBnClickedTestbutton() { // TODO: Add your control notification handler code here //WriteDB(); // TODO: Add your control notification handler code here CApplication oApp; CDocuments oDocs; CDocument0 oDoc; if (!oApp.CreateDispatch(_T("Word.Application"), NULL)) { AfxMessageBox(_T("启动Word程序失败!")); exit(1); } //查看自动化过程 oApp.put_Visible(true); oDocs = oApp.get_Documents(); COleVariant varOPt(DISP_E_PARAMNOTFOUND, VT_ERROR); COleVariant varStartLine, varEndLine; varStartLine.intVal = 2; varEndLine.intVal = 50; //添加一个新文档 oDoc = oDocs.Add(varOPt, varOPt, varOPt, varOPt); //获取文档区域 CRange range = oDoc.Range(varStartLine, varEndLine); UpdateData(TRUE); range.put_Text(_T("DDDDDDDDDDDDDDDDDSS1234")); //保存docx文档 try { oDoc.SaveAs(COleVariant(_T("D:\\conn\\TEMP.DOCX")), varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt, varOPt); } catch (COleException* e) { LPVOID lpMsg; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, e->m_sc, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsg, 0, NULL); ::MessageBox(NULL, (LPCTSTR)lpMsg, _T("COM Error"), MB_OK|MB_SETFOREGROUND); ::LocalFree(lpMsg); } catch(COleDispatchException *e) { TCHAR msg[512]; wsprintf(msg, _T("程序运行出错'%d',系统提示信息为:\n\n%s"), e->m_scError & 0x0000FFFF, e->m_strDescription); ::MessageBox(NULL, msg, _T("无法保存文件"), MB_OK|MB_SETFOREGROUND); } oDoc.Close(varOPt, varOPt, varOPt); oApp.Quit(varOPt, varOPt, varOPt); }
#include "MsWord/CApplication.h" //word程序对象 #include "MsWord/CDocuments.h" //文档集对象 #include "MsWord/CDocument0.h" //docx对象 #include "MsWord/CSelection.h" //所选内容 #include "MsWord/CCell.h" //单个单元格 #include "MsWord/CCells.h" //单元格集合 #include "MsWord/CRange.h" //文档中的一个连续范围 #include "MsWord/CTable0.h" //单个表格 #include "MsWord/CTables0.h" //表格集合 #include "MsWord/CRow.h" //单个行 #include "MsWord/CRows.h" //行集合 #include "MsWord/CBookmark0.h" // #include "MsWord/CBookmarks.h" // #include "MsWord/CWindow0.h" #include "MsWord/CView0.h" #include "MsWord/CPane.h" #include "MsWord/CHeaderFooter.h" #include "MsWord/CField.h" #include "MsWord/CFields.h" #include "MsWord/CnlineShape.h" #include "MsWord/CnlineShapes.h" #include "MsWord/CFont0.h" #include "MsWord/CBorder.h" #include "MsWord/CBorders.h" #include "MsWord/CColumns0.h" #include "MsWord/CColumn.h" #include "MsWord/CParagraphFormat.h" #include "enums.h" #include <comdef.h>
MS Office 自动化编程(二)---Word文档的读写_leogray的博客-CSDN博客
标签:NULL,word,oApp,varOPt,msword,写入,文档,include,MsWord From: https://www.cnblogs.com/hshy/p/17650849.html