delphi读取WORD文档每一页的内容

我想把WORD每一页的内容分别做为一个记录存到数据库中。delphi如何实现。请高手把代码写在回答里。... 我想把WORD每一页的内容分别做为一个记录存到数据库中。delphi如何实现。请高手把代码写在回答里。 展开
 我来答
神说早安
推荐于2017-12-15 · 超过33用户采纳过TA的回答
知道答主
回答量:111
采纳率:0%
帮助的人:95.9万
展开全部
uses ComObj,WordXp;

var wordapp, WordDoc, PageRange: Variant;
sContext: string;
i, nPageCounts, nStart, nEnd : Integer;
begin
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := True;
if dlgOpen1.Execute = False then Exit;
WordDoc := wordapp.Documents.Open(dlgOPen1.FileName);
//文档总页数
nPageCounts := wordapp.Selection.Information[wdNumberOfPagesInDocument];

//如果只有一页 那么全选就OK了
if nPageCounts = 1 then
begin
wordapp.Selection.WholeStory;
mmo1.Lines.Add('=============第'+IntToStr(nPageCounts)+'页内容:===================');
mmo1.Lines.Add(wordapp.Selection.Text);
Exit;
end;

nStart := -1;
nEnd := -1;
//循环获取文档页中的内容
for i := 1 to nPageCounts do
begin
//定位到第i页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i));
//如果第i页是最后一页 那么直接将光标移动到最后 并输出内容
if i = nPageCounts then
begin
wordapp.Selection.EndKey(wdStory,wdExtend);
sContext := WordApp.Selection.Range.Text;
mmo1.Lines.Add('=============第'+IntToStr(i)+'页内容:===================');
mmo1.Lines.Add(sContext);
Exit;
end;

//取第i页的页首位置作为开始位置
nStart := wordapp.Selection.Start;
//定位到i+1页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i+1));
//取第i+1页的页首位置作为结束位置
nEnd := wordapp.Selection.Start;
//根据开始位置和结束位置确定文档选中的内容(第i页的内容)
WordDoc.Range(nStart,nEnd).Select;
sContext := WordDoc.Range.Text;
//输出内容
mmo1.Lines.Add('=============第'+IntToStr(i)+'页内容:===================');
mmo1.Lines.Add(sContext);
nStart := -1;
nEnd := -1;
end;
finally
wordapp.Quit;
end;
end;

昨天没有测试好 这个应该没有问题了吧 试一下吧
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
清风菜
2010-07-08 · TA获得超过222个赞
知道小有建树答主
回答量:240
采纳率:0%
帮助的人:0
展开全部
我来第一段,取得文档中的段落数
function GetParagraphsCount(docFileName:string):integer;
var Word : Variant;
begin
result:=0;
try
Word := CreateOLEObject('Word.Application');
Word.Documents.Open(docFileName,false);
result:=Word.ActiveDocument.Range.Paragraphs.Count;
finally
Word.Quit;
end;
end;

//打开文档
procedure TForm1.OpenWord(Sfile: string);
var
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format,
Encoding, Visible, OpenAndRepair,
DocumentDirection, NoEncodingDialog: OleVariant;
ItemIndex: OleVariant;
begin
FileName := sfile;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;
//打开文件
WordApplication1.Documents.Open(FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format,
Encoding, Visible, OpenAndRepair,
DocumentDirection, NoEncodingDialog);
ItemIndex := 1;
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
WordApplication1.Connect;
WordApplication1.Visible := True;
WordApplication1.Disconnect;
end;

//设置页眉页脚
procedure TForm1.Button5Click(Sender: TObject);
var
mmm, nnn, aaa: OleVariant;
begin
mmm := wdLine;
nnn := 1;
aaa := wdFieldPage;
worddocument1.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
worddocument1.activewindow.Selection.Move(mmm, nnn);
worddocument1.activewindow.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;

worddocument1.activewindow.Selection.InsertAfter('第');
mmm := wdCharacter;
worddocument1.activewindow.Selection.Move(mmm, nnn);
worddocument1.activewindow.Selection.Fields.Add(worddocument1.activewindow.Selection.Range, aaa, mmm, nnn);
aaa := wdFieldNumPages;
worddocument1.activewindow.Selection.InsertAfter('页/第');
worddocument1.activewindow.Selection.Move(mmm, nnn);
worddocument1.activewindow.Selection.Fields.Add(worddocument1.activewindow.Selection.Range, aaa, mmm, nnn);
worddocument1.activewindow.Selection.InsertAfter('页');

mmm := wdWord;
nnn := 2;
worddocument1.activewindow.Selection.Move(mmm, nnn);
worddocument1.activewindow.activepane.selection.insertafter(#13+'武汉大学中南医院');

//结束编辑
worddocument1.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
end;
//替换页眉
procedure TForm1.Button6Click(Sender: TObject);
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace,
MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl:olevariant;

i:Single;
begin
matchcase := false;
matchwholeword := true;
matchwildcards := false;
matchsoundslike := false;
matchallwordforms := false;
forward := true;
wrap := wdfindcontinue;
format := false;
replace := true;
MatchKashida:=True;
MatchDiacritics:=True;
MatchAlefHamza:=True;
MatchControl:=True;

findtext := '武汉'; //'<#姓名>'
replacewith := '1234';

worddocument1.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
worddocument1.activewindow.activepane.selection.Find.Execute(Findtext, matchcase, matchwholeword,
matchwildcards, matchsoundslike, matchallwordforms, forward,
wrap, format, replacewith, replace,MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl);
//结束编辑
worddocument1.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
end;
还有很多 提交不上来....

参考资料: word OLE

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式