515 lines
13 KiB
C++
515 lines
13 KiB
C++
// WordAutomation.cpp: implementation of the CWordApplication class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// Construction/Destruction
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
CWordApplication::CWordApplication()
|
|
{
|
|
}
|
|
|
|
CWordApplication::~CWordApplication()
|
|
{
|
|
// if(wordApplication.m_lpDispatch != NULL)
|
|
// Quit();
|
|
}
|
|
|
|
BOOL CWordApplication::InitWordApplication(BOOL visible)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
|
|
if (!wordApplication.CreateDispatch(_T("Word.Application")))
|
|
{
|
|
AfxMessageBox(_T("不能执行Microsoft Word 8.0!"));
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
if (visible == TRUE)
|
|
wordApplication.SetVisible(TRUE);
|
|
|
|
wordDocuments = wordApplication.GetDocuments();
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CWordApplication::SaveAndQuit(LPCTSTR fileName)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
|
|
|
|
activeDocument.SaveAs(COleVariant(fileName), vtOptional, vtOptional, vtOptional,
|
|
vtTrue, vtOptional, vtFalse, vtOptional, vtTrue, vtFalse, vtFalse);
|
|
// activeDocument.Close(vtFalse,vtOptional,vtOptional);
|
|
// wordApplication.Quit(vtFalse, vtTrue, vtFalse);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CWordApplication::CreateAndOpenDocument(LPCTSTR fileName)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
|
|
activeDocument = wordDocuments.Add(vtOptional,vtOptional);
|
|
|
|
activeDocument.SaveAs(COleVariant(fileName), vtOptional, vtOptional, vtOptional,
|
|
vtTrue, vtOptional, vtFalse, vtOptional, vtTrue, vtFalse, vtFalse);
|
|
|
|
return TRUE;
|
|
}
|
|
void CWordApplication::CreateDocument()
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
|
|
activeDocument = wordDocuments.Add(vtOptional,vtOptional);
|
|
}
|
|
BOOL CWordApplication::OpenDiskDocument(LPCTSTR fileName)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
|
|
activeDocument.AttachDispatch(wordDocuments.Open(COleVariant(fileName),
|
|
vtFalse, // conversion
|
|
vtFalse, // Not read only
|
|
vtTrue, // Add to recent documents
|
|
vtOptional, // passwordDoc
|
|
vtOptional, // passwordTemp
|
|
vtFalse, // Revert
|
|
vtOptional, // WritepasswordDoc
|
|
vtOptional, // WritepasswordTemp
|
|
vtOptional) // Format
|
|
);
|
|
return TRUE;
|
|
}
|
|
|
|
void CWordApplication::InsertEmptyLines(int numLines)
|
|
{
|
|
Selection wSelection = wordApplication.GetSelection();
|
|
|
|
for (int iCount = 1; iCount <= numLines; iCount++)
|
|
wSelection.TypeParagraph();
|
|
}
|
|
|
|
void CWordApplication::FillRow6(_Table pTable, int Row, CString Text1,
|
|
CString Text2, CString Text3, CString Text4,CString Text5,CString Text6)
|
|
{
|
|
Cell oCell;
|
|
Range oRange;
|
|
Cells pCells;
|
|
|
|
oCell = pTable.Cell(Row,1);
|
|
//pCells.Add(oCell);
|
|
pCells.Merge();
|
|
//oRange = oCell.GetRange();
|
|
//oRange.InsertAfter(Text1);
|
|
|
|
oCell = pTable.Cell(Row,2);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text2);
|
|
|
|
oCell = pTable.Cell(Row,3);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text3);
|
|
|
|
oCell = pTable.Cell(Row,4);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text4);
|
|
|
|
oCell = pTable.Cell(Row,5);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text5);
|
|
|
|
oCell = pTable.Cell(Row,6);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text6);
|
|
|
|
|
|
}
|
|
|
|
void CWordApplication::FillRow7(_Table pTable, int Row, CString Text1,
|
|
CString Text2, CString Text3, CString Text4,CString Text5,CString Text6,CString Text7)
|
|
{
|
|
Cell oCell,oCell1;
|
|
Range oRange;
|
|
Cells pCells;
|
|
|
|
oCell1 = pTable.Cell(Row,1);
|
|
// pCells.Item(1) = oCell;
|
|
//oRange = oCell.GetRange();
|
|
//_Font Font;
|
|
//Font = oRange.GetFont();
|
|
//Font.SetColorIndex(5);
|
|
//oRange.InsertAfter(Text1);
|
|
|
|
oCell = pTable.Cell(Row,2);
|
|
|
|
// pCells.Add(.Item(2) = oCell;
|
|
// oRange = oCell.GetRange();
|
|
// oRange.InsertAfter(Text2);
|
|
|
|
//pCells.Merge();
|
|
oCell1.Merge(oCell);
|
|
oCell = pTable.Cell(Row,1);
|
|
oRange = oCell.GetRange();
|
|
_Font Font;
|
|
Font = oRange.GetFont();
|
|
Font.SetColorIndex(5);
|
|
oRange.InsertAfter(Text1);
|
|
|
|
|
|
oCell = pTable.Cell(Row,2);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text3);
|
|
|
|
oCell = pTable.Cell(Row,3);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text4);
|
|
|
|
oCell = pTable.Cell(Row,4);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text5);
|
|
|
|
oCell = pTable.Cell(Row,5);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text6);
|
|
|
|
oCell = pTable.Cell(Row,6);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text7);
|
|
|
|
}
|
|
|
|
void CWordApplication::FillRow10(_Table pTable, int Row, CString Text1,
|
|
CString Text2, CString Text3, CString Text4,CString Text5,CString Text6,CString Text7,CString Text8,CString Text9,CString Text10)
|
|
{
|
|
Cell oCell;
|
|
Range oRange;
|
|
|
|
oCell = pTable.Cell(Row,1);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text1);
|
|
|
|
oCell = pTable.Cell(Row,2);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text2);
|
|
|
|
oCell = pTable.Cell(Row,3);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text3);
|
|
|
|
oCell = pTable.Cell(Row,4);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text4);
|
|
|
|
oCell = pTable.Cell(Row,5);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text5);
|
|
|
|
oCell = pTable.Cell(Row,6);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text6);
|
|
|
|
oCell = pTable.Cell(Row,7);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text7);
|
|
oCell = pTable.Cell(Row,8);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text8);
|
|
|
|
oCell = pTable.Cell(Row,9);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text9);
|
|
oCell = pTable.Cell(Row,10);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text10);
|
|
|
|
}
|
|
|
|
void CWordApplication::FillRow(_Table pTable, int Row, CString Text1,
|
|
CString Text2, CString Text3, CString Text4)
|
|
{
|
|
Cell oCell;
|
|
Range oRange;
|
|
|
|
oCell = pTable.Cell(Row,1);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text1);
|
|
|
|
oCell = pTable.Cell(Row,2);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text2);
|
|
|
|
oCell = pTable.Cell(Row,3);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text3);
|
|
|
|
oCell = pTable.Cell(Row,4);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(Text4);
|
|
|
|
}
|
|
void CWordApplication::AddText(CString text,CString sfont,float height, int align,float spacing,BOOL bold, BOOL italic,int color)
|
|
{
|
|
wordSelection = wordApplication.GetSelection();
|
|
|
|
_Font font = wordSelection.GetFont();
|
|
|
|
font.SetBold(bold);
|
|
font.SetItalic(italic);
|
|
font.SetSize(height);
|
|
font.SetName(sfont);
|
|
font.SetColorIndex(color);
|
|
wordParagraphs = wordSelection.GetParagraphs();
|
|
wordParagraphs.SetAlignment(align);
|
|
wordParagraphs.SetLineSpacing(spacing);
|
|
wordSelection.TypeText(text);
|
|
}
|
|
void CWordApplication::AddText2(CString text, float height, int align, BOOL bold, BOOL italic)
|
|
{
|
|
wordSelection = wordApplication.GetSelection();
|
|
|
|
_Font font = wordSelection.GetFont();
|
|
|
|
font.SetBold(bold);
|
|
font.SetItalic(italic);
|
|
font.SetSize(height);
|
|
font.SetColorIndex(11);
|
|
wordParagraphs = wordSelection.GetParagraphs();
|
|
wordParagraphs.SetAlignment(align);
|
|
wordSelection.TypeText(text);
|
|
}
|
|
Shape CWordApplication::AddLine(float x1, float y1, float x2, float y2)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
|
|
|
|
Shapes shapes = activeDocument.GetShapes();
|
|
|
|
Paragraphs paras = activeDocument.GetParagraphs();
|
|
Paragraph para = paras.GetLast();
|
|
Range rr = para.GetRange();
|
|
|
|
return shapes.AddLine(x1, y1, x2, y2, vtOptional);
|
|
}
|
|
|
|
Shape CWordApplication::AddOloObject(CString id, CString fName, BOOL linkToFile, BOOL displayAsIcon, CString icnFileName, int icnIndex, CString icnLabel, float left, float top, float width, float height)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE),
|
|
vtID(id),
|
|
vtfName(fName),
|
|
vticnFileName(icnFileName),
|
|
vticnIndex((short)icnIndex),
|
|
vticnLabel(icnLabel),
|
|
vtLeft((float)left),
|
|
vtTop((float) top),
|
|
vtWidth((float) width),
|
|
vtHeight((float) height);
|
|
|
|
Shapes shapes = activeDocument.GetShapes();
|
|
return shapes.AddOLEObject(
|
|
vtOptional, //vtID,
|
|
vtfName,
|
|
vtTrue,
|
|
vtFalse,
|
|
vtOptional, //vticnFileName,
|
|
vtOptional, //vticnIndex,
|
|
vtOptional, //vticnLabel,
|
|
vtLeft,
|
|
vtTop,
|
|
vtWidth,
|
|
vtHeight,
|
|
vtOptional
|
|
);
|
|
}
|
|
/////////////////
|
|
long CWordApplication::MoveDown_han(int Number)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE);
|
|
Selection wSelection = wordApplication.GetSelection();
|
|
|
|
for (int iCount = 1; iCount <= Number; iCount++)
|
|
return wSelection.MoveDown(vtFalse,vtFalse,vtFalse);
|
|
return 0;
|
|
}
|
|
|
|
//MoveUp(VARIANT* Unit, VARIANT* Count, VARIANT* Extend);
|
|
|
|
/* Window window = wordApp.activeDocument.GetActiveWindow();
|
|
window.GetActivePane();
|
|
|
|
|
|
|
|
window.SmallScroll(true,false,10.0,0.0);
|
|
*/
|
|
void CWordApplication::smallscroll(BOOL down , BOOL up, float toleft ,float toright)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtDown((short)down),
|
|
vtUp((short)up),
|
|
vttoleft((float)toleft),
|
|
vttoright((float)toright);
|
|
|
|
Window window = wordApplication.GetSelection();;
|
|
window.SmallScroll(vtDown,vtUp,vttoleft,vtOptional);
|
|
|
|
|
|
}
|
|
|
|
InlineShape CWordApplication::AddPicture_Han(CString fileName, BOOL linkToFile, BOOL saveWithDoc, BOOL Range)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE),
|
|
vtRange((short)Range);
|
|
|
|
InlineShapes inlineshapes = activeDocument.GetInlineShapes();
|
|
return inlineshapes.AddPicture(
|
|
fileName,
|
|
vtTrue,
|
|
vtFalse,
|
|
// vtRange,
|
|
vtOptional
|
|
);
|
|
|
|
}
|
|
|
|
Shape CWordApplication::AddPicture(CString fileName, BOOL linkToFile, BOOL saveWithDoc, float left, float top, float width, float height)
|
|
{
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
|
|
vtTrue((short)TRUE),
|
|
vtFalse((short)FALSE),
|
|
vtLeft((float)left),
|
|
vtTop((float) top),
|
|
vtWidth((float) width),
|
|
vtHeight((float) height);
|
|
|
|
Shapes shapes = activeDocument.GetShapes();
|
|
return shapes.AddPicture(
|
|
fileName,
|
|
vtTrue,
|
|
vtFalse,
|
|
vtLeft,
|
|
vtTop,
|
|
vtWidth,
|
|
vtHeight,
|
|
vtOptional
|
|
);
|
|
}
|
|
|
|
_Table CWordApplication::CreateTable(int rows, int columns, CWordArray &widths, int height)
|
|
{
|
|
if (widths.GetSize() < columns)
|
|
return NULL;
|
|
|
|
Selection wSelection = wordApplication.GetSelection();
|
|
|
|
Range oRange = wSelection.GetRange();
|
|
Tables oTables = activeDocument.GetTables();
|
|
_Table oTable = oTables.Add(oRange, rows, columns);
|
|
|
|
Columns oColumns;
|
|
Column oColumn;
|
|
|
|
// Set the width of each column
|
|
for (int col = 0; col < columns; col++)
|
|
{
|
|
oColumns = oTable.GetColumns();
|
|
oColumn = oColumns.Item(col+1);
|
|
|
|
float width = widths.GetAt(col);
|
|
oColumn.SetWidth(width, 0); // 0 = wdAdjustNone
|
|
}
|
|
|
|
return oTable;
|
|
}
|
|
void CWordApplication::GoToTheEnd()
|
|
{
|
|
// Go to the end of the document
|
|
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
|
|
|
|
wordSelection = wordApplication.GetSelection();
|
|
|
|
wordSelection.GoTo(COleVariant((short)3), // 3 = wdGoToLine
|
|
COleVariant((short)-1),vtOptional,vtOptional); // -1 = wdGoToLast
|
|
}
|
|
|
|
void CWordApplication::SetShading(_Table oTable, int row, int colorIndex, BOOL boldFlag)
|
|
{
|
|
Rows oRows = oTable.GetRows();
|
|
Row oRow = oRows.Item(row);
|
|
Cells oCells = oRow.GetCells();
|
|
Shading oShading = oCells.GetShading();
|
|
oShading.SetBackgroundPatternColorIndex(colorIndex); // 16 = wdGray25
|
|
|
|
Range oRange = oRow.GetRange();
|
|
oRange.SetBold(boldFlag);
|
|
}
|
|
|
|
void CWordApplication::SetItemText(_Table pTable, int nRow, int nCol, CString textStr)
|
|
{
|
|
Cell oCell;
|
|
Range oRange;
|
|
|
|
oCell = pTable.Cell(nRow, nCol);
|
|
oRange = oCell.GetRange();
|
|
oRange.InsertAfter(textStr);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// apptend by han
|
|
|
|
long CWordApplication::getEndKey_han()
|
|
{
|
|
/* COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
|
|
Selection wSelection = wordApplication.GetSelection();
|
|
|
|
// return wordSelection.GoTo(COleVariant((short)3), // 3 = wdGoToLine
|
|
// COleVariant((short)-1),vtOptional,vtOptional); // -1 = wdGoToLast
|
|
for (int i = 1 ; i<30; i++)
|
|
{
|
|
// wSelection.MoveUp(vtOptional,vtOptional,vtOptional);passed do not delete
|
|
}
|
|
*/
|
|
return 10;
|
|
|
|
|
|
|
|
}
|
|
//bool CExcelApplication::Create(BOOL bVisible)
|
|
//{
|
|
// application.CreateDispatch(_T("Excel.Application"));
|
|
// if(!application)
|
|
// return false;
|
|
// Workbooks oBooks = application.GetWorkbooks();
|
|
// COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
|
|
// book = oBooks.Add(vOpt);
|
|
// //Worksheets oSheets = book.GetWorksheets();
|
|
// //sheet = oSheets.GetItem(COleVariant((short)1));
|
|
// if(bVisible)
|
|
// application.SetVisible(TRUE);
|
|
// return true;
|
|
//}
|