小弟我最近在做一个关于文档处理的程序,要求是把数据库中的相应字段导入到Word中去,看到一些资料上介绍可以用Olecontainer把word显示出来,但是没有讲怎么才能在窗体中的Olecontainer显示word的同时,把DBedit中的字段替换相应的word中文档的文字空缺处.比如说我的word文档是:今天的温度是(空一),风速是(空二).我的dbedit1.text='温度',dbedit2.text='风速',我想把dbedit1.text和dbedit2.text的内容直接添入到olecontainer中word的空一和空二两处.
  看了一些资料,好像worddocument好像可以解决,但那时建立在worddocument连接到wordapplication的基础上的,我想把worddocument.connectto的属性连接到olecontainer中,这可以吗?
  能帮我解决上面的问题者,小弟不胜感激,马上结分100表示谢意,谢谢各位大虾!

解决方案 »

  1.   

    轉貼:
    查询记录导出为word文档的问题procedure TForm1.Button11Click(Sender: TObject);
    var
      WordApp,WordDoc,WordTable:OleVariant;
      i,j:integer;
    begin
      WordApp:=CreateOleObject('Word.Application');
      WordApp.Visible:=True;
      WordDoc:=WordApp.Documents.Add;
      WordTable:=WordDoc.Tables.Add(WordApp.Selection.Range,DBGrid1.DataSource.DataSet.RecordCount+1,DBGrid1.Columns.Count);  for i:=1 to DBGrid1.Columns.Count do
      WordTable.Cell(1,i).Range.InsertAfter(DBGrid1.Columns[i-1].Title.Caption);
      i:=2;
      with DBGrid1.DataSource.DataSet do
        while not eof do
         begin
           for j:=1 to DBGrid1.Columns.Count-1 do
            WordTable.Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
           Next;
           Inc(i);
        end;
    end;怎样读出word文件里面的内容,并保存到一个字符串变量中建立WordApplication和WordDocument控件,以及一个Memo和按钮,用以下程序可以实现,不过只能取出Word中所有的文字,如果有表格或其它格式的话,只取其中的文字。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, OleServer, Word97;type
      TForm1 = class(TForm)
        Button1: TButton;
        WordDocument2: TWordDocument;
        Memo1: TMemo;
        WordApplication2: TWordApplication;
        procedure Button1Click(Sender: TObject);
      private
        Procedure PrintToWord(modulefile:String);
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    Procedure TForm1.PrintToWord(modulefile:String);
    var
      PostRange:Variant;
      I:Integer;
      itemindex:olevariant;  filename,confirmconversions, readonly, addtorecentfiles: olevariant;
      passworddocument, passwordtemplate, revert,writepassworddocument: olevariant;
      writepasswordtemplate, open_format: olevariant;
    begin
      filename:=modulefile;  confirmconversions := false;
      readonly := true;
      addtorecentfiles := false;
      passworddocument := '';
      passwordtemplate := '';
      revert := true;
      writepassworddocument := '';
      writepasswordtemplate := '';
      open_format := wdopenformatdocument;  WordApplication2.Connect;  WordApplication2.documents.open( filename, confirmconversions,
    readonly, addtorecentfiles, passworddocument, passwordtemplate,
    revert, writepassworddocument, writepasswordtemplate, open_format );
      itemindex := 1;
      WordDocument2.connectto(WordApplication2.documents.item(itemindex));end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      PrintToWord(你要打开的文件名);
      Memo1.lines.Add(WordDocument2.Content.Get_Text);
    end;end.
    我加了两个参数Encoding,Visible进去就可以
      

  2.   

    procedure TFrmbbfxreport.SBexportdataClick(Sender: TObject);
    var
     i,row:integer;
    begin
     if SaveDialog1.Execute then
     begin
       Screen.Cursor:=crHourGlass;
       ExcelApplication1.Connect;
       ExcelApplication1.Workbooks.Add(Null,0);
       ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
       ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
       if not cwxtdm.atbltemp.Active then
       begin
         cwxtdm.atbltemp.Open;
       end;
       for i:=0 to cwxtdm.atbltemp.Fields.Count-1 do
         ExcelWOrkSheet1.Cells.Item[1,i+1]:=cwxtdm.atbltemp.Fields[i].FieldName;
       row:=2;
       while not cwxtdm.atbltemp.Eof do
       begin
         for i:=0 to cwxtdm.atbltemp.Fields.Count-1 do
         begin
           ExcelWOrkSheet1.Cells.Item[row,i+1]:=cwxtdm.atbltemp.Fields[i].AsString;
         end;
         row:=row+1;
         cwxtdm.atbltemp.Next;
       end;
       ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
       ExcelWOrkBook1.Close(false);
       ExcelApplication1.Disconnect;
       ExcelApplication1.Quit;
       Screen.Cursor:=crDefault;
       Application.MessageBox('导出成功!','提示',0);
     end;
    end;  
      

  3.   

    对不起,上面两位的回答好像和我问的有些出入.
    我想在Form上面加上一个Olecontainer的控件,然后把word文挡嵌入到其中,这样用户可以不必切换程序就可以在同一个窗体中既可以作数据库操作,同时也可以看到word文档.