给点代码吧!

解决方案 »

  1.   

    1、如果知道WinWord的安装为止,那么使用WinExec函数!!!
    2、如果不知道WinWord的安装为止,那么使用ShellExecute函数!!!具体请参考MSDN!!!
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Word2000, Office2000, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        procedure AppClose(Sender:TObject);
      public
        WordApp: TWordApplication;
        WordDoc: TWordDocument;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.AppClose(Sender: TObject);
    begin
      Form1.Close;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      Template,NewTemplate,DocumentType,Visible: OleVariant;
      OldCursor: TCursor;  FileName,FileFormat,LockComments,Password,
      AddToRecentFiles,WritePassword,ReadOnlyRecommended,
      EmbedTrueTypeFonts,SaveNativePictureFormat,
      SaveFormsData,SaveAsAOCELetter: OleVariant;begin
      OldCursor:=Screen.Cursor;
      Screen.Cursor:=crHourglass;  WordApp:=TWordApplication.Create(Application);
      WordApp.Visible:=True;
      WordApp.OnQuit:=AppClose;
      WordApp.Caption:='Delphi Control Word';  Template:='';
      NewTemplate:=False;
      DocumentType:=wdNewBlankDocument;
      Visible:=True;
      if WordApp.Documents.Count=0 then
        WordApp.Documents.Add(Template,NewTemplate,DocumentType,Visible);  WordDoc:=TWordDocument(WordApp.ActiveDocument);
      WordApp.Activate;  
      FileName:='c:\Sample.doc';
      FileFormat:=wdFormatDocument;
      LockComments:=False;
      Password:='';
      AddToRecentFiles:=True;
      WritePassword:='';
      ReadOnlyRecommended:=false;
      EmbedTrueTypeFonts:=False;
      SaveNativePictureFormat:=False;
      SaveFormsData:=False;
      SaveAsAOCELetter:=false;
      WordApp.ActiveDocument.SaveAs(FileName,
                                    FileFormat,
                                    LockComments,
                                    Password,
                                    AddToRecentFiles,
                                    WritePassword,
                                    ReadOnlyRecommended,
                                    EmbedTrueTypeFonts,
                                    SaveNativePictureFormat,
                                    SaveFormsData,
                                    SaveAsAOCELetter);  WordApp.ActiveDocument.Save;
      WordApp.ActiveDocument.Saved:=True;  Screen.Cursor:=OldCursor;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
      SaveChanges, OriginalFormat, RouteDocument: OleVariant;
    begin
      SaveChanges:=wdPromptToSaveChanges;
      OriginalFormat:=wdWordDocument;
      RouteDocument:=False;
      if Assigned(WordApp) then
      try
        WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
        WordApp.Free;
      except
      end;
    end;end.
      

  3.   

    procedure TForm1.Button1Click(Sender: Tobject);
    var MSWord: Variant;
    begin
    MSWord := CreateOLEObject('Word.Application');//连接Word
    MSWord.Documents.Open(FileName:='d:\test.doc', ReadOnly:=True);//打开外部Word文档
    MSWord.Visible := 1;//是否显示文件编辑
    end;
      

  4.   

    CreateOLEObject('Word.Application');//连接Word
    系统提示还没有定义!怎么引用它啊?
      

  5.   

    CreateOLEObject('Word.Application');//连接Word
    系统提示还没有定义!怎么引用它啊?
      

  6.   

    CreateOLEObject('Word.Application');//连接Word
    系统提示还没有定义!怎么引用它啊?
      

  7.   

    增加引用声明:
    在这个位置增加:
    implementation
     uses comobj;
      

  8.   

    给你点代码,这是我前几天在公司作的,希望与大家共勉。
    启动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用如下代码。如果想保存Doc文件,请修改SaveChanges变量的内容: 
    var 
    SaveChanges, OriginalFormat, RouteDocument: OleVariant; 
    begin 
    SaveChanges := WdDoNotSaveChanges; 
    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; 让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替换标记字符串要使用WordDocument.Range.Find.Execute,这里用Delphi替换了<#Name>: 
    var 
    FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, 
    MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant; 
    begin 
    FindText := '<#Name>'; 
    MatchCase := False; 
    MatchWholeWord := True; 
    MatchWildcards := False; 
    MatchSoundsLike := False; 
    MatchAllWordForms := False; 
    Forward := True; 
    Wrap := wdFindContinue; 
    Format := False; 
    ReplaceWith := 'Delphi'; 
    Replace := True; WordDocument.Range.Find.Execute( FindText, MatchCase, MatchWholeWord, 
    MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, 
    Wrap, Format, ReplaceWith, Replace );