我正在使用OLE Search Replace将“占位符标记”替换为存储在db字段中的内容到Word docuemnt中。我使用类似于讨论here的技术。
这是有效的,但当然不适用于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上搜索我找到了a trick that uses the clipboard。注意:我不使用书签,这个例子使用书签,我只是将我的标签定义为纯文本,如''当我在搜索和替换循环中找到'时,我替换文本。
更新:你看到这个剪贴板技巧有任何问题吗?
您有其他想法并可以提出其他解决方案吗?
答案 0 :(得分:0)
我建议改用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;
答案 1 :(得分:0)
这是我多年前保存的帖子。它由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;