 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rok Rejc Guest
|
Posted: Sat Sep 03, 2005 9:48 am Post subject: Replaceing tags in MS Word document |
|
|
I want to write a function which finds some tag in Microsoft Word
document (like <#TAG>) and replaces the tag with a picture (picture must
be inserted at the tag position, and tag must be removed).
I have written this funciton:
for I:=0 to 11 do // all story ranges
begin
sSearchTag:='<#TAG>';
sSearchValue:='c:picture.jpg';
repeat
// Loop through all tags in a range
Range:=MSWord.ActiveDocument.StoryRanges.Item(I);
iPosStart:=Pos(sSearchTag, Range.text) - 1;
if iPosStart >= 0 then
begin
iPosEnd:=iPosStart + Length(sSearchTag);
Range.SetRange(iPosStart, iPosEnd);
if IsImageFile(sSearchValue) then
begin
// Image values
Range.Text:='';
Range.InlineShapes.AddPicture(sSearchValue);
end
else
// Text values
Range.Text:=sSearchValue;
end;
until (iPosStart < 0);
end;
But this function doesn't work because iPosStart isn't right. In
Range.Text there are also some "formating" chars like #$D, #1 ...
Anybody knows how to write function that will work. I have read all msdn
and similar sites but i dont have a clue. Thanks.
|
|
| Back to top |
|
 |
Oliver Townshend Guest
|
Posted: Sun Sep 04, 2005 5:36 am Post subject: Re: Replaceing tags in MS Word document |
|
|
| Quote: | But this function doesn't work because iPosStart isn't right. In
Range.Text there are also some "formating" chars like #$D, #1 ...
Anybody knows how to write function that will work. I have read all msdn
and similar sites but i dont have a clue. Thanks.
|
What about using Word search functionality. You could search in the range
for the #Tag text, and then replace the selection with the picture?
Oliver Townshend
|
|
| Back to top |
|
 |
Rok Rejc Guest
|
Posted: Sun Sep 04, 2005 1:09 pm Post subject: Re: Replaceing tags in MS Word document |
|
|
Yes, i know about the search functionality. But how should i use it?
How can i get start position of tag ?
Range.Find.Execute method returns only boolean (found or not found).
For replacing text i can use this method:
procedure TrrWordReport.ReplaceTagInRange(ARange: Range; ATag, AValue:
WideString);
var
vFind: OleVariant;
vReplaceWith: OleVariant;
vMatchCase: OleVariant;
vMatchWholeWord: OleVariant;
vReplace: OleVariant;
begin
vFind:=ATag;
vReplaceWith:=AValue;
vMatchCase:=True;
vMatchWholeWord:=True;
vReplace:=wdReplaceAll;
ARange.Find.Execute(vFind, vMatchCase, vMatchWholeWord, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,EmptyParam, EmptyParam,
vReplaceWith,
vReplace, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;
Any idea ?
Oliver Townshend wrote:
| Quote: | What about using Word search functionality. You could search in the range
for the #Tag text, and then replace the selection with the picture?
|
|
|
| Back to top |
|
 |
Oliver Townshend Guest
|
Posted: Mon Sep 05, 2005 6:22 am Post subject: Re: Replaceing tags in MS Word document |
|
|
| Quote: | Range.Find.Execute method returns only boolean (found or not found).
|
I thought that after the Range.Find.Execute, Range gets redefined to be the
text you are looking for, so:
Range.Find.Execute
Range.Text:='';
Range.InlineShapes.AddPicture(sSearchValue);
Then you will probably have to redefine your range, or do a FindNext (which
may give you a new range in the same original range that you had - I don't
know).
Oliver
|
|
| Back to top |
|
 |
Rok Rejc Guest
|
Posted: Mon Sep 05, 2005 5:17 pm Post subject: Re: Replaceing tags in MS Word document |
|
|
Yeah its working
Thanks.
Oliver Townshend wrote:
| Quote: | Range.Find.Execute method returns only boolean (found or not found).
I thought that after the Range.Find.Execute, Range gets redefined to be the
text you are looking for, so:
Range.Find.Execute
Range.Text:='';
Range.InlineShapes.AddPicture(sSearchValue);
Then you will probably have to redefine your range, or do a FindNext (which
may give you a new range in the same original range that you had - I don't
know).
Oliver
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|