BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Replaceing tags in MS Word document

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation
View previous topic :: View next topic  
Author Message
Rok Rejc
Guest





PostPosted: Sat Sep 03, 2005 9:48 am    Post subject: Replaceing tags in MS Word document Reply with quote



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





PostPosted: Sun Sep 04, 2005 5:36 am    Post subject: Re: Replaceing tags in MS Word document Reply with quote



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





PostPosted: Sun Sep 04, 2005 1:09 pm    Post subject: Re: Replaceing tags in MS Word document Reply with quote



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





PostPosted: Mon Sep 05, 2005 6:22 am    Post subject: Re: Replaceing tags in MS Word document Reply with quote

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





PostPosted: Mon Sep 05, 2005 5:17 pm    Post subject: Re: Replaceing tags in MS Word document Reply with quote

Yeah its working Smile
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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.