if WordApp.Visible=false then //运行Word,并新建一文档 WordApp.Visible:=true; filename:=''; tempboolean:=false; WordApp.Documents.Add(filename,tempboolean); if wordtype.radiogroup1.ItemIndex=0 then WordApp.ActiveDocument.PageSetup.Orientation:=wdOrientLandscape;//将纸张调为横向 with WordApp.Selection do//将标题写入Word文档 begin Paragraphs.Alignment:=1; Font.Size:=20; Font.Bold:=1; TypeText(panel12.Caption); end; with WordApp.Selection do//将各参数还原 begin TypeText(#13); Paragraphs.Alignment:=0; Font.Size:=11; Font.Bold:=0; end; for i:=0 to rect_num-1 do //将矩形对应的模块(文字、图、表)写入Word文档 begin temptext:=copy(Word_Text[i],1,pos('_',Word_Text[i])-1); if temptext='文字材料' then begin pathname:=extractfilepath(application.ExeName)+'Text\'; temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i])); filename:=pathname+temptext+'.txt'; range:=''; confirm:=false; link:=false; attachment:=false; WordApp.Selection.TypeParagraph; WordApp.Selection.InsertFile(filename,range,confirm,link,attachment); end; if temptext='统计分析表' then begin pathname:=extractfilepath(application.ExeName)+'Tables\'; temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i])); filename:=pathname+temptext+'.htm'; range:=''; confirm:=false; link:=false; attachment:=false; start:=WordApp.Selection.Get_Start; WordApp.Selection.InsertFile(filename,range,confirm,link,attachment); endof:= WordApp.Selection.Get_Start; WordApp.ActiveDocument.Range(start,endof).select; WordApp.Selection.Font.Name:='宋体'; WordApp.Selection.Font.Size:=9; WordApp.Selection.Set_Start(endof); end; if temptext='统计分析图' then begin pathname:=extractfilepath(application.ExeName)+'StatBmp\'; temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i])); filename:=pathname+temptext+'.bmp'; start:=WordApp.Selection.Range; withfilesave:=true; wordApp.Selection.TypeText(' '); WordApp.Selection.Paragraphs.Alignment:=1; WordApp.Selection.InlineShapes.AddPicture(filename,attachment,withfilesave,start); end; end; WordApp.Disconnect; end;我的代码,成功插入图片和文字,看不懂问我
按照你的要求,我在WORD中录制了一个宏,然后取得如下的代码:Sub TestInsert() ' ' TestInsert Macro ' 宏在 2002-10-15 由 manager 录制 ' Selection.MoveUp Unit:=wdLine, Count:=5 Selection.TypeText Text:=" This is a Test " Selection.InlineShapes.AddPicture FileName:= _ "C:\Documents and Settings\Administrator\My Documents\My Pictures\1.bmp", _ LinkToFile:=False, SaveWithDocument:=True End Sub 根据这些代码,我们可以用DELPHI写出相应的代码:procedure TNewCardForm.Button1Click(Sender: TObject); var WordApp,MyWorkBook: OLEVariant; begin try wordApp:=CreateOleObject('Word.Application'); except application.Messagebox('无法打开word文件,请确认已 经安装word.','', mb_OK+mb_IconStop); Exit; end; MyworkBook:= wordApp.documents.Open('doc1.doc');
myworkbook.MoveUp(,5); Myworkbook.TypeText('This is a Test'); Myworkbook.InlineShapes.AddPicture('C:\Documents and Settings\Administrator\My Documents\My Pictures\1.bmp',false,true);
WordApp.Visible:=true;
filename:='';
tempboolean:=false;
WordApp.Documents.Add(filename,tempboolean); if wordtype.radiogroup1.ItemIndex=0 then
WordApp.ActiveDocument.PageSetup.Orientation:=wdOrientLandscape;//将纸张调为横向
with WordApp.Selection do//将标题写入Word文档
begin
Paragraphs.Alignment:=1;
Font.Size:=20;
Font.Bold:=1;
TypeText(panel12.Caption);
end;
with WordApp.Selection do//将各参数还原
begin
TypeText(#13);
Paragraphs.Alignment:=0;
Font.Size:=11;
Font.Bold:=0;
end; for i:=0 to rect_num-1 do //将矩形对应的模块(文字、图、表)写入Word文档
begin temptext:=copy(Word_Text[i],1,pos('_',Word_Text[i])-1);
if temptext='文字材料' then
begin
pathname:=extractfilepath(application.ExeName)+'Text\';
temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i]));
filename:=pathname+temptext+'.txt';
range:='';
confirm:=false;
link:=false;
attachment:=false;
WordApp.Selection.TypeParagraph;
WordApp.Selection.InsertFile(filename,range,confirm,link,attachment); end;
if temptext='统计分析表' then
begin
pathname:=extractfilepath(application.ExeName)+'Tables\';
temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i]));
filename:=pathname+temptext+'.htm';
range:='';
confirm:=false;
link:=false;
attachment:=false; start:=WordApp.Selection.Get_Start;
WordApp.Selection.InsertFile(filename,range,confirm,link,attachment);
endof:= WordApp.Selection.Get_Start;
WordApp.ActiveDocument.Range(start,endof).select;
WordApp.Selection.Font.Name:='宋体';
WordApp.Selection.Font.Size:=9;
WordApp.Selection.Set_Start(endof);
end;
if temptext='统计分析图' then
begin
pathname:=extractfilepath(application.ExeName)+'StatBmp\';
temptext:=copy(Word_Text[i],pos('_',Word_Text[i])+1,length(Word_Text[i]));
filename:=pathname+temptext+'.bmp';
start:=WordApp.Selection.Range;
withfilesave:=true;
wordApp.Selection.TypeText(' ');
WordApp.Selection.Paragraphs.Alignment:=1;
WordApp.Selection.InlineShapes.AddPicture(filename,attachment,withfilesave,start);
end;
end;
WordApp.Disconnect;
end;我的代码,成功插入图片和文字,看不懂问我
'
' TestInsert Macro
' 宏在 2002-10-15 由 manager 录制
'
Selection.MoveUp Unit:=wdLine, Count:=5
Selection.TypeText Text:=" This is a Test "
Selection.InlineShapes.AddPicture FileName:= _
"C:\Documents and Settings\Administrator\My Documents\My Pictures\1.bmp", _
LinkToFile:=False, SaveWithDocument:=True
End Sub
根据这些代码,我们可以用DELPHI写出相应的代码:procedure TNewCardForm.Button1Click(Sender: TObject);
var
WordApp,MyWorkBook: OLEVariant;
begin
try
wordApp:=CreateOleObject('Word.Application');
except
application.Messagebox('无法打开word文件,请确认已 经安装word.','',
mb_OK+mb_IconStop);
Exit;
end;
MyworkBook:= wordApp.documents.Open('doc1.doc');
myworkbook.MoveUp(,5);
Myworkbook.TypeText('This is a Test');
Myworkbook.InlineShapes.AddPicture('C:\Documents and Settings\Administrator\My Documents\My Pictures\1.bmp',false,true);
wordApp.quit;
wordApp:=Unassigned; //释放VARIANT变量
end;
NewTemplate :=False,
DocumentType:=0,
Visible:=True);但是在尝试代码:
WordApp.Selection.TypeText(' ');
WordApp.Selection.Paragraphs.Alignment:=1;
WordApp.Selection.InlineShapes.AddPicture(
FileName:='C:\1.bmp',
attachment:=false,
withfilesave:=true,
start:=WordApp.Selection.Range );
无法成功。
错误信息:
Method 'AddPicture' not supported by automation obiect.谢谢,等待中...