Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Styles, manual formatting and tables in VBA AAARGH

    I don't have as much hair to lose as I once did, and trying to get all my tables to look how I'd like them is definitely not what the trichologist ordered.

    I'm trying to apply a named style to all the tables in a document. I'd like to keep some aspects of manual formatting, such as colour, bold and italic highlights, but get rid of others, such as errant fonts left over from copying a table from excel. Unfortunately the default behaviour seems to be exactly the opposite. On running this code:

    Sub FormatTableText()
    Dim aTable As Table, aRow As Row, aCell As Cell, lColour As Long, aWord As Words
    For Each aTable In ActiveDocument.Tables
    aTable.Select
    If Selection.Font.ColorIndex = wdAuto Then
    Selection.Style = ActiveDocument.Styles("NormalTable")
    Else
    For Each aCell In Selection.Cells
    aCell.Select
    If Selection.Font.ColorIndex = wdAuto Then
    Selection.Style = ActiveDocument.Styles("NormalTable")
    ElseIf Selection.Font.ColorIndex <> 9999999 Then
    lColour = Selection.Font.Color
    Selection.Style = ActiveDocument.Styles("NormalTable")
    Selection.Font.Color = lColour
    Else
    For Each aWord In Selection.Words
    If Selection.Font.ColorIndex = wdAuto Then
    Selection.Style = ActiveDocument.Styles("NormalTable")
    ElseIf Selection.Font.ColorIndex <> 9999999 Then
    lColour = Selection.Font.Color
    Selection.Style = ActiveDocument.Styles("NormalTable")
    Selection.Font.Color = lColour
    End If
    Next
    End If
    Next
    End If
    Next
    End Sub

    I lose all bold and italics in cells, and manual alignment (most of the content is right aligned, whereas the headings are left aligned), but there are still some tables with bizarre fonts, even though the style is reported to be NormalTable. Some desired formatting, eg superscript and subscripting, is preserved.

    I have several questions:
    1) Why are the results inconsistent?
    2) Previously when replacing styles, the manual formatting has not been overwritten (unless I reset manual formatting specifically) - why is it happening here?
    3) Is there any way to check if a cell has any manual formatting applied to it - checking for colour, bold, italic and alignment separately will make the code painful (and might miss something I've forgotten)
    4) This code fails at the third nested level - how should I refer to individual words within a cell (I've never really understood the Word object model)?

    More worrying still is that the custom border formatting appears to have been removed on a number of tables. The style specifies nothing about borders (this is 2000 - so no table styles), and I would have thought these should have been left unchanged.

    Thanks for your help

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Styles, manual formatting and tables in VBA AAARGH

    I don't have any help for you, unfortunately, but I may have at least a partial explanation of your inconsistent results (although you may already know about this). When you change the style of a paragraph, and there's manual formatting in the paragraph that's inconsistent with the style (e.g., italic words when the paragraph style isn't italic), Word will blow out that manual formatting, but only if the manual formatting affects more than half of the paragraph. So, for example, if you have a 5-word paragraph, and the entire paragraph (or maybe 4 out of 5 words) is bold (manually formatted) but just one word is italic, and you change the style of the paragraph to a paragraph style that doesn't include either bold or italic, the bold formatting will disappear but the one italic word will remain italic.

    One of my hugest pet peeves with Word (and I have a lot of problems with Word) is the fact that there's no option to turn off this "helpful" behavior.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Styles, manual formatting and tables in VBA AA

    Thanks, I didn't know that, but I'm not sure that it applies here.

    The plot thickens, though, as, exasperated, I've taken to manually formatting the tables. The big surprise here is that bold and italic highlights are not removed on manual application of a style to the whole table, although font colour does revert to auto. In addition, most of the undesired manual formatting does disappear, although not all of it (some requires a ctrl-space to leave). So there seem to be differences in applying a style to a table manually, and applying it with VBA, which I don't understand. For completeness I should add that for the manual style application I'm selecting the table by placing the cursor anywhere in the table and choosing select-table from the table menu, so the differences in behaviour shouldn't be down to differences in selection behaviour.

  4. #4
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Styles, manual formatting and tables in VBA AA

    I may have to eat my words, as it appears you were correct, except that word seems to be treating the entire table as a paragraph.

    If I manually apply a style twice to a table, I receive a little pop up box asking me whether I want to reapply the style or update the style. Choosing reapply then does clear all my useful and desired manually applied formatting, and almost always clears up any undesired font differences (but not always - that I still don't understand). I don't know how Word calculates whether a style is in the majority or not - some fonts which occupy the majority of cells, and comprise the majority of words, are not cleared until the second application. They might be in a minority on character count, though.

    So it appears that VBA was actually doing it "right", with the exception of these odd resistant changes that don't respond to a second application of the style and need a ctrl-space to remove.

    So questions 1 and 2 have been answered - this just leaves the VBA issues in 3 & 4. I shall await the arrival of the VBA cavalry on Monday with eager anticipation.

    Thanks for your help.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Styles, manual formatting and tables in VBA AA

    I once wrote a Macro that selectively removed any desired manual formatting from paragraphs selected by Style. It is here in the Lounge somewhere, just let me have a search...

    ...my goodness that was a while ago, November 2003, <post:=309,846>post 309,846</post:>.

    StuartR

  6. #6
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Styles, manual formatting and tables in VBA AA

    Hi Mark,

    Looks like you are being hit by Words "Character/Paragraph" style formatting.
    From para to para you apply the entire style. Within paras you apply the character style (a word in bold or italic - or even another font)

    I can think of at least two ways around this:
    1. Selection.font.reset (if it's only a font issue)
    2. Selection.clearformatting - then reapply the style you want (This is the heavy one, where you overrule the users preferences and enforces "corporate style")

    You can do this to either individual cells or entire tables - as you prefer.
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

Posting Permissions

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