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;昨天没有测试好 这个应该没有问题了吧 试一下吧
在DELPHI 中使用 word 文档
这个问题难不住我~1.首先你可以使用ole 调用word ,将word内容在delphi界面显示出来。
2.然后利用word本身的‘宏’命令。
选择‘录制新宏’。
3.在宏录制状态下做你想做的操作(将字段插入word中你想要的任何地方)。
操作完毕后选择停止录制。
4.打开刚刚录制的‘宏’。
选择编辑,把VBA命令转换为delphi代码。
(其实你应该去大富翁看看,那里有很多答案)
我想在Delphi往一个已经设计好的Word模板的特定位置写内容,怎么...
一. 定义word应用程序,文档对象,比如:wordApp: OleVariant;wordDoc: OleVariant;二. 打开word文档wordApp := CreateOleObject('Word.Application');wordDoc := WordApp.Documents.Open(FileName:=filename,ReadOnly:= false, Revert:=True, Visible:= True);WordApp.Visible := TRUE;三.表格插入数据WordDoc.Tables.Item(TbIndex).cell(cellrow,cellcol).Range.InsertAfter(conStr);四.在指定行写数据WordDoc.Paragraphs.Item(16).Range.InsertAfter('abcd');五.指定字符位置插入数据MyRange: OleVariant;MyRange := wordDoc.Range(Start:=0,End:=10);MyRange.InsertAfter('abcd'+#13);六.文件保存wordDoc.SaveAs(filename);七.连接word(联系:二打开文档)//测试当前是否运行了wordtryv := getactiveoleobject(word.application);except//未运行则运行之v := createoleobject(word.basic);end;r /> try//连接到wordwordapp.connect;exceptbeep;messagedlg(不能生成文档,请确认是否安装了word 2000!, mterror, [mbok], 0);abort;end;//八.复制打开的word的内容WordApp := CreateOleObject('Word.Application'); //创建word对象WordApp.Documents.open(file1); //打开一个word文档wordapp.visible:=false; //是否让打开的word文档显示出来wordapp.activedocument.select; //选取打开的word文档中全部内容wordapp.selection.copy; //拷贝选取的内容memo1.PasteFromClipboard; //把拷贝的内容粘贴到richedit中WordApp.Quit; //关闭对象九.粘贴至word(粘贴板)WordApp := CreateOleObject('Word.Application'); //创建word对象WordApp.Visible:= True ;WordApp.Documents.Add;WordApp.Documents.Item(1).Range.paste;十.新建文档:格式:WordDocuments.Add(Template,NewTemplate,DocumentType,Visible)Template: 使用模板的名称,NewTemplate: 新建文档的类型,True表示为模板,False表示为文档DocumentType: 文档类型,默认为空白文档Visible: 打捞的窗口是否可见举例:Doc_Handle:=Word_Ole.Documents.Add(Template:='C:/Temlate.dot',NewTemplate:=False);十一.连接已打开文档WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
请问:如何通过delphi向word文档的指定页(比如,第二页)添加文本...
我将我的商用《旅行社管理系统》的 发团通知 部分奉献给您,望对您有所帮助。
procedure TFrmMain.N327Click(Sender: TObject); var FWord :Variant; FDoc :Variant; strSQL :string; begin //首先创造对象,如果出现异常就做出提示 try FWord := CreateOleObject('Word.Application'); //WOrd程序的执行是否可见,值为false时,程序在后台运行。
FWord.Visible := True; except messagebox(0,'创建word对象失败!','警告',MB_OK+MB_ICONINFORMATION); exit; end; strSQL :='select * From "B_T_Company_Infor"'; with dm.ClientDataSet_Temp do begin if Active then Active:=False; CommandText:=strSQL; Open; end; //现在打开的Word中,创建一个新的页面,然后在其中键入需要的内容 try FDOC :=FWord.Documents.Add; {对于Word的新页面} FWord.Selection.Font.Name :='宋体'; FWord.Selection.Font.Size :=20; FWord.Selection.Font.Bold :=2; FWord.Selection.paragraphs.Alignment := wdAlignParagraphCenter; FWord.Selection.TypeText(dm.ClientDataSet_Temp.Fields[0].AsString); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph; FWord.Selection.paragraphs.Alignment := wdAlignParagraphLeft; FWord.Selection.Font.Size :=12; FWord.Selection.Font.Bold :=0; FWord.Selection.TypeText(' 现将我社组团部的'); FWord.Selection.TypeText(' '); FWord.Selection.TypeText('团计划发出,各地请按下列行程做接待,并及早落实'+ '团队计划和选派优秀导游员。
各地代订住宿、火车票、机票务必安排好接待,日程'+ '如有变更,及时与下站接待社或本社联系,各地接团时请举【'+ dm.ClientDataSet_Temp.Fields[0].AsString+'】接站牌。
'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('请速回传真确认,谢谢合作!'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('人数:'); FWord.Selection.TypeText('人数:'); FWord.Selection.TypeText('国籍:'); FWord.Selection.TypeText('国籍:'); FWord.Selection.TypeText('等级:'); FWord.Selection.TypeText('等级:'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('这里是团队安排'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('TO: '); FWord.Selection.TypeText('12345678901234567890'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('TEL: '); FWord.Selection.TypeText('12345678901234567890'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('FAX: '); FWord.Selection.TypeText('12345678901234567890'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('______________________________________________'); FWord.Selection.Typeparagraph;//换行 FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('FROM: '); FWord.Selection.TypeText(dm.ClientDataSet_Temp.Fields[0].AsString); FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('TEL: '); FWord.Selection.TypeText(dm.ClientDataSet_Temp.Fields[3].AsString); FWord.Selection.Typeparagraph;//换行 FWord.Selection.TypeText('FAX: '); FWord.Selection.TypeText(dm.ClientDataSet_Temp.Fields[4].AsString); FWord.Selection.Typeparagraph;//换行 Except on e: Exception do Messagebox(0,Pchar(e.Message),'警告',MB_OK+MB_ICONINFORMATION); end; //保存文档 FWord.Caption := '组团计划保存在: '+ExtractFilePath(application.ExeName)+'Reports\GroupPlan.doc'; FDOC.SaveAS(ExtractFilePath(application.ExeName)+'Reports\GroupPlan.doc'); if messagebox(0,'组团计划已经保存成功!'+chr(13)+chr(13)+ '单击【确定】退出Word,返回到程序中!','您确信要退出Word吗?', MB_YESNO+MB_ICONINFORMATION+MB_DEFBUTTON2)=IDYES then begin FWord.Quit; FWord := Unassigned; end; end; ----------------------------- Delphi与Word之间的融合技术(1) 一、VBA代码含义 Microsoft Word是一个集成化环境,是美国微软公司的字处理系统,但是它决不仅仅是一个字处理系统,它集成了Microsoft Visual Basic,可以通过编程来实现对Word功能的扩展。
Microsoft Visual Basic在word中的代码即Word的宏,通过编写Word宏,可实现一些文档处理的自动化,如实现文档的自动备份、存盘等,可扩展Word文档的功能,因此,能够充分利用Word的特性,甚至使Word成为自己软件的一部分。
Word的宏既有有利的一部分,因为它能够帮助我们实现文档的自动化,但是Word的宏也不是纯粹的有利,有时它可能危害我们的文档、计算机系统甚 至网络,从最开始的Taiwan NO1宏病毒到现在的...
Delphi 中怎样获得文本框中被选取的文字内容
//试下用idhttpuses IdHTTP;function GetWebPage(const Url :string; IsUtf8 :Boolean = False):string;var idp :TIdHTTP;begin Result := ''; idp := TIdHTTP.Create(Application); try idp.HandleRedirects := True; idp.Request.UserAgent := 'Mozilla/5.0'; Result := idp.Get(Url); if IsUtf8 then Result := Utf8ToAnsi(Result); finally FreeAndNil(idp); end;end;//当网页编码是utf8时,第2个参数请为true,要不汉字会出现乱码ShowMessage(GetWebPage(Load_web_update + '/SysUpFiles.htm'));...
转载请注明出处51数据库 » delphi word 内容