用OleContainer吗?大家谁能给出一个具体操作的例子。包括可以在delphi程序中对word进行各种操作的代码。

解决方案 »

  1.   

    启动Word时用如下代码: 
    begin
    try 
    Wordapplication.Connect; 
    except 
    MessageDlg('Word may not be installed', mtError, [mbOk], 0); 
    Abort; 
    end; 
    Wordapplication.Visible := True; 
    WordApplication.Caption := 'Delphi automation'; 
    end; 
    让Word打开一个指定的文件,需要先放置OpenDialog,然后调用WordApplication.Documents.Open: 
    var 
    ItemIndex :OleVariant; 
    FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, 
    PasswordDocument, PasswordTemplate, Revert, 
    WritePasswordDocument, WritePasswordTemplate, Format: OleVariant; 
    begin 
    if not dlgOpen.Execute then 
    Exit; {Open document} 
    FileName := dlgOpen.FileName; 
    ConfirmConversions := False; 
    ReadOnly := False; 
    AddToRecentFiles := False; 
    PasswordDocument := ''; 
    PasswordTemplate := ''; 
    Revert := True; 
    WritePasswordDocument := ''; 
    WritePasswordTemplate := ''; 
    Format := wdOpenFormatDocument; WordApplication.Documents.Open( FileName, ConfirmConversions, 
    ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, 
    Revert, WritePasswordDocument, WritePasswordTemplate, Format ); {Assign WordDocument component} 
    ItemIndex := 1; 
    WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex)); {Turn Spell checking of because it takes a long time if enabled and slows down Winword} 
    WordApplication.Options.CheckSpellingAsYouType := False; 
    WordApplication.Options.CheckGrammarAsYouType := False; 
    end;  判断Word是否运行先在Form上添加一个Server组件中的WordApplication,命名为WordApplication1,然后在"关闭WORD"按扭中输入如下代码:
    var
        SaveChanges, OriginalFormat, RouteDocument: OleVariant;
    begin
        SaveChanges := WdDoNotSaveChanges;
        OriginalFormat := UnAssigned;
        RouteDocument := UnAssigned;
        try
            WordApplication1.Quit(SaveChanges, OriginalFormat, RouteDocument);
            WordApplication1.Disconnect;
        except
        on E: Exception do
            begin
                Showmessage(E.Message);
                WordApplication1.Disconnect;
            end;
        end;
    end;
    准成,当然了,请按实际情况修改 SaveChanges, OriginalFormat, RouteDocument的内容. 
      

  2.   

    如何输出到Word如果用的是MICROSOFT 旗下的数据库管理系统生成的表例如*.DBF等,可以通过OLE技术直接在WORD文档中显示完整的数据表。
    如果用PARADOX表,可以用下面的方法:
    var
      msword:variant;
    begin
      try
        msword:=createoleobject('word.basic');
        msword.filenew;
        msword.appshow;
        table1.disablecontrols;
        try
          book:=table1.getbook;
          try
            msword.bold;
            msword.insert('报表标题'#13#10);
            msword.insert('字段名1'+#9+....+'字段名n'#13#10);
            table1.first;
            while not table1.eof do
              begin
                msword.insert(table1.fieldbyname(字段名1).asstring+#9+.....+table1.fieldbyname(字段名n).asstring+#13#10);
                msword.next;
              end;
             finally
               table1.gotobook(book);
               table1.freebook(book);
             end;
           finally
             table1.enablecontrols;
            end;
        except
           showmessage('没有发现WORD,请安装!');
        end;
    end;
    //在OLE服务器WORD启动后自动写了一个制表文件,此时只要选择全部数据(除标题),然后选插入表格即可作出WORD报表。
    //如果有其他好方法请与我联系。
    //我想解决的是在程序中自动画出表格。  控制Wordvar
    tbl : Table;
    i,j:integer;
    un_Var,ex_Var,cnt_Var:OleVariant;
    row_num,col_num:integer;
    st:string;
    begin
        // 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小
        WordApplication1.Connect;
        WordApplication1.Visible := True;
        WordApplication1.Documents.Add(EmptyParam,EmptyParam);
        WordDocument1.Connect;
        WordApplication1.ActiveWindow.View.Type_:= wdNormalView;
        WordApplication1.Selection.Font.Name :='黑体';
        WordApplication1.Selection.Font.Size := 16;
        WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
        WordApplication1.Selection.TypeText('昆明市土地信息系统表格输出');
        WordApplication1.Selection.TypeParagraph;
        WordApplication1.Selection.TypeParagraph;
        WordApplication1.Selection.Font.Name :=  '宋体';
        WordApplication1.Selection.Font.Size :=12;
        WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
        WordApplication1.Selection.TypeText('日期'+formatdatetime('yyyy"年"mm"月"dd"日"',now));
        WordApplication1.Selection.TypeParagraph;
        WordApplication1.Selection.TypeParagraph;//回车
        WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
        row_num:=table1.RecordCount;
        col_num:=table1.Fields.Count;
        tbl := WordApplication1.ActiveDocument.Tables.Add(WordApplication1.Selection.Range,row_num+1,Col_num);
        un_Var:=wdCharacter;
        cnt_Var:=1;
        ex_Var:=wdMove;
        table1.First;
        for j := 0 to Col_num-1 do    //标题
        begin
                st:=table1.Fields.Fields[j].FieldName;
                WordApplication1.Selection.TypeText(st);
                WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
        end;
        for i := 0 to row_num-1 do    // 行
        begin
            for j := 0 to Col_num-1 do    // 列
            begin
                st:=table1.Fields.Fields[j].AsString;
                WordApplication1.Selection.TypeText(st);
                WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
            end;
            WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
            table1.next;
        end;
        WordApplication1.Selection.TypeText('制表人:阎磊');
        WordApplication1.Selection.TypeParagraph;
    end; 
      

  3.   

    打开WORD文档的一段程序
    uses ComObj ;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      vWord,vDoc,vRange : Variant ;
      sText,sReplace : string ;
      lReturn : Boolean ;
    begin
      sText := 'ABCDEFG' ;  //原文字串
      sReplace := 'GFEDCBA' ; //新字串
      vWord := CreateOleObject('Word.Application') ;//创建Word线程
      try
        //打开要操作的文件
        vDoc := vWord.Documents.Open('C:\My Documents\AAAc.Doc');
        vDoc.Select ;  //选取中整个文档
        vRange := vDoc.Range ; //替换范围
        lReturn := True ;
        while lReturn do
        begin  //找到并替代成功则返回 True    共11个参数
          lReturn := vDoc.Range.Find.Execute(sText,,,,,,,,,sReplace,True) ;
        end ;
      finally
        vDoc.Close(True) ; //关闭文并保存
        vWord.Quit(False) ; //退出Word
      end ;
    end;
      

  4.   

    楼上的你的是用delphi调用word,并不是内嵌在form中。
      

  5.   

    用OleContainer和WebBrower可以做到,
    但是控制比较麻烦
      

  6.   

    控制WORD文档的一段程序
    unit Unit1;
     
    interface
     
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    StdCtrls,OleCtnrs,ComObj;
     
    type
        TForm1 = class(TForm)
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        Label4: TLabel;
        Label5: TLabel;
        ED_WenHao: TEdit;
        ED_BiaoTi: TEdit;
        ED_ShouWenDanWei: TEdit;
        ED_ZhenWen: TMemo;
        ED_FaWenDanWei: TEdit;
        Btn_PrintToWord: TButton;
        Btn_Quit: TButton;
        procedure Btn_PrintToWordClick(Sender: TObject);
        procedure Btn_QuitClick(Sender: TObject);
        private
            { Private declarations }
        public
            { Public declarations }
    end;
     
    var
        Form1: TForm1;
     
    implementation
     
    {$R *.DFM}
     
    //开始:数据发送到 word事件
    procedure TForm1.Btn_PrintToWordClick(Sender: TObject);
    vAR
        VarWord: Variant;// 创建 WORD时所用
    begin
        try
            // 1. 建立 OleObject,连接 word97
            VarWord:=CreateOleObject('word.basic');
            // 2. 建立 Word97的新文件
            VarWord.FileNew;
            // 3. 设置 Word97的基本状态
            VarWord.ViewZoom75; //设置显示比例为 75%
            VarWord.ViewPage; //改为页面显示方式
            // 4. 将当前数据控件上的信息发送至 Word97
            // 4.1 发送文号数据
            VarWord.CenterPara; //居中
            Varword.font('宋体 '); //设置字体
            VarWord.FontSize(14); //设置字号
            varword.insert(#13+#13+ ED_WenHao.Text+#13+#13+#13);
            // 4.2 发送标题数据
            VarWord.font('黑体 ');
            VarWord.Fontsize(16);
            VarWord.insert( ED_BiaoTi.text+#13);
            // 4.3 发送收文单位数据
            VarWord.LeftPara; //左对齐
            VarWord.Font('宋体 ');
            VarWord.fontSize(14);
            VarWord.Insert(#13+ ED_ShouWenDanWei.Text+': '+#13);
            // 4.5 发送正文数据
            VarWord.fontSize(14);
            VarWord.Insert( ED_ZhenWen.Text+#13);
           // 4.6 发送发文单位数据
            VarWord.RightPara; //右对齐
            VarWord.fontSize(14);
            VarWord.Insert( ED_FaWenDanWei.Text+#13);
            // 5 最后设置
            VarWord.StartOfdocument; //到文首
            VarWord.AppMaxiMize; //设置窗口最大化
            VarWord.AppShow; //显示应用程序
        except
            showmessage('运行 Microsoft Word 失败! ');
        end; //end of try
    end;
    //end:数据发送到 word事件
     
     
    //开始:窗口关闭事件
    procedure TForm1.Btn_QuitClick(Sender: TObject);
    begin
            close;
    end;
    //End:窗口关闭事件
     
    end.
    // 这是主程序的尾部
     
      

  7.   

    Delphi自带的有例子
    C:\Program Files\Borland\Delphi5\Demos\Activex
      

  8.   

    To: yq3woaini(哈哈镜(初级代码工)(★★★)) 
      你说的这些我都明白,我现在想知道的是找一个什么控件能够嵌在form中,感觉上和在word中对文字、表格等的处理一样。我想问这个空间是什么,怎么用?