D7操作word代码报错,大家看看错在哪?30分送上(30)

  • 主题发起人 主题发起人 楚辞
  • 开始时间 开始时间

楚辞

Unregistered / Unconfirmed
GUEST, unregistred user!
Button1: TButton; Button2: TButton; Button3: TButton; Memo1: TMemo; WordApplication1: TWordApplication; WordDocument1: TWordDocument; WordFont1: TWordFont;procedure TForm1.FormCreate(Sender: TObject);begin button1.Caption:='插入到word'; button2.Caption :='打印'; button3.Caption :='退出'; button1.Enabled :=false; button2.Enabled :=false;end;procedure TForm1.Button1Click(Sender: TObject);vartemplate,newtemplate,itemindex:olevariant; procedure setfont;begin wordfont1.ConnectTo(worddocument1.Sentences.Last.Font ); wordfont1.Bold :=1; wordfont1.Italic :=1; wordfont1.Shadow :=1; WordFont1.Size := 12;end; begin try template:=emptyparam; newtemplate:=true; itemindex:=1; try Wordapplication1.Connect; except MessageDlg('无法连接,也许没有安装Word', mtError, [mbOk], 0); abort; end; Wordapplication1.Visible := False;WordApplication1.Caption := 'Delphi Word'; //file://建立一个新文档Template := EmptyParam;NewTemplate := False;//报错地方 WordApplication1.Documents.Add(template, newtemplate);//[Error] Unit1.pas(74): Not enough actual parameters//file://建立WordDocument连接WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));//file://因为Word进行拼写检查需要很多时间,所以首先关闭检查WordApplication1.Options.CheckSpellingAsYouType:= False;WordApplication1.Options.CheckGrammarAsYouType := False;//file://将Memo1的内容拷贝到Word中 SetFont;WordDocument1.Range.InsertAfter('Memo1的文本: '+#13+Memo1.Text+#13);Button2.Enabled :=True;Button3.Enabled :=True;Button1.Enabled :=False;excepton E:Exception dobeginShowMessage(E.Message);WordApplication1.Disconnect;end; end;procedure TForm1.Button2Click(Sender: TObject);beginWordDocument1.PrintOut;end;procedure TForm1.Button3Click(Sender: TObject); varSaveChanges,OriginalFormat,RouteDocument,SavePath: OleVariant;beginSaveChanges := WdDoNotSaveChanges;OriginalFormat := UnAssigned;RouteDocument := UnAssigned;trySavePath:='c:/samp.doc';//这个地方我没WordDocument1.SaveAs(SavePath);WordDocument1.Close;WordDocument1.Disconnect;WordApplication1.Quit(SaveChanges, OriginalFormat, RouteDocument);WordApplication1.Disconnect;close;excepton E: Exception dobeginShowmessage(E.Message);WordApplication1.Disconnect;end;end;end;
 
请大家看看.
 
摘自:http://topic.csdn.net/t/20040211/16/2723814.htmlWordapplication1.Documents.Add(Template,NewTemplate,DocumentType,Visible) Template: 使用模板的名称 NewTemplate: 新建文档的类型,True表示为模板,False表示为文档 DocumentType: 文档类型,默认为空白文档 Visible: 打捞的窗口是否可见 举例: var visible,Template,NewTemplate,DocuType: OleVariant; //赋初值 Template:=''; NewTemplate:=False; DocuType:=wdNewBlankDocument; Visible:=true; //然后使用 wordapplication1.documents.add(Template,NewTemplate,DocuType,Visible);
 
谢谢.试试看.
 
procedure TForm1.Button1Click(Sender: TObject);vartemplate,newtemplate,Visible,itemindex,DocuType:olevariant;加上这句成功.谢谢.
 
后退
顶部