Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Formatting precedence (VBA/Word/2000 SP-3)

    I am trying to control the font within tables loaded with the proper text but virtually uncontrolled font attributes. I am first determining if the required style is visible to the document, and if it is; create myFont; then modify myFont and apply it to the text. The following code applies. When this procedure is called the cursor is within the table of interest.
    <pre>Dim i As Long
    Dim myFont As Font

    With ActiveDocument
    For i = 1 To ActiveDocument.Styles.Count
    If .Styles(i).NameLocal = "Table Text" Then Exit For
    Next i
    Set myFont = .Styles(i).Font.Duplicate
    End With

    With myFont
    .Underline = wdUnderlineNone
    .Italic = False
    End With

    Selection.Tables(1).Select
    Selection.Font = myFont
    </pre>


    Running this code does not initially remove the underlining nor italics which have been applied to portions of the text. However if I revise the "With myFont" section of the code to set the Underline to any specific Underline attribute other than "None", and set the "Italic" attribute to "True"; when I rerun the code the complete table is italicized and underlined. Then reverting to the original code, it is all cleared.
    From the foregoing and my understanding of the formatting precedence, it appears to me that:
    <UL><LI>The formatting which was applied to the characters directly took precedence over any attributes assigned to myFont
    <LI>When myFont was applied with a matching characteristic, the precedence for that characteristic was yielded to the applied font (myFont).[/list] Can someone please confirm, expand upon my understanding, or set me straight?
    TIA
    Regards
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    Why not use

    Dim myFont As Font
    Set myFont = ActiveDocument.Styles("Table Text").Font
    With Selection.Tables(1).Range
    .Font.Italic = False
    .Font.Underline = wdUnderlineNone
    .Font = myFont
    End With

  3. #3
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    Hans

    I apologize for not being more clear. What I provided in my post, is a work in progress. There is no certainty that "Table Text" will be available to the document; hence the For - Next loop at the beginning of the code. I would welcome a more efficient manner of determining the availability of a given style.

    Following the For - Next loop, there will eventually be an If - Else construct based on the value of i. At this stage of development I was working with a document that contained the style "Table Text", with the intent of sorting out the code for the situation where "Table Text" exists. The code for the situation where "Table Text" is currently missing and will be developed after I get to understand this bit of code.

    I expected that setting myFont to the font found in the "Table Text" style, then setting particular attributes of myFont, I would be able to change the format of the existing text. I was surprised to find the behaviour described in my initial post, and seek confirmation or correction of my understanding presented in my initial post.
    Regards
    Don

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    I see the same behavior as you do, so I suppose you're correct that setting the font of a range to a specific font applies the attibutes that are present in the font, but doesn't remove attributes that aren't present in the font, in other words if the applied font is bold, the range will become bold, but if the applied font isn't bold, the bold attribute of the range will be left unchanged.

    Another way to test the presence of a given style is:

    On Error Resume Next
    Set myFont = ActiveDocument.Styles("Table Text").Font
    On Error GoTo 0 ' or to your error handler
    If myFont Is Nothing Then
    ' uh oh, style not present, font not set
    ...
    Else
    ' ok, style present, font set
    ...
    End If

    Note: some programmers abhor the use of On Error Resume Next. I have no objection in situations such as this one.

  5. #5
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    Thank you Hans
    Regards
    Don

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    > Note: some programmers abhor the use of On Error Resume Next.

    Speaking as a programmer who abhors the use of On Error Resume Next, ....
    That's me.
    However, I've modified my views somewhat.
    In the example given, alternative program code , without the On Error trap, must loop in VBA, examining every named style. That's slower than On Error, I would think, and adds a layer of complexity to the VBA code that must be read by another programmer.

    My shift is away from "On Error is bad" towards "I'm forced into On Error because the VBA designers didn't give us a full complement of tests-for-existence".

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Formatting precedence (VBA/Word/2000 SP-3)

    Try VB.Net, Chris. It has more stuff than you can use in a normal lifetime ... assuming normality, of course. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>
    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •