1. delphi读取WORD文档每一页的内容
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;昨天没有测试好 这个应该没有问题了吧 试一下吧。
2. delphi读取WORD文档每一页的内容
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;
昨天没有测试好 这个应该没有问题了吧 试一下吧
3. Delphi中关于word操作的示例源文件谁有
一、Delphi程序启动Word采用CreateOleObjects的方法来启动Word,调用VBA代码,具体实现过程为:首先使用GetActiveOleObject('Word.Application')判断当前内存中是否存在Word程序,如果存在,则直接连接,如果没有Word程序,则使用CreateOleObject('Word.Application')启动Word二、Delphi程序新建Word文稿格式:WordDocuments.Add(Template,NewTemplate,DocumentType,Visible)Template: 使用模板的名称,NewTemplate: 新建文档的类型,True表示为模板,False表示为文档DocumentType: 文档类型,默认为空白文档Visible: 打捞的窗口是否可见举例:Doc_Handle:=Word_Ole.Documents.Add(Template:='C:\Temlate.dot',NewTemplate:=False);三、Delphi程序打开Word文稿格式:WordDocuments.Open(FileName,ConfirmConversions,ReadOnly,PassWordDocument,PasswordTemplate,Revent,WritePasswordDocument,WritePassWordTemplate,Format,Encoding,Visible)FileName: 文档名(包含路径)Confirmconversions: 是否显示文件转换对话框ReadOnly: 是否以只读方式打开文档AddToRecentFiles: 是否将文件添加到"文件"菜单底部的最近使用文件列表中PassWordDocument: 打开此文档时所需要的密码PasswordTemplate: 打开此模板时所需要的密码Revert: 如果文档已经,是否重新打开文档WritePasswordDocument: 保存对文档更改时所需要的密码WritePasswordTemplate: 保存对模板进行更改时所需要的密码Format: 打开文档时所需使用的文件转换器Encoding: 所使用的文档代码页Visible: 打开文档的窗口是否可见举例:Doc_Handle:=Word_Ole.Documents.open(FileName:=Doc_File,ReadOnly:=False,AddToRecentFiles:=False);四、Delphi程序保存Word文稿格式:WordDocuments.SaveAs(FileName, FileFormat, LockComments, Password,AddToRecentFiles, WritePassword, ReadOnlyRecommended,EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData,SaveAsAOCELetter)FileName: 文件名。
默认为当前文件夹和文件名。FileFormat 文档保存的格式。
LockComments 如果为 True,则此文档只允许进行批注。Password 打开文档时的口令。
AddToRecentFiles 如果为True,则将文档添至"文件"菜单中最近使用的文档列表中。WritePassword 保存对文档的修改所需的口令。
ReadOnlyRecommended 如果为 True,在每次打开文档时,Word 将建议用户采用只读方式。EmbedTrueTypeFonts 如果为 True,则将文档与 TrueType 字体一起保存。
SaveNativePictureFormat 如果为 True,则从其他系统平台(例如 Macintosh)导入的图形仅保存其 Windows 版本。SaveFormsData 如果为 True,则将窗体中用户输入的数据存为一条数据记录。
SaveAsAOCELetter 如果文档包含一个附加,当此属性值为 True 时,将文档存为一篇 AOCE 信笺(同时保存邮件)。举例:Word_Ole.Documents.SaveAs(FileName:=Doc_File,FileFormat=wdFormatDocument,AddToRecentFiles=False);。
4. 在Delphi中输入的数据填入Word表格中,并显示Word
uses
word2000, comobj;
var
MyWord, MyDoc :Variant;
begin
MyWord := CreateOleObject('Word.Application');
MyDoc := CreateOleObject('Word.Document');
MyWord.Visible := 1;
MyDoc := MyWord.Documents.Open(FileName := 'C:\A.doc', ReadOnly:=False);//打开外部Word文档
MyDoc.Tables.Item(表格序号).Cell(x, y).Range.Text := 'abcd';
end;
转载请注明出处51数据库 » delphi判断word版本
懒得起名了了