PDA

View Full Version : Understanding Selection.Font object



falconwing
10-12-2007, 11:41 AM
In a word 2003 document, the user invokes my macro anywhere inside the document. The macro then injects text with normal style. After it is finished, the macro should set the text attributes back to what they where before the text was injected.

Dim saveStyle As String
Dim saveFont As Object
...
saveStyle = Selection.Style
Set saveFont = Selection.Font
...
'code injects text here
....
'now ready to put text attributes back as they were
Selection.Style = saveStyle
Set Selection.Font = saveFont

So the problem is that the last line of code fails ("Run-time error '438': Object doesn't support this property of method").

I have also tried making saveFont type Font, but that also does not work. When I look at Selection.Font and saveFont in the variables windows they are both object/Font and have the same data. I'd prefer some elegant way to do this rather than discretely setting every text attribute, although if there was a way to foreach through them programmatically that would be bearable.

Just saving and restoring the style has not been sufficient as it does not pick up font changes on an existing style.

I do not want to
with Selection.Font
.Bold = saveFont.Bold
.Name = saveFont.Name
.Size = saveFont.Size
... etc may times over ...
end with

Any insights greatly appreciated...

Falconwing

fumei
10-12-2007, 12:54 PM
Just saving and restoring the style has not been sufficient as it does not pick up font changes on an existing style.No...it won't, and it never will.

Existing styles should never have font changes. That completely defeats the reason, the purpose, of Styles.

Properly used, a Word document should NEVER, EVER, use Normal Style.

I have written probably thousands of documents. None of them have a single instance of Normal style. None.

Sigh.

If you have to do this (inserting text with Normal), you should simply set the Selection style back to what it was. There is no point is setting the selection style back, and then change the font!

How is Word to know what the manually changed attributes were? I will tell you. One....by one...by one...by one.

Exactly as you do not want to do.

.Font.Size = yadda
.Font.Bold = true or false
.Font.Name = whatever it was.

That is exactly the price you pay for not using Styles properly. If a style is manually changed, Word has to be told to individually change those attributes.

Not having to do this is the entire purpose of using Styles.

If you do not DO any "font changes on an existing style" then you will not have this problem at all.

If you DO "font changes on an existing style"....you will.

Try doing:MsgBox Selection.FontYou get an error: Object does not support this property or method.

Font is a container of child objects. It requires those child object properties.

Selection.Font does not mean anything.

Sigh. It goes against the grain to tell you this, but there is in fact a way around this....sort of. Here are your choices.

1. Yup, you run through all the attributes one at a time.

2. You make a new Style of the (wrongly) manually changed style. Then you apply that Style back again.

This is VERY VERY poor use of Word. The Method is Duplicate. Its REAL purpose is to create new styles programmatically. Here is how:Dim aFont As Font
Set aFont = Selection.Font.Duplicate
' create a NEW Style of the current Selection
' character attributes
ActiveDocument.Styles.Add(Name:="BadBadBadDog", _
Type:=wdStyleTypeCharacter).Font = aFont

' do whatever it is you are doing

' now USE the new Style
Selection.Style = "BadBadBadDog"

Note that you can NOT, repeat NOT, use the Duplicated attributes by itself.Dim aFont
Set aFont = Selection.Font.Duplicate

' do whatever it is you are doing
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:="Should be Normal." & vbCrLf
' now USE the new Style????
Selection.Font = aFont
' NOPE! ERROR!Because aFont has the same issue that Font alone has....it requires child properties. It can pass those attributes to a new Style object.