如何在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 RICHVIEW EDIT怎么设置上下标???
展开全部 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文档之急上加
展开全部 一. 定义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中?我没有使用数据库
写个函数给您,其中myw就是您的WordApplication组件,mydoc就是您的WordDocument组件procedure CreateNewWordDoc(myw: TWordApplication; mydoc: TWordDocument);overload;var p: OleVariant;beginmyw.Disconnect;p := EmptyParam;myw.connect();myw.Documents.Add(p, p, p, p);mydoc.ConnectTo(myw.ActiveDocument);myw.Visible:=true;end;
用Delphi在word中插入表
一个输出到模板表格中的例子 uses SysUtils,Windows,ADODB,DB,Word97,OleServer, Classes; var sSQL: String; qry: TADOQuery; fileN:OLEvariant; oleGrid: array[1..5] of olevariant; oWordApp: TWordApplication; oWordDoc: TWordDocument; sResult: String; i: Integer; begin qry:= nil; try sResult:= ' '; with qry do begin qry:= TADOQuery.Create(nil); qry.LockType:= ltReadOnly; //转换日期范围 //加载合同项目数据 sSQL:= 'select ProjectName, ProjectCode from FPPro_Project where ProjectID= '+ IntToStr(iProjectID); db_uPublic_GetRecordset(gConn, sSQL, qry); if RecordCount= 0 then begin //如果没有记录,退出 qry.Free; Result:= '没有找到项目记录! '; Exit; end; //连接Word oWordApp:= TWordApplication.Create(nil); owordDoc:= TWordDocument.Create(nil); try oWordApp.Connect; except qry.Free; owordDoc.Free; oWordApp.Free; Result:= '你可能没有安装Word! '; Exit; end; fileN:= fucGetExePath+ 'Template\基础资料清单.dot '; if not FileExists(fileN) then begin Result:= '没有找到模板文件! '; Exit; end; oWordApp.Documents.Add(fileN,emptyparam); oWordDoc.ConnectTo(oWordApp.ActiveDocument as _document); oWordApp.Options.CheckSpellingAsYouType:= False; //关闭拼写检查 oWordApp.Options.CheckGrammarAsYouType:= False; //关闭语法检查 oleGrid[1]:= oWordDoc.Tables.Item(1); oleGrid[1].Cell(1,2).Range.Text:= FieldByName( 'ProjectName ').AsString; oleGrid[1].Cell(1,4).Range.Text:= FieldByName( 'ProjectCode ').AsString; sSQL:= 'select FileName, CreateLogon, CreateDate from (FPPro_AddonFile ' + 'left join FPPro_RFile on FPPro_AddonFile.FileID=FPPro_RFile.FileID) ' + 'where FPPro_AddonFile.FileType=2 and FPPro_RFile.RelationKind=1 ' + 'and FPPro_RFile.RID= '+ IntToStr(iProjectID); db_uPublic_GetRecordset(gConn, sSQL, qry); oleGrid[1].Cell(6,1).Range.Text:= '共计份数: '+ IntToStr(RecordCount)+ '份 '; if RecordCount> 0 then begin //添加统计项目 First; i:=4; while not Eof do begin oleGrid[1].Cell(i,1).Range.Select; oWordApp.Selection.Rows.Add(emptyparam); //插入空行 oleGrid[1].Cell(i,1).Range.Text:= FieldByName( 'FileName ').AsString; oleGrid[1].Cell(i,2).Range.Text:= FieldByName( 'CreateLogon ').AsString; oleGrid[1].Cell(i,3).Range.Text:= FieldByName( 'CreateDate ').AsString; i:=i+1; Next; end; end; oWordApp.Visible:= True; oWordApp.WindowState:= 1; oWordApp.Activate; end; qry.Free; oWordApp.Free; owordDoc.Free; Result:= sResult; except on e: Exception do begin qry.Free; if owordDoc nil then owordDoc.Free; if oWordApp nil then oWordApp.Free; Result:= e.Message; end; end; end;
delphi 怎样将数据库的记录添加到word中去?
给你一小段代码,参考下wordapp=CreateOLEObject(Word.Application); wordapp.visible:=true; wordapp.documents.add('report.doc',False); wordapp.Documents.Item(1).Bookmarks.Item('first').Range.InsertAfter(first.text); wordapp.Documents.Item(1).Bookmarks.Item('second').Range.InsertAfter(second.text); wordapp.Documents.Item(1).Bookmarks.Item('third').Range.InsertAfter(third.text); wordapp.Documents.Item(1).Saveas('report1.doc'); 查看更多答案>>采纳哦
转载请注明出处51数据库 » delphi wordapp.docum