如何实现Delphi OLE自动化中Word文档间文本复制,无需使用剪贴板?
- 内容介绍
- 文章标签
- 相关推荐
本文共计922个文字,预计阅读时间需要4分钟。
在Delphi XE进行Word自动化时,若同时打开了两个文档,并想将一个文档的指定内容复制到另一个文档的特定范围,可以采用以下步骤实现:
delphiprocedure TForm1.ManipulateDocuments;var Doc1, Doc2: TWord.Document; Range1, Range2: TWord.Range; TextToCopy: string;begin // 打开第一个文档 Doc1 :=TWord.Document.Create; try Doc1.LoadFromFile('C:\Path\To\FirstDocument.docx');
// 打开第二个文档 Doc2 :=TWord.Document.Create; try Doc2.LoadFromFile('C:\Path\To\SecondDocument.docx');
// 设置要复制的文本 TextToCopy :='这是要复制的文本';
// 设置第一个文档的复制范围 Range1 :=Doc1.Content; Range1.Find.Execute(FindText:=TextToCopy, Replace:=False, Forward:=True, Format:=False, MatchCase:=False, MatchWholeWord:=False, MatchWildcards:=False, MatchSoundsLike:=False, MatchAllWordForms:=False);
// 如果找到了文本,则复制到第二个文档的指定范围 if Range1.Find.Found then begin Range2 :=Doc2.Content; Range2.Collapse(wdCollapseStart); Range2.InsertBefore(TextToCopy); end;
finally Doc2.Free; end; finally Doc1.Free; end;end;
这段代码首先创建了两个`TWord.Document`对象,分别加载两个Word文档。然后,设置要复制的文本内容,并找到第一个文档中该文本的位置。如果找到了,就将该文本插入到第二个文档的开始位置。注意替换路径为你的实际文档路径。
在从Delphi XE进行Word自动化时,我同时打开了两个文档.我想将给定范围的一个文档的内容复制到另一个文档中的另一个范围.我怎样才能做到这一点?请考虑以下代码:
procedure TForm1.ManipulateDocuments; var vDoc1,vDoc2 : TWordDocument; vFilename : olevariant; vRange1,vRange2 : Range; begin vDoc1 := TWordDocument.Create(nil); vDoc2 := TWordDocument.Create(nil); try vFilename := 'c:\temp\test1.doc'; vDoc1.ConnectTo(FWordApp.Documents.Open(vFilename,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam)); vFilename := 'c:\temp\test2.doc'; vDoc2.ConnectTo(FWordApp.Documents.Open(vFilename,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam)); vRange1 := GetSourceRange(vDoc1); vRange2 := GetDestinationRange(vDoc2); vRange2.CONTENTS := vRange1.CONTENTS; //What should I substitute for CONTENTS? finally vDoc1.Free; vDoc2.Free; end; end;
有什么东西我可以替代内容吗?我不能使用文本,因为我想复制格式,书签,字段代码等.我是否必须以另一种方式完成它?有什么建议?
我不知道早期版本的Word的方法,但对于较新的版本(2007及更高版本),您可以从文档到片段文件 export a range,然后从另一个文档获取 import.如果你想要早期绑定,你可能需要导入类型库(msword.olb),我不知道Delphi XE是否有它.否则代码可能如下所示:function GetTempFileName(Prefix: string): string; begin SetLength(Result, MAX_PATH); GetTempPath(MAX_PATH, PChar(Result)); windows.GetTempFileName(PChar(Result), PChar(Prefix), 0, PChar(Result)); end; procedure TForm2.Button1Click(Sender: TObject); const // wdFormatDocument = 0; wdFormatRTF = $00000006; var WordApp : OleVariant; fragment: string; vDoc1, vDoc2: OleVariant; vRange1, vRange2: OleVariant; begin try WordApp := GetActiveOleObject('Word.Application'); except WordApp := CreateOleObject('Word.Application'); end; WordApp.Visible := True; vDoc1 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test1.doc'); vRange1 := vDoc1.Range(20, 120); // the export range fragment := GetTempFileName('frg'); vRange1.ExportFragment(fragment, wdFormatRTF); try vDoc2 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test2.doc'); vRange2 := vDoc2.Range(15, 15); // where to import vRange2.ImportFragment(fragment); finally DeleteFile(fragment); end; end;
通过我的测试,“文档”格式引发了一个错误(类似于无法插入XML格式),因此使用了RTF格式.
编辑:
对于早期版本,似乎可以将一个文档中的命名选择插入另一个文档中的选择.如果其中一个选择恰好位于某些文本的中间,那么结果似乎并不完美.但除此之外似乎工作得很好.
... WordApp.Visible := True; vDoc1 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test1.doc'); vRange1 := vDoc1.Range(20, 188); // the transfer range vDoc1.Bookmarks.Add('TransferSection', vRange1); // arbitrary bookmark name vDoc2 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test2.doc'); vRange2 := vDoc2.Range(103, 104); // where to import the bookmark vRange2.Select; vDoc2.ActiveWindow.Selection.InsertFile(vDoc1.FullName, 'TransferSection'); vDoc1.Bookmarks.Item('TransferSection').Delete; // no need for the bookmark anymore
本文共计922个文字,预计阅读时间需要4分钟。
在Delphi XE进行Word自动化时,若同时打开了两个文档,并想将一个文档的指定内容复制到另一个文档的特定范围,可以采用以下步骤实现:
delphiprocedure TForm1.ManipulateDocuments;var Doc1, Doc2: TWord.Document; Range1, Range2: TWord.Range; TextToCopy: string;begin // 打开第一个文档 Doc1 :=TWord.Document.Create; try Doc1.LoadFromFile('C:\Path\To\FirstDocument.docx');
// 打开第二个文档 Doc2 :=TWord.Document.Create; try Doc2.LoadFromFile('C:\Path\To\SecondDocument.docx');
// 设置要复制的文本 TextToCopy :='这是要复制的文本';
// 设置第一个文档的复制范围 Range1 :=Doc1.Content; Range1.Find.Execute(FindText:=TextToCopy, Replace:=False, Forward:=True, Format:=False, MatchCase:=False, MatchWholeWord:=False, MatchWildcards:=False, MatchSoundsLike:=False, MatchAllWordForms:=False);
// 如果找到了文本,则复制到第二个文档的指定范围 if Range1.Find.Found then begin Range2 :=Doc2.Content; Range2.Collapse(wdCollapseStart); Range2.InsertBefore(TextToCopy); end;
finally Doc2.Free; end; finally Doc1.Free; end;end;
这段代码首先创建了两个`TWord.Document`对象,分别加载两个Word文档。然后,设置要复制的文本内容,并找到第一个文档中该文本的位置。如果找到了,就将该文本插入到第二个文档的开始位置。注意替换路径为你的实际文档路径。
在从Delphi XE进行Word自动化时,我同时打开了两个文档.我想将给定范围的一个文档的内容复制到另一个文档中的另一个范围.我怎样才能做到这一点?请考虑以下代码:
procedure TForm1.ManipulateDocuments; var vDoc1,vDoc2 : TWordDocument; vFilename : olevariant; vRange1,vRange2 : Range; begin vDoc1 := TWordDocument.Create(nil); vDoc2 := TWordDocument.Create(nil); try vFilename := 'c:\temp\test1.doc'; vDoc1.ConnectTo(FWordApp.Documents.Open(vFilename,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam)); vFilename := 'c:\temp\test2.doc'; vDoc2.ConnectTo(FWordApp.Documents.Open(vFilename,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam)); vRange1 := GetSourceRange(vDoc1); vRange2 := GetDestinationRange(vDoc2); vRange2.CONTENTS := vRange1.CONTENTS; //What should I substitute for CONTENTS? finally vDoc1.Free; vDoc2.Free; end; end;
有什么东西我可以替代内容吗?我不能使用文本,因为我想复制格式,书签,字段代码等.我是否必须以另一种方式完成它?有什么建议?
我不知道早期版本的Word的方法,但对于较新的版本(2007及更高版本),您可以从文档到片段文件 export a range,然后从另一个文档获取 import.如果你想要早期绑定,你可能需要导入类型库(msword.olb),我不知道Delphi XE是否有它.否则代码可能如下所示:function GetTempFileName(Prefix: string): string; begin SetLength(Result, MAX_PATH); GetTempPath(MAX_PATH, PChar(Result)); windows.GetTempFileName(PChar(Result), PChar(Prefix), 0, PChar(Result)); end; procedure TForm2.Button1Click(Sender: TObject); const // wdFormatDocument = 0; wdFormatRTF = $00000006; var WordApp : OleVariant; fragment: string; vDoc1, vDoc2: OleVariant; vRange1, vRange2: OleVariant; begin try WordApp := GetActiveOleObject('Word.Application'); except WordApp := CreateOleObject('Word.Application'); end; WordApp.Visible := True; vDoc1 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test1.doc'); vRange1 := vDoc1.Range(20, 120); // the export range fragment := GetTempFileName('frg'); vRange1.ExportFragment(fragment, wdFormatRTF); try vDoc2 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test2.doc'); vRange2 := vDoc2.Range(15, 15); // where to import vRange2.ImportFragment(fragment); finally DeleteFile(fragment); end; end;
通过我的测试,“文档”格式引发了一个错误(类似于无法插入XML格式),因此使用了RTF格式.
编辑:
对于早期版本,似乎可以将一个文档中的命名选择插入另一个文档中的选择.如果其中一个选择恰好位于某些文本的中间,那么结果似乎并不完美.但除此之外似乎工作得很好.
... WordApp.Visible := True; vDoc1 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test1.doc'); vRange1 := vDoc1.Range(20, 188); // the transfer range vDoc1.Bookmarks.Add('TransferSection', vRange1); // arbitrary bookmark name vDoc2 := WordApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'test2.doc'); vRange2 := vDoc2.Range(103, 104); // where to import the bookmark vRange2.Select; vDoc2.ActiveWindow.Selection.InsertFile(vDoc1.FullName, 'TransferSection'); vDoc1.Bookmarks.Item('TransferSection').Delete; // no need for the bookmark anymore

