|
- #pragma once
- #include "msword/CApplication.h" //word程序对象
- #include "msword/CDocuments0.h" //文档集对象
- #include "msword/CDocument1.h" //docx对象
- #include "msword/CSelection0.h" //所选内容
- #include "msword/CCell0.h" //单个单元格
- #include "msword/CCells0.h" //单元格集合
- #include "msword/CRange0.h" //文档中的一个连续范围
- #include "msword/CTable1.h" //单个表格
- #include "msword/CTables1.h" //表格集合
- #include "msword/CRow0.h" //单个行
- #include "msword/CRows0.h" //行集合
- #include "msword/CBookmark0.h" //
- #include "msword/CBookmarks0.h" //
- #include "msword/CFont0.h"
- #include "msword/CnlineShape.h"
- #include "msword/CnlineShapes.h"
-
- const auto wdMove = 0;
- const auto wdExtend = 1;
- const auto wdCharacter = 1;
- const auto wdLine = 5;
- const auto wdCell = 12;
- const auto wdDeleteCellsEntireRow = 2;
-
- class WordOperate
- {
- public:
- WordOperate(void);
- ~WordOperate(void);
- //创建一个新的WORD应用程序
- BOOL CreateApp();
-
- //创建一个新的Word文档集合
- BOOL CreateDocuments();
-
- //创建一个新的Word文档
- BOOL CreateDocument();
-
- //创建新的WORD应用程序并创建一个新的文档
- BOOL Create();
-
- //显示WORD文档
- void ShowApp();
-
- //隐藏word文档
- void HideApp();
-
- //打开已经存在的文档。
- BOOL OpenDocument(const CString& fileName);
-
- //创建新的WORD应用程序并打开一个已经存在的文档。
- BOOL Open(CString fileName);
-
- //设置当前激活的文档。
- BOOL SetActiveDocument(short i);
-
- //文档是以打开形式,保存。
- BOOL SaveDocument();
-
- //文档以创建形式,保存。
- BOOL SaveDocumentAs(const CString& fileName);
-
- BOOL CloseDocument();
- void CloseApp();
-
- void WriteText(CString szText); //当前光标处写文本
- void WriteNewLineText(CString szText, int nLineCount = 1); //换N行写字
- void WriteEndLine(CString szText); //文档结尾处写文本
- void WholeStory(); //全选文档内容
- void Copy(); //复制文本内容到剪贴板
- void InsertFile(CString fileName); //将本地的文件全部内容写入到当前文档的光标处。
-
- void SelectMoveDown(short lineCount, short unit);//有选择操作的移动
- void NoneSelectMoveDown(short lineCount, short unit);//仅仅移动光标,不选中
- void SelectMoveUp(short lineCount, short unit);//有选择操作的移动
- void NoneSelectMoveUp(short lineCount, short unit);//仅仅移动光标,不选中
-
- void SelectMoveLeft(short charCount, short unit);//有选择操作的移动
- void NoneSelectMoveLeft(short charCount, short unit);//
- void SelectMoveRight(short charCount, short unit);//有选择操作的移动
- void NoneSelectMoveRight(short charCount, short unit);//
-
- void MoveToFirst();
- void MoveToNextPage();
- void TypeParagraph();
- void PasteAndFormat();
- void Paste();
- void TypeBackspace(int count);
- //填写书签
- void SetCBookmark(const CString& bookmarkname, const CString& value, const CString& fontname = "");
- //设置单元格内容
- void SetWordCellValue(int row, int column, int tableIndex, const CString& value, float size, const CString& fontname = "");
- //合并单元格
- BOOL MergeCell(int cell1row, int cell1col, int cell2row, int cell2col, int tableIndex);
- //查找字符串 然后全部替换
- void FindWord(CString FindW, CString RelWord);
- //表格插入行
- void TableInsertRows(int row, int column, int tableIndex);
- //表格删除行
- void TableRemoveRows(int row, int column, int tableIndex);
- //插入照片
- void InsertJpg(CString strPicture);
- private:
- CApplication m_wdApp;
- CDocuments0 m_wdDocs;
- CDocument1 m_wdDoc;
- CSelection0 m_wdSel;
- CRange0 m_wdRange;
- CBookmarks0 bookmarks;
- CTables1 tables;
- };
-
复制代码- #include "stdafx.h"
- #include "WordOperate.h"
-
- WordOperate::WordOperate(void)
- {
- }
-
-
- WordOperate::~WordOperate(void)
- {
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- //m_wdDoc.Save();
- m_wdApp.Quit(vFalse, // SaveChanges.
- vTrue, // OriginalFormat.
- vFalse // RouteDocument.
- );
- m_wdRange.ReleaseDispatch();
- m_wdSel.ReleaseDispatch();
- //m_wdFt.ReleaseDispatch();
- m_wdDoc.ReleaseDispatch();
- m_wdDocs.ReleaseDispatch();
- m_wdApp.ReleaseDispatch();
- }
-
- BOOL WordOperate::CreateApp()
- {
- COleException pe;
- if (!m_wdApp.CreateDispatch(_T("Word.Application"), &pe))
- {
- AfxMessageBox(_T("Application创建失败,请确保安装了word 2000或以上版本!"), MB_OK | MB_ICONWARNING);
- pe.ReportError();
- throw& pe;
- return FALSE;
- }
- m_wdApp.put_DisplayAlerts(FALSE);//屏蔽警告
- return TRUE;
- }
-
- BOOL WordOperate::CreateDocuments()
- {
- if (FALSE == CreateApp())
- {
- return FALSE;
- }
- m_wdDocs.AttachDispatch(m_wdApp.get_Documents());
- //m_wdDocs=m_wdApp.get_Documents();
- if (!m_wdDocs.m_lpDispatch)
- {
- AfxMessageBox(_T("Documents创建失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- return TRUE;
- }
-
- BOOL WordOperate::CreateDocument()
- {
- if (!m_wdDocs.m_lpDispatch)
- {
- AfxMessageBox(_T("Documents为空!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
-
- COleVariant varTrue(short(1), VT_BOOL), vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- CComVariant Template(_T("")); //没有使用WORD的文档模板
- CComVariant NewTemplate(false), DocumentType(0), Visible;
-
- m_wdDocs.Add(&Template, &NewTemplate, &DocumentType, &Visible);
-
- //得到document变量
- m_wdDoc = m_wdApp.get_ActiveDocument();
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到selection变量
- m_wdSel = m_wdApp.get_Selection();
- if (!m_wdSel.m_lpDispatch)
- {
- AfxMessageBox(_T("Select获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到Range变量
- m_wdRange = m_wdDoc.Range(vOptional, vOptional);
- if (!m_wdRange.m_lpDispatch)
- {
- AfxMessageBox(_T("Range获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
-
- return TRUE;
- }
-
- BOOL WordOperate::Create()
- {
- /*if (FALSE == CreateDocuments())
- {
- return FALSE;
- }*/
- return CreateDocument();
- }
-
- void WordOperate::ShowApp()
- {
- m_wdApp.put_Visible(TRUE);
- }
-
- void WordOperate::HideApp()
- {
- m_wdApp.put_Visible(FALSE);
- }
-
- BOOL WordOperate::OpenDocument(const CString& fileName)
- {
- if (!m_wdDocs.m_lpDispatch)
- {
- AfxMessageBox(_T("Documents为空!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
-
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
- vZ((short)0);
- COleVariant vFileName(fileName);
-
- //得到document变量
- try
- {
- //m_wdDoc = m_wdDocs.Add(vFileName, vOptional, vOptional, vOptional);
- m_wdDoc.AttachDispatch(m_wdDocs.Open(
- vFileName, // FileName
- vTrue, // Confirm Conversion.
- vFalse, // ReadOnly.
- vFalse, // AddToRecentFiles.
- vOptional, // PasswordDocument.
- vOptional, // PasswordTemplate.
- vOptional, // Revert.
- vOptional, // WritePasswordDocument.
- vOptional, // WritePasswordTemplate.
- vOptional, // Format. // Last argument for Word 97
- vOptional, // Encoding // New for Word 2000/2002
- vOptional, // Visible
- //如下4个是word2003需要的参数。本版本是word2000。
- vOptional, // OpenAndRepair
- vZ, // DocumentDirection wdDocumentDirection LeftToRight
- vOptional, // NoEncodingDialog
- vOptional
-
- ) // Close Open parameters
- ); // Close AttachDispatch
- }
- catch (const std::exception&p)
- {
- CString str= p.what();
- AfxMessageBox(str, MB_OK | MB_ICONWARNING);
- return FALSE;
- }
-
-
-
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到selection变量
- m_wdSel = m_wdApp.get_Selection();
- if (!m_wdSel.m_lpDispatch)
- {
- AfxMessageBox(_T("Select获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到全部DOC的Range变量
- m_wdRange = m_wdDoc.Range(vOptional, vOptional);
- if (!m_wdRange.m_lpDispatch)
- {
- AfxMessageBox(_T("Range获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到全部书签
- bookmarks = m_wdDoc.get_Bookmarks();
- if (!bookmarks.m_lpDispatch)
- {
- AfxMessageBox(_T("bookmarks获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到全部表格
- tables = m_wdDoc.get_Tables();
- if (!tables.m_lpDispatch)
- {
- AfxMessageBox(_T("tables获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- return TRUE;
- }
-
- BOOL WordOperate::Open(CString fileName)
- {
- if (FALSE == CreateDocuments())
- {
- return FALSE;
- }
- //HideApp();
- return OpenDocument(fileName);
- }
-
- BOOL WordOperate::SetActiveDocument(short i)
- {
- COleVariant vIndex(i), vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
-
- m_wdDoc.AttachDispatch(m_wdDocs.Item(vIndex));
- m_wdDoc.Activate();
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到selection变量
- m_wdSel = m_wdApp.get_Selection();
- if (!m_wdSel.m_lpDispatch)
- {
- AfxMessageBox(_T("Select获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到全部DOC的Range变量
- m_wdRange = m_wdDoc.Range(vOptional, vOptional);
- if (!m_wdRange.m_lpDispatch)
- {
- AfxMessageBox(_T("Range获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- //得到全部书签
- bookmarks = m_wdDoc.get_Bookmarks();
- if (!bookmarks.m_lpDispatch)
- {
- AfxMessageBox(_T("bookmarks获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- HideApp();
- return TRUE;
- }
-
- BOOL WordOperate::SaveDocument()
- {
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- m_wdDoc.Save();
- return TRUE;
- }
-
- BOOL WordOperate::SaveDocumentAs(const CString& fileName)
- {
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- COleVariant vFileName(fileName);
-
- m_wdDoc.SaveAs(
- vFileName, //VARIANT* FileName
- vOptional, //VARIANT* FileFormat
- vOptional, //VARIANT* LockComments
- vOptional, //VARIANT* Password
- vOptional, //VARIANT* AddToRecentFiles
- vOptional, //VARIANT* WritePassword
- vOptional, //VARIANT* ReadOnlyRecommended
- vOptional, //VARIANT* EmbedTrueTypeFonts
- vOptional, //VARIANT* SaveNativePictureFormat
- vOptional, //VARIANT* SaveFormsData
- vOptional, //VARIANT* SaveAsAOCELetter
- vOptional, //VARIANT* ReadOnlyRecommended
- vOptional, //VARIANT* EmbedTrueTypeFonts
- vOptional, //VARIANT* SaveNativePictureFormat
- vOptional, //VARIANT* SaveFormsData
- vOptional //VARIANT* SaveAsAOCELetter
- );
- return TRUE;
- }
-
-
- BOOL WordOperate::CloseDocument()
- {
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
-
- m_wdDoc.Close(vFalse, // SaveChanges.
- vTrue, // OriginalFormat.
- vFalse // RouteDocument.
- );
- /*
- //AfxMessageBox("c1");
- m_wdDoc.AttachDispatch(m_wdApp.get_ActiveDocument());
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox(_T("Document获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- // AfxMessageBox("c2");
- //得到selection变量
- m_wdSel = m_wdApp.get_Selection();
- if (!m_wdSel.m_lpDispatch)
- {
- AfxMessageBox(_T("Select获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- // AfxMessageBox("c3");
- //得到全部DOC的Range变量
- m_wdRange = m_wdDoc.Range(vOptional, vOptional);
- if (!m_wdRange.m_lpDispatch)
- {
- AfxMessageBox(_T("Range获取失败!"), MB_OK | MB_ICONWARNING);
- return FALSE;
- }
- // AfxMessageBox("c4");
- */
- m_wdRange.ReleaseDispatch();
- m_wdRange = nullptr;
- m_wdSel.ReleaseDispatch();
- m_wdSel = nullptr;
- bookmarks.ReleaseDispatch();
- bookmarks = nullptr;
- tables.ReleaseDispatch();
- tables = nullptr;
- m_wdDoc.ReleaseDispatch();
- m_wdDoc = nullptr;
- return TRUE;
- }
-
- void WordOperate::CloseApp()
- {
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- //m_wdDoc.Save();
- m_wdApp.Quit(vFalse, // SaveChanges.
- vTrue, // OriginalFormat.
- vFalse // RouteDocument.
- );
- //释放内存申请资源
- bookmarks.ReleaseDispatch();
- m_wdRange.ReleaseDispatch();
-
- m_wdSel.ReleaseDispatch();
- tables.ReleaseDispatch();
- m_wdDoc.ReleaseDispatch();
- m_wdDocs.ReleaseDispatch();
- m_wdApp.ReleaseDispatch();
- }
-
- void WordOperate::WriteText(CString szText)
- {
- m_wdSel.TypeText(szText);
- }
-
- void WordOperate::WriteNewLineText(CString szText, int nLineCount /**//* = 1 */)
- {
- int i;
- if (nLineCount <= 0)
- {
- nLineCount = 0;
- }
- for (i = 0; i < nLineCount; i++)
- {
- m_wdSel.TypeParagraph();
- }
- WriteText(szText);
- }
-
- void WordOperate::WriteEndLine(CString szText)
- {
- m_wdRange.InsertAfter(szText);
- }
-
- void WordOperate::WholeStory()
- {
- m_wdRange.WholeStory();
- }
-
- void WordOperate::Copy()
- {
- m_wdSel.Copy();
- //m_wdSel.CopyFormat();
- }
-
- void WordOperate::TypeParagraph()
- {
- m_wdSel.TypeParagraph();
- }
-
- void WordOperate::PasteAndFormat()
- {
- m_wdSel.PasteAndFormat(0);
- }
-
- void WordOperate::Paste()
- {
- m_wdSel.Paste();
- //m_wdSel.PasteFormat();
- }
-
- void WordOperate::TypeBackspace(int count)
- {
- for (int i = 0; i < count; i++)
- m_wdSel.TypeBackspace();
- }
- void WordOperate::SetCBookmark(const CString& bookmarkname, const CString& value, const CString& fontname)
- {
- if (bookmarks.Exists(bookmarkname))
- {
- CBookmark0 bookmark = bookmarks.Item(&_variant_t(bookmarkname));
- if (bookmark != nullptr)
- {
- CRange0 range = bookmark.get_Range();
- range.put_Text(value);
- if (fontname != "")
- {
- CFont0 font = range.get_Font();
- font.put_Name(fontname);//设置字体
- font.ReleaseDispatch();
- }
- range.ReleaseDispatch();
- range = nullptr;
- bookmark.ReleaseDispatch();
- bookmark = nullptr;
- }
- }
- }
- void WordOperate::SetWordCellValue(int row, int column, int tableIndex, const CString& value, float size, const CString& fontname)
- {
- CTable1 table = tables.Item(tableIndex);
- CCell0 c1 = table.Cell(row, column);
- c1.Select();
- //m_wdSel = m_wdApp.get_Selection();//Selection表示输入点,即光标闪烁的那个地方
- m_wdSel.TypeText(value);
- CFont0 font = m_wdSel.get_Font();
- if (fontname != "")
- {
- font.put_Name(fontname);//设置字体
- }
- font.put_Size(size);
-
- //aa.Add(covOptional);
- //COleVariant vNull(_T(""));
- //aa.Add(vNull);
- //CRow0 row0= c1.get_Row();
- //CRange0 range=c1.get_Range();
- //range.put_Text(value);
- //CFont0 font = range.get_Font();
- //font.put_Name(_T("宋体"));//设置字体
- //font.put_Size(14);
-
- //range.ReleaseDispatch();
- font.ReleaseDispatch();
- c1.ReleaseDispatch();
- table.ReleaseDispatch();
- }
- BOOL WordOperate::MergeCell(int cell1row, int cell1col, int cell2row, int cell2col, int tableIndex)
- {
- CTable1 table = tables.Item(tableIndex);
- CCell0 cell1 = table.Cell(cell1row, cell1col);
- CCell0 cell2 = table.Cell(cell2row, cell2col);
- cell1.Merge(cell2);
- cell1.ReleaseDispatch();
- cell2.ReleaseDispatch();
- table.ReleaseDispatch();
- return TRUE;
- }
- void WordOperate::FindWord(CString FindW, CString RelWord)
- {
- }
- void WordOperate::TableInsertRows(int row, int column, int tableIndex)
- {
- CTable1 table = tables.Item(tableIndex);
- CCell0 c1 = table.Cell(row, column);
- c1.Select();
- COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- m_wdSel.InsertRows(covOptional);
-
- c1.ReleaseDispatch();
- table.ReleaseDispatch();
- }
- void WordOperate::TableRemoveRows(int row, int column, int tableIndex)
- {
- CTable1 table = tables.Item(tableIndex);
- CCell0 c1 = table.Cell(row, column);
- c1.Select();
- c1.Delete(COleVariant((long)wdDeleteCellsEntireRow));
- c1.ReleaseDispatch();
- table.ReleaseDispatch();
- }
- void WordOperate::InsertJpg(CString strPicture)
- {
- COleVariant covTrue((short)TRUE),
- covFalse((short)FALSE),
- covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
-
- CnlineShapes nLineShapes1 = m_wdSel.get_InlineShapes();
-
- CnlineShape nLineshape1 = nLineShapes1.AddPicture(strPicture, covFalse, covTrue, covOptional);// LinkToFile False 使图片文件的独立副本
- //auto old= nLineshape1.get_LockAspectRatio();
- nLineshape1.put_LockAspectRatio(1);//锁定纵横比
- //一页一张
- //auto width = nLineshape1.get_Width();
- //auto height = nLineshape1.get_Height();
- auto a = nLineshape1.get_ScaleWidth();
- auto b=nLineshape1.get_ScaleHeight();
- //nLineshape1.put_Width(206.5*2);
- //auto c = 206.5*2 / (width / height);
- //nLineshape1.put_Height(c);
-
- nLineshape1.ReleaseDispatch();
- nLineShapes1.ReleaseDispatch();
- }
- void WordOperate::InsertFile(CString fileName)
- {
- COleVariant vFileName(fileName),
- vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR),
- vNull(_T(""));
- /**//*
- void InsertFile(LPCTSTR FileName, VARIANT* Range, VARIANT* ConfirmConversions, VARIANT* Link, VARIANT* Attachment);
- */
- m_wdSel.InsertFile(
- fileName,
- vNull,
- vFalse,
- vFalse,
- vFalse
- );
- }
-
- void WordOperate::SelectMoveDown(short lineCount, short unit)//有选择操作的移动
- {
- m_wdSel.MoveDown(COleVariant(unit), COleVariant((short)lineCount), COleVariant((short)wdExtend));
- }
-
- void WordOperate::NoneSelectMoveDown(short lineCount, short unit)//仅仅移动光标,不选中
- {
- m_wdSel.MoveDown(COleVariant(unit), COleVariant((short)lineCount), COleVariant((short)wdMove));
- }
-
- void WordOperate::SelectMoveUp(short lineCount, short unit)//有选择操作的移动
- {
- m_wdSel.MoveUp(COleVariant(unit), COleVariant((short)lineCount), COleVariant((short)wdExtend));
- }
-
- void WordOperate::NoneSelectMoveUp(short lineCount, short unit)//仅仅移动光标,不选中
- {
- m_wdSel.MoveUp(COleVariant(unit), COleVariant((short)lineCount), COleVariant((short)wdMove));
- }
-
- void WordOperate::SelectMoveLeft(short charCount, short unit)//有选择操作的移动
- {
- m_wdSel.MoveLeft(COleVariant(unit), COleVariant((short)charCount), COleVariant((short)wdExtend));
- }
-
- void WordOperate::NoneSelectMoveLeft(short charCount, short unit)//
- {
- m_wdSel.MoveLeft(COleVariant(unit), COleVariant((short)charCount), COleVariant((short)wdMove));
- }
- void WordOperate::SelectMoveRight(short charCount, short unit)//有选择操作的移动
- {
- m_wdSel.MoveRight(COleVariant(unit), COleVariant((short)charCount), COleVariant((short)wdExtend));
- }
- void WordOperate::NoneSelectMoveRight(short charCount, short unit)//
- {
- m_wdSel.MoveRight(COleVariant(unit), COleVariant((short)charCount), COleVariant((short)wdMove));
- }
- void WordOperate::MoveToFirst()
- {
- m_wdSel.GoTo(COleVariant((short)1), COleVariant((short)2), COleVariant((short)0), COleVariant((short)1));
- }
-
- void WordOperate::MoveToNextPage()
- {
- m_wdSel.GoTo(COleVariant((short)1), COleVariant((short)2), COleVariant((short)1), COleVariant());
- }
复制代码 |
|