delphi读取WORD文档每一页的内容
我想把WORD每一页的内容分别做为一个记录存到数据库中。delphi如何实现。请高手把代码写在回答里。...
我想把WORD每一页的内容分别做为一个记录存到数据库中。delphi如何实现。请高手把代码写在回答里。
展开
2个回答
展开全部
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;
昨天没有测试好 这个应该没有问题了吧 试一下吧
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;
昨天没有测试好 这个应该没有问题了吧 试一下吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
博思aippt
2024-07-20 广告
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT...
点击进入详情页
本回答由博思aippt提供
展开全部
我来第一段,取得文档中的段落数
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;
还有很多 提交不上来....
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询