How to bypass the 255 char limitation in MSWord Se

2019-08-04 01:57发布

问题:

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.

Something like

Dear {NAME},

I want to tell you {WHAT_I_DID_LAST_WEEK}

Of course NAME and WHAT_I_DID_LAST_WEEK are DB fields that can be longer than 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) truncate to 255 (good one ;) ) may be appending "..." at the end

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) is a quick solution, at least user doesn't recieve the error, but of course it is not very good

(2) would probably work but it is a workaround, I would prefer another solution.

May be another solution is not use OLE Serach&Replace but use another function.

回答1:

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;

regards