procedure TForm1.startword;
begin
  if (wordapplication.Documents.Count <> 0 ) then
  begin
    messagedlg('请先关闭word!', mterror, [mbok], 0);
    exit;
  end;  try
    wordapplication.connect;
  except
    messagedlg('word 没有正确安装', mterror, [mbok], 0);
    abort;
  end;
  wordapplication.visible := true;
  wordapplication.caption := 'delphi automation';
end;procedure TForm1.CloseWord;
var
  savechanges, originalformat, routedocument: olevariant;
begin
  savechanges := wdSaveChanges;
  originalformat := unassigned;
  routedocument := unassigned;
  try
    wordapplication.quit(savechanges, originalformat, routedocument);
    wordapplication.disconnect;
  except
    on e: exception do
  begin
    showmessage(e.message);
    wordapplication.disconnect;
  end;
  end;
end;

解决方案 »

  1.   

    要么直接用d5.d6自带的控件了,要么自己导入类型库,要么用olecontainer,看你怎么用了,呵呵
      

  2.   

    cszhz(丑小鸭)兄:
    非常感谢你提的思路。希望能得到更详细的指点,如:如何新建文档、增加文字、表格、字体等细节。
    如果能告诉我在何处找到这些接口的详细帮助资料,就更好了。
    谢谢。
      

  3.   

    以下是我所用到的:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, Grids, DBGrids, ADODB, OleServer, Word2000, shellapi;type
      TForm1 = class(TForm)
        Button1: TButton;
        ADOQuery1: TADOQuery;
        DataSource1: TDataSource;
        wordapplication: TWordApplication;
        WordDocument: TWordDocument;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        Button6: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button6Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure StartWord;
        procedure CloseWord;
        procedure OpenDocument;
        procedure SaveDocument;
        procedure RepalceString;
      end;var
      Form1: TForm1;
      a: OleVariant;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var FileName:OleVariant;
    begin
      startword;
    end;procedure TForm1.startword;
    begin
      if (wordapplication.Documents.Count <> 0 ) then
      begin
        messagedlg('请先关闭word!', mterror, [mbok], 0);
        exit;
      end;  try
        wordapplication.connect;
      except
        messagedlg('word 没有正确安装', mterror, [mbok], 0);
        abort;
      end;
      wordapplication.visible := true;
      wordapplication.caption := 'delphi automation';
    end;procedure TForm1.CloseWord;
    var
      savechanges, originalformat, routedocument: olevariant;
    begin
      savechanges := wdSaveChanges;
      originalformat := unassigned;
      routedocument := unassigned;
      try
        wordapplication.quit(savechanges, originalformat, routedocument);
        wordapplication.disconnect;
      except
        on e: exception do
      begin
        showmessage(e.message);
        wordapplication.disconnect;
      end;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      closeword;
    end;procedure TForm1.Button4Click(Sender: TObject);
    begin
      RepalceString;
    end;procedure TForm1.OpenDocument;
    var
      FileName : OleVariant;
    //Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument,
    //PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format,
    //Encoding, Visible)
    begin
      FileName := 'd:\2.doc';  try
        WordApplication.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam,
                                       EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                       EmptyParam, EmptyParam, EmptyParam, EmptyParam);
      except
        showmessage('该文件不存在!')
      end;
    end;procedure TForm1.Button5Click(Sender: TObject);
    begin
      OpenDocument;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
      OpenDir:pchar;
      vunit,count : olevariant;
    begin
    //wdCharacter、wdWord、wdSentence、wdParagraph、wdSection、wdStory、wdCell、wdColumn、wdRow 或 wdTable
      vunit := wdTable;
      count := 1;
    //  wordapplication.Selection.InsertRows(count);
      wordapplication.Selection.Move(vunit, count);
    //  wordapplication.Selection.Text := 'bbb';
    end;procedure TForm1.RepalceString;
    //让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>:
    var
    //一共15个参数
      findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike,
      matchallwordforms, forward1, wrap, format, replacewith, replace,
      MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl : olevariant; //后四个均为可选项,默认false  num : olevariant;
    begin
      findtext := '数据上报操作说明';
      matchcase := false;
      matchwholeword := true;
      matchwildcards := false;
      matchsoundslike := false;
      matchallwordforms := false;
      forward1 := true;
      wrap := wdfindcontinue;
      format := false;
      replacewith := 'delphi';
      replace := true;
      MatchKashida := false;
      MatchDiacritics := false;
      MatchAlefHamza := false;
      MatchControl := false;//  num := 1;
    //  WordApplication.Documents.Item(num);
      worddocument.ConnectTo(WordApplication.ActiveDocument);
      worddocument.range.find.execute( findtext, matchcase, matchwholeword,
                                       matchwildcards, matchsoundslike, matchallwordforms,
                                       forward1, wrap, format, replacewith, replace,
                                       matchKashida, MatchDiacritics,
                                       MatchAlefHamza, MatchControl );
    end;procedure TForm1.SaveDocument;
    var
      FileName, FileFormat, LockComments, Password,
      AddToRecentFiles, WritePassword, ReadOnlyRecommended,
      EmbedTrueTypeFonts, SaveNativePictureFormat,
      SaveFormsData, SaveAsAOCELetter :olevariant;
    begin
      FileName := 'd:\2.doc';
      FileFormat := wdFormatDocument;
      LockComments := false;
      Password := '';
      AddToRecentFiles := false;
      WritePassword := '';
      ReadOnlyRecommended := false;
      EmbedTrueTypeFonts := false;
      SaveNativePictureFormat := false;
      SaveFormsData := false;
      SaveAsAOCELetter := false;  worddocument.SaveAs(FileName, FileFormat, LockComments, Password,
                          AddToRecentFiles, WritePassword, ReadOnlyRecommended,
                          EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData,
                          SaveAsAOCELetter);
    end;procedure TForm1.Button6Click(Sender: TObject);
    begin
      SaveDocument;
    end;end.