我正在使用 OLE 搜索替换将"占位符标签"替换为存储在 Word 文档中的数据库字段中的内容。我使用了一种类似于这里讨论的技术。
这是有效的,但当然不适用于 rtf 字段。我有包含 rtf 数据的数据库字段,如果搜索替换,我将获得完整的 rtf 代码,所以而不是看到
你好世界
我看到类似的东西
{\rtf1\ansi\ansicpg1252\deff0\deflang1040 \viewkind4\uc1\pard\sa200\sl276\slmult1\lang16\b\f0\fs22 Hello \i 世界\b0\i0\par }
有人已经解决了这个问题吗?在StackOverflow上搜索时,我发现了一个使用剪贴板的技巧。注意:我不使用书签,此示例使用书签,我只是将标签定义为纯文本,例如",当我在搜索和替换循环中找到"时,我会替换文本。
更新:你在这个剪贴板技巧中看到任何问题吗?
您有其他想法并可以提出其他解决方案吗?
我建议改用Selection.InsertFile。这里有一个例子,它应该做你想做的事,它找到"占位符"并插入一个 rtf 文件。之前将 rtf 保存到临时文件...
procedure TForm1.Button1Click(Sender: TObject);
var
Fword,FDocument,FFindObject:OleVariant;
Filename:String;
begin
Filename := 'C:temptest.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:temptest.rtf')
end;
这是我很久以前保存的一篇文章。它是由TeamB的Peter Below博士发布到旧的Borland Delphi新闻组,但它今天仍然适用。它演示如何使用EM_STREAMIN
和EM_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;