如何使用OLE绕过MSWord Search& Replace中的255个字符限制 [英] How to bypass the 255 char limitation in MSWord Search&Replace using OLE
问题描述
我正在使用(在Delphi win32应用程序中)OLE执行搜索并在Word文档中进行替换.
I am using (in a Delphi win32 application) OLE to perform search and replace in Word Documents.
用户准备一个带有一些文本标签的文件,并将其括在"{"和}"中,然后保存该文件.
THe user prepares a file with some textual tags enclosing them in "{" and "}" and saves the file.
类似
尊敬的{NAME},
Dear {NAME},
我想告诉你{WHAT_I_DID_LAST_WEEK}
I want to tell you {WHAT_I_DID_LAST_WEEK}
当然NAME
和WHAT_I_DID_LAST_WEEK
是DB字段,可以长于255.
Of course NAME
and WHAT_I_DID_LAST_WEEK
are DB fields that can be longer than 255.
因此,现在通过使用搜索"并用OLE替换,我得到了STRING PARAMETER TOO LONG错误(似乎255是在那里可用的最长字符串).
So now by using Search and replace with OLE i get a STRING PARAMETER TOO LONG error (it seems 255 is the longest string usable there).
有没有一种简单的方法可以解决这个问题?
Is there an easy way to get rid of the problem?
我想到的一些自制解决方案是:
Some home made solutons I thought of are:
1)截断为255(一个;))可能在末尾附加"..."
1) truncate to 255 (good one ;) ) may be appending "..." at the end
2)对于每个需要替换超过255个字符的标签",我可以先插入更多标签,例如{WHAT_I_DID_LAST_WEEK_1}{WHAT_I_DID_LAST_WEEK_2}{WHAT_I_DID_LAST_WEEK_N}
,然后一次替换255个字符
2) for every "tag" that requires a replace of more than 255 chars I could first insert more tags like {WHAT_I_DID_LAST_WEEK_1}{WHAT_I_DID_LAST_WEEK_2}{WHAT_I_DID_LAST_WEEK_N}
and then replace 255 chars at a time
(1)是一种快速的解决方案,至少用户没有收到该错误,但是当然不是很好
(1) is a quick solution, at least user doesn't recieve the error, but of course it is not very good
(2)可能会起作用,但这是一种解决方法,我希望使用其他解决方案.
(2) would probably work but it is a workaround, I would prefer another solution.
可能另一个解决方案是不使用OLE Serach& Replace,而是使用另一个功能.
May be another solution is not use OLE Serach&Replace but use another function.
推荐答案
我们使用AWordApp.Selection.TypeText(strValue)并循环替换具有大于255个字符的值字符串的标记...
we use AWordApp.Selection.TypeText(strValue) and loop for replacing tags that have value string longer then 255 chars ...
var
AWordApp: OLEVariant;
...
AWordApp := CreateOleObject('Word.Application');
...
if (Length(strValue) > 255) then
begin
bFound := AWordApp.Selection.Find.Execute(params...);
while bFound do
begin
AWordApp.Selection.TypeText(strValue);
bFound := AWordApp.Selection.Find.Execute(params...);
end;
end;
致谢
这篇关于如何使用OLE绕过MSWord Search& Replace中的255个字符限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!