乔山办公网我们一直在努力
您的位置:乔山办公网 > office365 > 用Delphi在word中插入表-delphi添加office工具栏,office无工具栏

用Delphi在word中插入表-delphi添加office工具栏,office无工具栏

作者:乔山办公网日期:

返回目录:office365


选中要插入的行,单击word工具栏的『表格布局』可选择的有:上方插入、下方插入、左边插入、右边插入等。mac的word和windows操作不太相同,刚才看了下面几个答案,都不是正确的解决办法,正好自己也在摸索插入的方法,操作了几下,发现了。以补充给需要的同学。

一个输出到模板表格中的例子
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:= '没有找e799bee5baa6e79fa5e98193e59b9ee7ad94332到项目记录! ';
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;
一. 定义word应用程序,文档对象e79fa5e98193e4b893e5b19e333,比如:
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(联系:二打开文档)
//测试当前是否运行了word
try
v := getactiveoleobject(word.application);
except
//未运行则运行之
v := createoleobject(word.basic);
end;r /> try
//连接到word
wordapp.connect;
except
beep;
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));

是说参数不够。应该还需要别的参数
//先定义一个OLE变量
insertLine: OleVariant ;

//在要插入的地方插入代码
insertLine:=1;//要插入的行数
WordApp.Selection.InsertRowsBelow(insertLine);

//如果对WORD操作不熟,可参考下面的网址
http://hi.baidu.com/jinsu/blog/item/42c2a53ee3e2c1fd828b139d.html

相关阅读

关键词不能为空
极力推荐
  • -office2010标准版神key,office版本

  • 首先,如果之前安装OFFICE不成功,或者无法正常卸载,请看下面文章:Windows安装程序清除:http:///post/WindowsInstallCleanup.html并且,卸载家庭版和学生版(阉割版),用以下方案。以下为几

ppt怎么做_excel表格制作_office365_word文档_365办公网