如何调用系统的word程序?如:怎么才能打开一个word文件?
要用到WordDocument或者WordApplication组件?如果用,怎么用??

解决方案 »

  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用如下代码。如果想保存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 );
    rongercao (2001-6-8 2:25:00)
    uses comobj;
    var msword:variant;
    msword:=createobject('word.application');
    msword.documents.open(要打开的文件);
    msword.visible:=1;
    如果要断开word与服务器的连接
    msword:=unassigned