Group:  Microsoft Word ยป microsoft.public.word.oleinterop
Thread: Word Replacing text maintaining formatting

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Word Replacing text maintaining formatting
tartancli 06.07.2006 17:16:02
Hi,

I have a C# Word add-in where I want to replace the text of each paragraph
with alternate text. I achieve this as follows:

foreach paragraph in the document
r = p.Range;
text = r.Text;
alternateText = getAltText(text)
r.Text = alternateText

This works fine for replacing the text. However I do lose formatting
information (e.g. where one word in the paragraph is italisised or where a
hypertext link is included). Is there another way to do what I want without
losing formatting?

Thanks.


Re: Word Replacing text maintaining formatting
Cindy M -WordMVP- <C.Meister-C[ at ]hispeed.ch> 07.07.2006 11:08:43
Hi =?Utf-8?B?dGFydGFuY2xp?=,

[Quoted Text]
> I have a C# Word add-in where I want to replace the text of each paragraph
> with alternate text. I achieve this as follows:
>
> foreach paragraph in the document
> r = p.Range;
> text = r.Text;
> alternateText = getAltText(text)
> r.Text = alternateText
>
> This works fine for replacing the text. However I do lose formatting
> information (e.g. where one word in the paragraph is italisised or where a
> hypertext link is included). Is there another way to do what I want without
> losing formatting?
>
Formatting within a paragraph is at the character level. Plus, the Text
property deals only with text, no formatting.

Assuming the alternateText is being picked up from a Word document, and
contains the required formatting, then you can use r.FormattedText =
otherr.FormattedText (in this case getAltText should return a range).

That's about all one can say without knowing where the alternate text is coming
from, or what kind of formatting is involved.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)

Re: Word Replacing text maintaining formatting
tartancli 11.07.2006 16:14:02
Hi Cindy,

Thanks for the reply. The replacement text doesn't come from another Word
document. It is dynamically generated and depends upon the original text and
some data taken from persistent store. What i really want to do is to
maintain the original formatting for the replacement text.


"Cindy M -WordMVP-" wrote:

[Quoted Text]
> Hi =?Utf-8?B?dGFydGFuY2xp?=,
>
> > I have a C# Word add-in where I want to replace the text of each paragraph
> > with alternate text. I achieve this as follows:
> >
> > foreach paragraph in the document
> > r = p.Range;
> > text = r.Text;
> > alternateText = getAltText(text)
> > r.Text = alternateText
> >
> > This works fine for replacing the text. However I do lose formatting
> > information (e.g. where one word in the paragraph is italisised or where a
> > hypertext link is included). Is there another way to do what I want without
> > losing formatting?
> >
> Formatting within a paragraph is at the character level. Plus, the Text
> property deals only with text, no formatting.
>
> Assuming the alternateText is being picked up from a Word document, and
> contains the required formatting, then you can use r.FormattedText =
> otherr.FormattedText (in this case getAltText should return a range).
>
> That's about all one can say without knowing where the alternate text is coming
> from, or what kind of formatting is involved.
>
> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
> http://www.word.mvps.org
>
> This reply is posted in the Newsgroup; please post any follow question or reply
> in the newsgroup and not by e-mail :-)
>
>
Re: Word Replacing text maintaining formatting
Cindy M -WordMVP- <C.Meister-C[ at ]hispeed.ch> 12.07.2006 10:51:11
Hi =?Utf-8?B?dGFydGFuY2xp?=,

[Quoted Text]
> The replacement text doesn't come from another Word
> document. It is dynamically generated and depends upon the original text and
> some data taken from persistent store. What i really want to do is to
> maintain the original formatting for the replacement text.
>
Can you be any more specific about this? Maybe a couple of examples (use <markup> to
indicate formatting in both original and alternate)?

There's not going to be any "simple" way to do this that matches the example you
show. If we have some details, we may be able to suggest some alternate approaches.
You should also tell us which version of Word is involved as that can affect the
possibilities available to you.

Further: should this be available in "any and every" document, or just certain types
of document?

> > > I have a C# Word add-in where I want to replace the text of each paragraph
> > > with alternate text. I achieve this as follows:
> > >
> > > foreach paragraph in the document
> > > r = p.Range;
> > > text = r.Text;
> > > alternateText = getAltText(text)
> > > r.Text = alternateText
> > >
> > > This works fine for replacing the text. However I do lose formatting
> > > information (e.g. where one word in the paragraph is italisised or where a
> > > hypertext link is included). Is there another way to do what I want without
> > > losing formatting?
> > >
> > Formatting within a paragraph is at the character level. Plus, the Text
> > property deals only with text, no formatting.
> >
> > Assuming the alternateText is being picked up from a Word document, and
> > contains the required formatting, then you can use r.FormattedText =
> > otherr.FormattedText (in this case getAltText should return a range).
> >
> > That's about all one can say without knowing where the alternate text is coming
> > from, or what kind of formatting is involved.
>

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in
the newsgroup and not by e-mail :-)

Re: Word Replacing text maintaining formatting
tartancli 13.07.2006 08:55:02
Hi Cindy,

Thanks again for your help. Here's a small example of what i'm trying to
achieve.

Using markup as follows:
<h1> = heading1 style
<TRx> = Font Times Roman x Pt
<b> = bold
<i>= italic


Example Original Text:

<h1>This line has a heading1 style and may have [xxxxx yyyyyy] replaced</h1>

This line has <TR16>different font</TR16> sizes and may have
[<TR8>xxxx</TR8> <TR16>yyyyy</TR16>] replaced

This line has bold and italics and may have [<b>xxxx</b> <i>yy</i>yyy]
replaced

Required output:

<h1>This line has a heading1 style and may have [aaaaa bbbbbb] replaced</h1>

This line has <TR16>different font</TR16> sizes and may have
[<TR8>aaaa</TR8> <TR16>bbbbb</TR16>] replaced

This line has bold and italics and may have [<b>aaaa</b> <i>bb</i>bbb]
replaced

Sizes of replacement text are deliberatly chosen (i.e replacement text has
the same number of characters as the original text). The original text markup
is example only, I have no control over how the origianl text is formatted -
i must just maintain it for the replacement text.

The add-in is being developed for all Word versions from 97 to 2003. The
user may select to convert any and every document.

Thanks again.

"Cindy M -WordMVP-" wrote:

[Quoted Text]
> Hi =?Utf-8?B?dGFydGFuY2xp?=,
>
> > The replacement text doesn't come from another Word
> > document. It is dynamically generated and depends upon the original text and
> > some data taken from persistent store. What i really want to do is to
> > maintain the original formatting for the replacement text.
> >
> Can you be any more specific about this? Maybe a couple of examples (use <markup> to
> indicate formatting in both original and alternate)?
>
> There's not going to be any "simple" way to do this that matches the example you
> show. If we have some details, we may be able to suggest some alternate approaches.
> You should also tell us which version of Word is involved as that can affect the
> possibilities available to you.
>
> Further: should this be available in "any and every" document, or just certain types
> of document?
>
> > > > I have a C# Word add-in where I want to replace the text of each paragraph
> > > > with alternate text. I achieve this as follows:
> > > >
> > > > foreach paragraph in the document
> > > > r = p.Range;
> > > > text = r.Text;
> > > > alternateText = getAltText(text)
> > > > r.Text = alternateText
> > > >
> > > > This works fine for replacing the text. However I do lose formatting
> > > > information (e.g. where one word in the paragraph is italisised or where a
> > > > hypertext link is included). Is there another way to do what I want without
> > > > losing formatting?
> > > >
> > > Formatting within a paragraph is at the character level. Plus, the Text
> > > property deals only with text, no formatting.
> > >
> > > Assuming the alternateText is being picked up from a Word document, and
> > > contains the required formatting, then you can use r.FormattedText =
> > > otherr.FormattedText (in this case getAltText should return a range).
> > >
> > > That's about all one can say without knowing where the alternate text is coming
> > > from, or what kind of formatting is involved.
> >
>
> Cindy Meister
> INTER-Solutions, Switzerland
> http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
> http://www.word.mvps.org
>
> This reply is posted in the Newsgroup; please post any follow question or reply in
> the newsgroup and not by e-mail :-)
>
>
Re: Word Replacing text maintaining formatting
Cindy M -WordMVP- <C.Meister-C[ at ]hispeed.ch> 13.07.2006 11:47:11
Hi =?Utf-8?B?dGFydGFuY2xp?=,

[Quoted Text]
> The add-in is being developed for all Word versions from 97 to 2003. The
> user may select to convert any and every document.
>
Word 97 doesn't support COM-Addins, so you may need to re-think this... And
you need to develop against the oldest supported version, then test in all
newer versions, otherwise you may be in for nasty surprises

I'm afraid the only possibility you're going to have is to use Word's FIND
(and maybe Replace) functionality to search for each possibility,
individually. I'd try first with the full Find and Replace. As long as the
formatting doesn't change within the found range, it should retain it. If
not, we can work around it.

doc.Find.ClearFormatting(); to reset the Find in case the user has done
anything with it.
doc.Find.Execute(bunch of parameters) is the method you need, but this is
going to give you problems in a multi-version environment as the list of
optional parameters differs from version to version.

Plus, see this

http://support.microsoft.com/default.aspx?scid=kb;en-us;313104

Facit:
You'll need to first test the Application.Version and branch accordingly.
Set up one method for each supported version, using late-binding as
described in the cited article so that your code will compile.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)

Re: Word Replacing text maintaining formatting
tartancli 13.07.2006 14:22:02

Thanks Cindy.

[Quoted Text]
> Word 97 doesn't support COM-Addins, so you may need to re-think this... And
> you need to develop against the oldest supported version, then test in all
> newer versions, otherwise you may be in for nasty surprises

Although i understand that 97 doesn't directly support COM-Addins, I
understood that there were ways to work around this with a sort of small VBA
wrapper. I read about this possibility at
http://www.codeproject.com/com/adwordaddin.asp, though i haven't tried it
yet. Do you have any opinion as to how effective this approach is?


> I'm afraid the only possibility you're going to have is to use Word's FIND
> (and maybe Replace) functionality to search for each possibility,

Thanks, I'll try using this method. Regards.
Re: Word Replacing text maintaining formatting
Cindy M -WordMVP- <C.Meister-C[ at ]hispeed.ch> 13.07.2006 16:12:20
Hi =?Utf-8?B?dGFydGFuY2xp?=,

[Quoted Text]
> Although i understand that 97 doesn't directly support COM-Addins, I
> understood that there were ways to work around this with a sort of small VBA
> wrapper. I read about this possibility at
> http://www.codeproject.com/com/adwordaddin.asp, though i haven't tried it
> yet. Do you have any opinion as to how effective this approach is?
>
Ah, new one for me :-) Thanks for the link. Yes, I should think this should
work as long as the user doesn't change his Startup folder :-)

This would also give you the possibiltiy of assigning keyboard shortcuts to
functions in your Addin, if you need that, BTW.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :-)

Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net