線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2352
推到 Plurk!
推到 Facebook!

在DELPHI7中如何使用WORD相关元件来替换文字及插入图片等问题

尚未結案
dynamic_zhou
一般會員


發表:1
回覆:2
積分:0
註冊:2004-07-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-02 16:12:59 IP:218.80.xxx.xxx 未訂閱
请教各位大大: 小弟刚开始学用DELPHI转出WORD文档,系统平台及软件环境为WINXP DELPHI7 OFFICE2003,操作WORD的主要目的是:一. 替换固定文档中的字符;二. 在有书签的位置插入图片,图片从文件获得;小弟在这边也查找过一些相关资料,略有帮助,但还是不得要领,请各位大大们不吝赐教,小弟在此谢过。 發表人 - dynamic_zhou 於 2004/08/02 16:55:55
dynamic_zhou
一般會員


發表:1
回覆:2
積分:0
註冊:2004-07-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-02 17:13:19 IP:218.80.xxx.xxx 未訂閱
以下是小弟写的程式码,我把问题也列出来,请各位大大指教,谢谢!    procedure TForm_grade_change.Button1Click(Sender: TObject);
var
  vWhat,vWhich,vCount,vName :OleVariant;      vPictureName,vLinkToFile,vSaveWithDocument,vRange :OleVariant;      vFileName,vConfirmConversions,vReadOnly,vAddToRecentFiles,vPasswordDocument,vIndex,
  vPasswordTemplate,vRevert,vWritePasswordDocument,vWritePasswordTemplate,vFormat,
  vEncoding,vVisible,vOpenAndRepair,vDocumentDirection,vNoEncodingDialog :OleVariant;      vFindText,vMatchCase,vMatchWholeWord,vMatchWildCards,vMatchSoundsLike,
  vMatchAllWordForms,vForward,vWrap,vReplaceWith,
  vReplace,vMatchKashida,vMatchDiacritics,vMatchAlefHamza,vMatchControl :OleVariant;
begin
  //===>>> 开启指定文档 <<<===//      vFileName := 'D:\TEST.doc';
  vConfirmConversions := False;
  vReadOnly := False;
  vAddToRecentFiles := False;
  vPasswordDocument := EmptyParam;
  vPasswordTemplate := EmptyParam;
  vRevert := False;
  vWritePasswordDocument := EmptyParam;
  vWritePasswordTemplate := EmptyParam;
  vFormat := EmptyParam;
  vEncoding := EmptyParam;
  vVisible := False;
  vOpenAndRepair := True;
  vDocumentDirection := EmptyParam;
  vNoEncodingDialog := True;      vIndex := 1;      try
    WordApplication.Documents.Open(vFileName, vConfirmConversions,
                                   vReadOnly, vAddToRecentFiles,
                                   vPasswordDocument, vPasswordTemplate,
                                   vRevert, vWritePasswordDocument,
                                   vWritePasswordTemplate, vFormat,
                                   vEncoding, vVisible, vOpenAndRepair,
                                   vDocumentDirection, vNoEncodingDialog);        WordDocument.ConnectTo(WordApplication.Documents.Item(vIndex));
  except
    WordDocument.Disconnect;        Application.MessageBox('无法打开文档!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      //===>>> 替换指定字符 <<<===//      vFindText := 'TEST';
  vMatchCase := EmptyParam;
  vMatchWholeWord := EmptyParam;
  vMatchWildCards := EmptyParam;
  vMatchSoundsLike := EmptyParam;
  vMatchAllWordForms := EmptyParam;
  vForward := EmptyParam;
  vWrap := wdFindContinue;
  vFormat := EmptyParam;
  vReplaceWith := '替换';
  vReplace := wdReplaceAll;
  vMatchKashida := EmptyParam;
  vMatchDiacritics := EmptyParam;
  vMatchAlefHamza := EmptyParam;
  vMatchControl := EmptyParam;      try
    WordDocument.Range.Find.Execute(vFindText, vMatchCase,vMatchWholeWord, vMatchWildCards,vMatchSoundsLike,vMatchAllWordForms,vForward, vWrap,vFormat,vReplaceWith,
vReplace,vMatchKashida,vMatchDiacritics,vMatchAlefHamza,vMatchControl);
  except
    WordDocument.Disconnect;        Application.MessageBox('无法替换字符!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      //===>>> 在指定书签处插入指定图片文件 <<<===//      vWhat  := wdGoToBookmark;
  vWhich := EmptyParam;
  vCount := EmptyParam;
  vName  := '图片';      vPictureName := 'D:\TEST.jpg';
  vLinkToFile  := False;
  vSaveWithDocument := True;      try
    WordDocument.Range.GoTo_(vWhat,vWhich,vCount,vName);        vRange := WordDocument.Range;        //===>>> DEBUG到下段代码出现“服务器出现意外情况” <<<===//        WordDocument.Range.InlineShapes.AddPicture(vPictureName,vLinkToFile,vSaveWithDocument,vRange);
  except
    WordDocument.Disconnect;        Application.MessageBox('无法插入图片!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      WordApplication.Visible := True;
end;
發表人 - dynamic_zhou 於 2004/08/02 17:46:51
dynamic_zhou
一般會員


發表:1
回覆:2
積分:0
註冊:2004-07-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-08-06 13:22:29 IP:218.80.xxx.xxx 未訂閱
    经过几天的测试,加上朋友的帮助,我终于把问题找到了,问题还是出在参数上,看似不起眼的参数却在其中起着至关重要的作用哦,请留意下面代码的红色说明部分。    procedure TForm_grade_change.Button1Click(Sender: TObject);
var
  vWhat,vWhich,vCount,vName :OleVariant;      vPictureName,vLinkToFile,vSaveWithDocument,vRange :OleVariant;      vFileName,vConfirmConversions,vReadOnly,vAddToRecentFiles,vPasswordDocument,
  vPasswordTemplate,vRevert,vWritePasswordDocument,vWritePasswordTemplate,vFormat,
  vEncoding,vVisible,vOpenAndRepair,vDocumentDirection,vNoEncodingDialog :OleVariant;      vFindText,vMatchCase,vMatchWholeWord,vMatchWildCards,vMatchSoundsLike,
  vMatchAllWordForms,vForward,vWrap,vReplaceWith,
  vReplace,vMatchKashida,vMatchDiacritics,vMatchAlefHamza,vMatchControl :OleVariant;      vIndex,vDoNotSaveChanges :OleVariant;
begin
  //===>>> 开启指定文档 <<<===//      vFileName := 'D:\TEST.doc';
  vConfirmConversions := False;
  vReadOnly := False;
  vAddToRecentFiles := False;
  vPasswordDocument := EmptyParam;
  vPasswordTemplate := EmptyParam;
  vRevert := False;
  vWritePasswordDocument := EmptyParam;
  vWritePasswordTemplate := EmptyParam;
  vFormat := EmptyParam;
  vEncoding := EmptyParam;
  vVisible := True; //===>>> 一定要设成True,不然后续动作都无法实现 <<<===//
  vOpenAndRepair := False; //===>>> 设成False就不会出现文件修复对话框 <<<===//
  vDocumentDirection := EmptyParam;
  vNoEncodingDialog := True;      vIndex := vFileName; //===>>> 按文档名称来定位要修改的文档 <<<===//      vDoNotSaveChanges := wdDoNotSaveChanges; //===>>> 关闭文档时不出现提示保存对话框 <<<===//      try
    WordApplication.Documents.Open(vFileName, vConfirmConversions,
                                   vReadOnly, vAddToRecentFiles,
                                   vPasswordDocument, vPasswordTemplate,
                                   vRevert, vWritePasswordDocument,
                                   vWritePasswordTemplate, vFormat,
                                   vEncoding, vVisible, vOpenAndRepair,
                                   vDocumentDirection, vNoEncodingDialog);        WordDocument.ConnectTo(WordApplication.Documents.Item(vIndex));
  except
    WordDocument.Close(vDoNotSaveChanges);        Application.MessageBox('无法打开文档!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      //===>>> 替换指定字符 <<<===//      vFindText := 'TEST';
  vMatchCase := EmptyParam;
  vMatchWholeWord := EmptyParam;
  vMatchWildCards := EmptyParam;
  vMatchSoundsLike := EmptyParam;
  vMatchAllWordForms := EmptyParam;
  vForward := EmptyParam;
  vWrap := wdFindContinue;
  vFormat := EmptyParam;
  vReplaceWith := '替换';
  vReplace := wdReplaceAll;
  vMatchKashida := EmptyParam;
  vMatchDiacritics := EmptyParam;
  vMatchAlefHamza := EmptyParam;
  vMatchControl := EmptyParam;      try
    WordDocument.Range.Find.Execute(vFindText, vMatchCase,vMatchWholeWord, vMatchWildCards,vMatchSoundsLike,vMatchAllWordForms,vForward, vWrap,vFormat,vReplaceWith,
vReplace,vMatchKashida,vMatchDiacritics,vMatchAlefHamza,vMatchControl);
  except
    WordDocument.Close(vDoNotSaveChanges);        Application.MessageBox('无法替换字符!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      //===>>> 在指定书签处插入指定图片文件 <<<===//      vWhat  := wdGoToBookmark;
  vWhich := EmptyParam;
  vCount := EmptyParam;
  vName  := '图片';      vPictureName := 'D:\TEST.jpg';
  vLinkToFile  := False;
  vSaveWithDocument := True;      try
    WordApplication.Selection.GoTo_(vWhat,vWhich,vCount,vName);
    vRange := WordApplication.Selection.Range;
    WordApplication.Selection.InlineShapes.AddPicture(vPictureName,vLinkToFile,vSaveWithDocument,vRange);
  except
    WordDocument.Close(vDoNotSaveChanges);        Application.MessageBox('无法插入图片!','警告',MB_OK MB_ICONWARNING MB_APPLMODAL);        exit;
  end;      WordApplication.Visible := True;      WordDocument.Activate;
end;        此功能还在试用阶段,错误疏漏在所难免,各位大大们若发现问题请多提宝贵意见,谢谢!
發表人 - dynamic_zhou 於 2004/08/06 13:34:33
系統時間:2024-11-22 10:56:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!