//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Word_2K_SRVR"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//简单处理,直接执行替换操作
/**
* 在 InterWordDoc中,使用 newText 替换原来的 docText
*/
bool TForm1::WordReplace(String docText, String newText, TWordApplication *WordApp, TWordDocument *InterWordDoc)
{
bool bResult = false;
try
{
InterWordDoc->Select();
TVariant charCount = WordApp->ActiveDocument->Characters->Count;
InterWordDoc->Range(TVariant(0), charCount);
WordApp->Selection->Find->ClearFormatting();
WordApp->Selection->Find->Replacement->ClearFormatting();
WordApp->Selection->Find->set_Text(TVariant(docText));
WordApp->Selection->Find->Replacement->set_Text(TVariant(newText));
WordApp->Selection->Find->Forward = true; //从开头向后匹配
WordApp->Selection->Find->Wrap = wdFindContinue;
WordApp->Selection->Find->Format = false; //不论格式
WordApp->Selection->Find->MatchCase = false; //不论大小写
WordApp->Selection->Find->MatchWholeWord = false; //不是整词匹配
WordApp->Selection->Find->MatchByte = false;
WordApp->Selection->Find->MatchWildcards = false; //不是通配
WordApp->Selection->Find->MatchSoundsLike = false;
WordApp->Selection->Find->MatchAllWordForms = false;
WordApp->Selection->Find->Execute();
WordApp->Options->ReplaceSelection = true ;
WordApp->Selection->TypeText(TVariant(newText));
WordApp->Selection->Collapse();
bResult = true;
}
catch (Exception &e)
{
}
return bResult;
}
void __fastcall TForm1::OpenDocClick(TObject *Sender)
{
try{
WordApp->Connect();
// 得到EXE文件所在的路径 ,例如 c:\MyDir
String PathStr = ExtractFilePath(ParamStr(0));
//查看 目标文件夹 c:\MyDir\Document是否存在,若不存在,则创建之
if(!DirectoryExists(PathStr + "Document"))
{
if(!CreateDir(PathStr + "Document"))
throw Exception("不能创建目录:" + PathStr + "Document");
}
//模板文件
TVariant TemplateFile = PathStr + "DocTemplates\\encash_week_B001.dot";
if(!FileExists(TemplateFile))
{
throw Exception("模板文件不存在!");
}
//目标文件
TVariant DestFile = PathStr + "Document\\encash_week_B001_2004003.doc";
WordApp->set_Visible(1);//Word文档的打开、替换 置为 可见,1为short值
DestWordDoc->ConnectTo(WordApp->Documents->Add());
InterWordDoc->ConnectTo(WordApp->Documents->Add(&TemplateFile, EmptyParam));
WordReplace("#YEAR#", "2009", WordApp, InterWordDoc);
WordReplace("#MONTH#", "11", WordApp, InterWordDoc);
WordReplace("#DAY#", IntToStr(15), WordApp, InterWordDoc);
WordReplace("#CURRENTTERMCODE#", "HEB-BD-AG9999", WordApp, InterWordDoc);
InterWordDoc->Select();
TVariant charCount = WordApp->ActiveDocument->Characters->Count;
InterWordDoc->Range(TVariant(0), charCount);
WordApp->Selection->Copy();
DestWordDoc->Select();
charCount = WordApp->ActiveDocument->Characters->Count;
DestWordDoc->Range(TVariant(0), charCount);
while (1)
{ //被选中的文档 (DestWordDoc)向下移动10屏,若返回值为0屏,则跳出
if (WordApp->Selection->MoveDown(OleVariant(wdScreen), OleVariant(10), EmptyParam) == 0)
break;
}
WordApp->Selection->Paste();
//设置第一个表格中单元格的内容
DestWordDoc->Tables->Item(1)->Cell(1,2)->Range->set_Text(WideString("张三"));
DestWordDoc->Tables->Item(1)->Cell(2,8)->Range->set_Text(WideString("乐透乐透"));
DestWordDoc->Tables->Item(1)->Cell(3,2)->Range->set_Text(WideString("新的TouZhuH"));
DestWordDoc->Tables->Item(1)->Cell(4,6)->Range->set_Text(WideString("人民币10000万"));
//设置第二个表格中单元格的内容
DestWordDoc->Tables->Item(2)->Cell(1,4)->Range->set_Text(WideString("13124198208216543"));
// 读取表格中单元格的内容
WideString str = DestWordDoc->Tables->Item(1)->Cell(2,4)->Range->get_Text();
Edit1->Text = str;
//保存对文件所作的修改
DestWordDoc->SaveAs(DestFile);
WordApp->Quit(OleVariant(wdDoNotSaveChanges));
WordApp->Disconnect();
}
catch(...)
{
ShowMessage("您可能没有安装Word!");
WordApp->Disconnect();
}
}
//---------------------------------------------------------------------------
- 1
- 2
前往页