Hi,
I have a method in C# which is trying to change the font from unformatted to
either italics or underline in a TA field code.

I'm doing:

Application.Selection.Range.Font.Italic == 1;

and it keeps reverting back to -1 as the value for italic so the font
remains unformatted. Does anyone know how I can apply a format to selected
text in a TA field code and have it stick? Thanks very much.

RE: Applying Font in TA field code by stevencraigmiller(at)comcast(dot)net>

stevencraigmiller(at)comcast(dot)net>
Tue Jun 17 10:51:00 PDT 2008

To: tr7,

Assuming you have it selected, you need:

Selection.Font.Italic = True

Steven Craig Miller

"tr7" wrote:

> Hi,
> I have a method in C# which is trying to change the font from unformatted to
> either italics or underline in a TA field code.
>
> I'm doing:
>
> Application.Selection.Range.Font.Italic == 1;
>
> and it keeps reverting back to -1 as the value for italic so the font
> remains unformatted. Does anyone know how I can apply a format to selected
> text in a TA field code and have it stick? Thanks very much.
>

Re: Applying Font in TA field code by Jay

Jay
Tue Jun 17 10:53:38 PDT 2008

tr7 wrote:
> Hi,
> I have a method in C# which is trying to change the font from
> unformatted to either italics or underline in a TA field code.
>
> I'm doing:
>
> Application.Selection.Range.Font.Italic == 1;
>
> and it keeps reverting back to -1 as the value for italic so the font
> remains unformatted. Does anyone know how I can apply a format to
> selected text in a TA field code and have it stick? Thanks very much.

If your code is exactly as shown, the == operator is the problem. It's the
C# operator for comparison for equality. That means you aren't assigning a
value to anything. Use only a single = sign.

A second "problem" is that the value of the constant True in VB/VBA is -1
and not 1.

A possible third problem is that the field code must be displayed and the
desired text inside it must be selected in order for this statement to have
any effect. Can you verify that that's the case at run time?

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.



RE: Applying Font in TA field code by JeanGuyMarcil

JeanGuyMarcil
Tue Jun 17 11:08:02 PDT 2008

"tr7" wrote:

> Hi,
> I have a method in C# which is trying to change the font from unformatted to
> either italics or underline in a TA field code.
>
> I'm doing:
>
> Application.Selection.Range.Font.Italic == 1;
>
> and it keeps reverting back to -1 as the value for italic so the font
> remains unformatted. Does anyone know how I can apply a format to selected
> text in a TA field code and have it stick? Thanks very much.

The code you posted, as is, should work. If it doesn't, then you either have
some other code that is interfering or the Selection is not what you think it
is.

By the way, if you are coding in C#, you are probably automating Word. If
you are automating Word, using the Selection object is not a good idea. In
fact, whether I am automating Word or not, I always try not to use the
Selection object. For example, if I want to make the second line (or
paragraph) of the Table of Authorities italic, I could use:


Dim myTOA As TableOfAuthorities

Set myTOA = ActiveDocument.TablesOfAuthorities(1)

myTOA.Range.Paragraphs(2).Range.Font.Italic = True

Re: Applying Font in TA field code by tr7

tr7
Tue Jun 17 16:39:00 PDT 2008

thanks very much everyone. The error was that I forgot that -1 is true and 0
is false. Thanks again. I really appreciate it.

"Jay Freedman" wrote:

> tr7 wrote:
> > Hi,
> > I have a method in C# which is trying to change the font from
> > unformatted to either italics or underline in a TA field code.
> >
> > I'm doing:
> >
> > Application.Selection.Range.Font.Italic == 1;
> >
> > and it keeps reverting back to -1 as the value for italic so the font
> > remains unformatted. Does anyone know how I can apply a format to
> > selected text in a TA field code and have it stick? Thanks very much.
>
> If your code is exactly as shown, the == operator is the problem. It's the
> C# operator for comparison for equality. That means you aren't assigning a
> value to anything. Use only a single = sign.
>
> A second "problem" is that the value of the constant True in VB/VBA is -1
> and not 1.
>
> A possible third problem is that the field code must be displayed and the
> desired text inside it must be selected in order for this statement to have
> any effect. Can you verify that that's the case at run time?
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
>
>