在word文档中插入rtf文本

发布于 2024-12-16 01:28:34 字数 757 浏览 3 评论 0原文

我正在使用 OLE 搜索替换将“占位符标签”替换为存储在数据库字段中的内容到 Word 文档中。我使用的技术类似于此处讨论的技术。

这是可行的,但当然它不适用于 rtf 字段。我有包含 rtf 数据的数据库字段,如果执行搜索替换,我将获得完整的 rtf 代码,因此我看到的不是

Hello World

类似的内容

{\rtf1\ansi\ansicpg1252\deff0\deflang1040 \viewkind4\uc1\pard\sa200\sl276\slmult1\lang16\b\f0\fs22 你好\i 世界\b0\i0\par }

有人已经解决了这个问题吗?在 StackOverflow 上搜索时,我发现一个使用剪贴板的技巧。注意:我不使用书签,此示例使用书签,我只是将标签定义为纯文本,如“”,当我在搜索和替换循环中找到“”时,我将替换文本。

更新:你看到这个剪贴板技巧有什么问题吗?

您还有其他想法并可以提出其他解决方案吗?

I am using OLE Search Replace to replace "placeholder tags" with content stored in db fields into a Word docuemnt. I use a technique similar to what disussed here.

THis works but of course it doesn't for rtf fields. I have db fields containing rtf data and if a do search replace I will get the full rtf code, so instead of seeing

Hello World

I see something like

{\rtf1\ansi\ansicpg1252\deff0\deflang1040
\viewkind4\uc1\pard\sa200\sl276\slmult1\lang16\b\f0\fs22 Hello \i
World\b0\i0\par }

Did anyone already solved the problem? Searching on StackOverflow I found a trick that uses the clipboard. Note: I don't use bookmarks, this example uses bookmarks, I simply have my tags defined as plain text like '' and when I find '' in my search and replace loop I replace the text.

UPDATE: Do you see any prolem in this clipboard trick?

Do you have other ideas and can suggest other solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-12-23 01:28:34

我建议改用 Selection.InsertFile 。这是一个应该执行您想要的操作的示例,它找到“占位符”并插入一个 rtf 文件。之前将您的 rtf 保存到临时文件...

 procedure TForm1.Button1Click(Sender: TObject);
var
  Fword,FDocument,FFindObject:OleVariant;
  Filename:String;

begin
   Filename := 'C:\temp\test.doc';
   Fword := CreateOleObject('Word.Application');
   FDocument := Fword.Documents.Add(Filename);
   FFindObject := FDocument.ActiveWindow.Selection.Find;
   Fword.visible := true;
   FFindObject.ClearFormatting;
   FFindObject.Replacement.ClearFormatting;
   FFindObject.Text := 'placeholder';
   FFindObject.Forward := True;
   FFindObject.Replacement.Text := '';
   FFindObject.Wrap := 1;
   FFindObject.MatchCase := False;
   FFindObject.MatchWholeWord := False;
   FFindObject.MatchWildcards := False;
   FFindObject.MatchSoundsLike := False;
   FFindObject.MatchAllWordForms := False;

   if FFindObject.Execute() then Fword.selection.InsertFile('C:\temp\test.rtf')   
end; 

I would suggest to use the Selection.InsertFile instead. Here is an example that shoud do what you want, it finds "placeholder" and inserts a rtf file. Save your rtf to a temp-file before...

 procedure TForm1.Button1Click(Sender: TObject);
var
  Fword,FDocument,FFindObject:OleVariant;
  Filename:String;

begin
   Filename := 'C:\temp\test.doc';
   Fword := CreateOleObject('Word.Application');
   FDocument := Fword.Documents.Add(Filename);
   FFindObject := FDocument.ActiveWindow.Selection.Find;
   Fword.visible := true;
   FFindObject.ClearFormatting;
   FFindObject.Replacement.ClearFormatting;
   FFindObject.Text := 'placeholder';
   FFindObject.Forward := True;
   FFindObject.Replacement.Text := '';
   FFindObject.Wrap := 1;
   FFindObject.MatchCase := False;
   FFindObject.MatchWholeWord := False;
   FFindObject.MatchWildcards := False;
   FFindObject.MatchSoundsLike := False;
   FFindObject.MatchAllWordForms := False;

   if FFindObject.Execute() then Fword.selection.InsertFile('C:\temp\test.rtf')   
end; 
深海里的那抹蓝 2024-12-23 01:28:34

这是我很久以前保存的帖子。它由 TeamB 的 Peter Below 博士发布到旧的 Borland Delphi 新闻组,但今天仍然适用。它演示了如何使用 EM_STREAMINEM_STREAMOUT 消息以及相关回调将 RTF 文本放入 TRichEdit 中以及从中复制出来。

Uses RichEdit;

Type
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): DWORD; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
 cb: Longint; var pcb: Longint): DWORD; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do begin
    dataAvail := Size - Position;
    Result := 0; {assume everything is ok}
    if dataAvail <= cb then begin 
      pcb := Read(pbBuff^, dataAvail); 
      if pcb <> dataAvail then  //couldn't read req. amount of bytes
        result := E_FAIL; 
    end 
    else begin
      pcb := Read(pbBuff^, cb); 
      if pcb <> cb then 
        result := E_FAIL; 
    end;
  end;
end;


Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): DWORD; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then 
       pcb := Write(pbBuff^, cb);
     Result := 0;
   end;
 end;

Procedure GetRTFSelection( aRichEdit: TRichEdit; intoStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(intoStream);
    dwError:= 0;
    pfnCallback:= EditStreamOutCallBack;
  end;
  aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@editstream));
End;

Procedure PutRTFSelection( aRichEdit: TRichEdit; sourceStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(sourceStream);
    dwError:= 0;
    pfnCallback:= EditStreamInCallBack;
  end;
  aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION, longint(@editstream));
End;

Here's a post I saved from ages ago. It was posted by Dr. Peter Below of TeamB to the old Borland Delphi newsgroups, but it's still applicable today. It shows how to use the EM_STREAMIN and EM_STREAMOUT messages and related callback to put RTF text into and copy out of a TRichEdit.

Uses RichEdit;

Type
  TEditStreamCallBack = function (dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): DWORD; stdcall;

  TEditStream = record
    dwCookie: Longint;
    dwError: Longint;
    pfnCallback: TEditStreamCallBack;
  end;

function EditStreamInCallback(dwCookie: Longint; pbBuff: PByte;
 cb: Longint; var pcb: Longint): DWORD; Stdcall;
var
  theStream: TStream;
  dataAvail: LongInt;
begin
  theStream := TStream(dwCookie);
  with theStream do begin
    dataAvail := Size - Position;
    Result := 0; {assume everything is ok}
    if dataAvail <= cb then begin 
      pcb := Read(pbBuff^, dataAvail); 
      if pcb <> dataAvail then  //couldn't read req. amount of bytes
        result := E_FAIL; 
    end 
    else begin
      pcb := Read(pbBuff^, cb); 
      if pcb <> cb then 
        result := E_FAIL; 
    end;
  end;
end;


Function EditStreamOutCallback(dwCookie: Longint; pbBuff: PByte;
    cb: Longint; var pcb: Longint): DWORD; stdcall;
 var
   theStream: TStream;
 begin
   theStream := TStream(dwCookie);

   with theStream do begin
     If cb > 0 Then 
       pcb := Write(pbBuff^, cb);
     Result := 0;
   end;
 end;

Procedure GetRTFSelection( aRichEdit: TRichEdit; intoStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(intoStream);
    dwError:= 0;
    pfnCallback:= EditStreamOutCallBack;
  end;
  aRichedit.Perform( EM_STREAMOUT, SF_RTF or SFF_SELECTION, longint(@editstream));
End;

Procedure PutRTFSelection( aRichEdit: TRichEdit; sourceStream: TStream );
Var
  editstream: TEditStream;
Begin
  With editstream Do Begin
    dwCookie:= Longint(sourceStream);
    dwError:= 0;
    pfnCallback:= EditStreamInCallBack;
  end;
  aRichedit.Perform( EM_STREAMIN, SF_RTF or SFF_SELECTION, longint(@editstream));
End;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文