程序代码:
#ifndef CMYWORD_H
#define CMYWORD_H
#include "msword.h"
#include <ATLBASE.H>
enum
Alignment{wdAlignParagraphCenter=1,wdAlignParagraphRight,wdAlignParagraphJustify};
enum
SaveType{
wdFormatDocument=0,
wdFormatWebArchive=9,
wdFormatHTML=8,
wdFormatFilteredHTML=10,
wdFormatTemplate=1
};
class
CmyWord
{
public
:
_Application app;
Documents docs;
_Document doc;
_Font font;
Selection sel;
Table tab;
Range range;
public
:
CmyWord();
virtual
~CmyWord();
void
ShowApp(
BOOL
flag);
void
AppClose();
BOOL
InitCOM();
BOOL
CreateAPP();
BOOL
CreateDocument();
BOOL
Create();
BOOL
Open(CString FileName,
BOOL
ReadOnly = FALSE,
BOOL
AddToRecentFiles = FALSE);
BOOL
Close(
BOOL
SaveChange=FALSE);
BOOL
Save();
BOOL
SaveAs(CString FileName,
int
SaveType=0);
void
WriteText(CString Text);
void
NewLine(
int
nCount=1);
void
WriteTextNewLineText(CString Text,
int
nCount=1);
void
SetFont(CString FontName,
int
FontSize=9,
long
FontColor=0,
long
FontBackColor=0);
void
SetFont(
BOOL
Blod,
BOOL
Italic=FALSE,
BOOL
UnderLine=FALSE);
void
SetTableFont(
int
Row,
int
Column,CString FontName,
int
FontSize=9,
long
FontColor=0,
long
FontBackColor=0);
void
CreateTable(
int
Row,
int
Column);
void
WriteCellText(
int
Row,
int
Column,CString Text);
void
SetParaphformat(
int
Alignment);
void
FindWord(CString FindW,CString RelWord);
void
GetWordText(CString &Text);
void
PrintWord();
};
#endif
程序代码:
#include "StdAfx.h"
#include "CmyWord.h"
CmyWord::CmyWord()
{
InitCOM();
}
CmyWord::~CmyWord()
{
CoUninitialize();
font.ReleaseDispatch();
range.ReleaseDispatch();
tab.ReleaseDispatch();
doc.ReleaseDispatch();
docs.ReleaseDispatch();
app.ReleaseDispatch();
sel.ReleaseDispatch();
}
BOOL
CmyWord::InitCOM()
{
if
(CoInitialize(NULL)!=S_OK)
{
AfxMessageBox(
"初始化com库失败"
);
return
0;
}
else
{
return
TRUE;
}
}
BOOL
CmyWord::CreateAPP()
{
if
(!app.CreateDispatch(
"Word.Application"
))
{
AfxMessageBox(
"你没有安装OFFICE"
);
return
FALSE;
}
else
{
app.SetVisible(TRUE);
return
TRUE;
}
}
void
CmyWord::ShowApp(
BOOL
flag)
{
if
(!app.m_lpDispatch)
{
AfxMessageBox(
"你还没有获得Word对象"
);
return
;
}
else
{
app.SetVisible(flag);
}
}
BOOL
CmyWord::CreateDocument()
{
if
(!app.m_lpDispatch)
{
AfxMessageBox(
"Application为空,Documents创建失败!"
, MB_OK|MB_ICONWARNING);
return
FALSE;
}
else
{
docs=app.GetDocuments();
if
(docs.m_lpDispatch==NULL)
{
AfxMessageBox(
"创建DOCUMENTS 失败"
);
return
FALSE;
}
else
{
CComVariant Template(_T(
""
));
CComVariant NewTemplate(
false
);
CComVariant DocumentType(0);
CComVariant Visible;
doc = docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"创建word失败"
);
return
FALSE;
}
else
{
sel = app.GetSelection();
if
(!sel.m_lpDispatch)
{
AfxMessageBox(
"selection 获取失败"
);
return
FALSE;
}
else
{
return
TRUE;
}
}
}
}
}
BOOL
CmyWord ::Create()
{
if
(CreateAPP())
{
if
(CreateDocument())
{
return
TRUE;
}
else
return
FALSE;
}
else
return
FALSE;
}
BOOL
CmyWord::Open(CString FileName,
BOOL
ReadOnly
,
BOOL
AddToRecentFiles
)
{
CComVariant Read(ReadOnly);
CComVariant AddToR(AddToRecentFiles);
CComVariant Name(FileName);
COleVariant vTrue((
short
)TRUE), vFalse((
short
)FALSE);
COleVariant varstrNull(
""
);
COleVariant varZero((
short
)0);
COleVariant varTrue(
short
(1),VT_BOOL);
COleVariant varFalse(
short
(0),VT_BOOL);
COleVariant vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
if
(!app.m_lpDispatch)
{
if
(CreateAPP()==FALSE)
{
return
FALSE;
}
}
if
(!docs.m_lpDispatch)
{
docs=app.GetDocuments();
if
(!docs.m_lpDispatch)
{
AfxMessageBox(
"DocuMent 对象创建失败"
);
return
FALSE;
}
}
CComVariant format(0);
doc=docs.Open(&Name,varFalse,&Read,&AddToR,vOpt,vOpt,
vFalse,vOpt,vOpt,&format,vOpt,vTrue,vOpt,vOpt,vOpt,vOpt);
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"文件打开失败"
);
return
FALSE;
}
else
{
sel=app.GetSelection();
if
(!sel.m_lpDispatch)
{
AfxMessageBox(
"打开失败"
);
return
FALSE;
}
return
TRUE;
}
}
BOOL
CmyWord::Save()
{
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"Documents 对象都没有建立 保存失败"
);
return
FALSE;
}
else
{
doc.Save();
return
TRUE;
}
}
BOOL
CmyWord::SaveAs(CString FileName,
int
SaveType
)
{
CComVariant vTrue(TRUE);
CComVariant vFalse(FALSE);
CComVariant vOpt;
CComVariant cFileName(FileName);
CComVariant FileFormat(SaveType);
doc=app.GetActiveDocument();
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"Document 对象没有建立 另存为失败"
);
return
FALSE;
}
else
{
doc.SaveAs(&cFileName,&FileFormat,&vFalse,COleVariant(
""
),&vTrue,
COleVariant(
""
),&vFalse,&vFalse,&vFalse,&vFalse,&vFalse,&vOpt,&vOpt,&vOpt,&vOpt,&vOpt);
}
return
TRUE;
}
BOOL
CmyWord::Close(
BOOL
SaveChange
)
{
CComVariant vTrue(TRUE);
CComVariant vFalse(FALSE);
CComVariant vOpt;
CComVariant cSavechage(SaveChange);
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"_Document 对象获取失败,关闭操作失败"
);
return
FALSE;
}
else
{
if
(TRUE==SaveChange)
{
Save();
}
doc.Close(&vFalse,&vOpt,&vOpt);
}
return
TRUE;
}
void
CmyWord::WriteText(CString Text)
{
sel.TypeText(Text);
}
void
CmyWord::NewLine(
int
nCount
)
{
if
(nCount<=0)
{
nCount = 0;
}
else
{
for
(
int
i=0;i<nCount;i++)
{
sel.TypeParagraph();
}
}
}
void
CmyWord::WriteTextNewLineText(CString Text,
int
nCount
)
{
NewLine(nCount);
WriteText(Text);
}
void
CmyWord::SetFont(
BOOL
Blod,
BOOL
Italic
,
BOOL
UnderLine
)
{
if
(!sel.m_lpDispatch)
{
AfxMessageBox(
"编辑对象失败,导致字体不能设置"
);
return
;
}
else
{
sel.SetText(
"F"
);
font=sel.GetFont();
font.SetBold(Blod);
font.SetItalic(Italic);
font.SetUnderline(UnderLine);
sel.SetFont(font);
}
}
void
CmyWord::SetFont(CString FontName,
int
FontSize
,
long
FontColor
,
long
FontBackColor
)
{
if
(!sel.m_lpDispatch)
{
AfxMessageBox(
"Select 为空,字体设置失败!"
);
return
;
}
sel.SetText(
"a"
);
font=sel.GetFont();
font.SetSize(20);
font.SetName(FontName);
font.SetColor(FontColor);
sel.SetFont(font);
}
void
CmyWord::SetTableFont(
int
Row,
int
Column,CString FontName,
int
FontSize
,
long
FontColor
,
long
FontBackColor
)
{
Cell c=tab.Cell(Row,Column);
c.Select();
_Font ft=sel.GetFont();
ft.SetName(FontName);
ft.SetSize(FontSize);
ft.SetColor(FontColor);
Range r=sel.GetRange();
r.SetHighlightColorIndex(FontBackColor);
}
void
CmyWord::CreateTable(
int
Row,
int
Column)
{
doc=app.GetActiveDocument();
Tables tbs=doc.GetTables();
CComVariant Vopt;
if
(!tbs.m_lpDispatch)
{
AfxMessageBox(
"创建表格对象失败"
);
return
;
}
else
{
tbs.Add(sel.GetRange(),Row,Column,&Vopt,&Vopt);
tab=tbs.Item(1);
}
}
void
CmyWord::WriteCellText(
int
Row,
int
Column,CString Text)
{
Cell c=tab.Cell(Row,Column);
c.Select();
sel.TypeText(Text);
}
void
CmyWord::SetParaphformat(
int
Alignment)
{
_ParagraphFormat p=sel.GetParagraphFormat();
p.SetAlignment(Alignment);
sel.SetParagraphFormat(p);
}
void
CmyWord::FindWord(CString FindW,CString RelWord)
{
sel=app.GetSelection();
Find myFind=sel.GetFind();
if
(!myFind.m_lpDispatch)
{
AfxMessageBox(
"获取Find 对象失败"
);
return
;
}
else
{
myFind.ClearFormatting();
Replacement repla=myFind.GetReplacement();
repla.ClearFormatting();
COleVariant Text(FindW);
COleVariant re(RelWord);
COleVariant vTrue((
short
)TRUE), vFalse((
short
)FALSE);
COleVariant vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
CComVariant v(1);
CComVariant v2(2);
CComVariant v3(_T(
""
));
myFind.Execute(Text,vFalse,vFalse,vFalse,vFalse,vFalse,
vTrue,&v,vFalse,&re,&v2,vOpt,vOpt,vOpt,vOpt);
}
}
void
CmyWord::GetWordText(CString &Text)
{
COleVariant vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
doc=app.GetActiveDocument();
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"获取激活文档对象失败"
);
return
;
}
else
{
range=doc.Range(vOpt,vOpt);
Text=range.GetText();
AfxMessageBox(Text);
}
}
void
CmyWord::PrintWord()
{
doc = app.GetActiveDocument();
if
(!doc.m_lpDispatch)
{
AfxMessageBox(
"获取激活文档对象失败"
);
return
;
}
else
{
COleVariant covTrue((
short
)TRUE),
covFalse((
short
)FALSE),
covOptional((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
doc.PrintOut(covFalse,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
COleVariant((
long
)1),
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional,
covOptional);
}
}
void
CmyWord::AppClose()
{
COleVariant vOpt((
long
)DISP_E_PARAMNOTFOUND, VT_ERROR);
if
(!app.m_lpDispatch)
{
AfxMessageBox(
"获取Word 对象失败,关闭操作失败"
);
return
;
}
else
{
app.Quit(vOpt,vOpt,vOpt);
}
}
我的工程 是vc6 控制台 支持MFC
http:
word操作封装类.zip