在DELPHI7中如何使用WORD相关元件来替换文字及插入图片等问题 |
尚未結案
|
dynamic_zhou
一般會員 發表:1 回覆:2 積分:0 註冊:2004-07-21 發送簡訊給我 |
|
dynamic_zhou
一般會員 發表:1 回覆:2 積分:0 註冊:2004-07-21 發送簡訊給我 |
以下是小弟写的程式码,我把问题也列出来,请各位大大指教,谢谢! 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 發送簡訊給我 |
经过几天的测试,加上朋友的帮助,我终于把问题找到了,问题还是出在参数上,看似不起眼的参数却在其中起着至关重要的作用哦,请留意下面代码的红色说明部分。 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 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |