Applying Font in TA field code

T

tr7

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.
 
S

StevenM

To: tr7,

Assuming you have it selected, you need:

Selection.Font.Italic = True

Steven Craig Miller
 
J

Jay Freedman

tr7 said:
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
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jean-Guy Marcil

tr7 said:
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
 
T

tr7

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top