加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – OLE自动化:如何在不使用剪贴板的情况下在Word文档之

发布时间:2020-12-15 09:11:42 所属栏目:大数据 来源:网络整理
导读:在从Delphi XE进行Word自动化时,我同时打开了两个文档.我想将给定范围的一个文档的内容复制到另一个文档中的另一个范围.我怎样才能做到这一点? 请考虑以下代码: procedure TForm1.ManipulateDocuments;var vDoc1,vDoc2 : TWordDocument; vFilename : oleva
在从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:temptest1.doc';
    vDoc1.ConnectTo(FWordApp.Documents.Open(vFilename,EmptyParam,EmptyParam));

    vFilename := 'c:temptest2.doc';
    vDoc2.ConnectTo(FWordApp.Documents.Open(vFilename,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),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
?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读