Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The attached document has a single table with two columns.
    Selecting the entire table using Table > Select > Table, or all the rows starting with the second one (but not just the second one, and not rows 1 and 2 or rows 1-3) using Table > Select > Rows and suddenly, VBA can't fetch the style of the selection.

    The problem specifically happens when the end-of-row-markers are selected. Don't select the EOR's, and it works too. Selecting more than just the table, and there's no problem either.

    But select all (or that most I specified above) including the EORs, and Selection.Style returns error 91 (object or with variable not set).

    Plenty of other tables work fine, but I'm finding this problem with a lot of tables in my documents. I need to make this work, because I have tools that operate on entire tables.

    My usual trick of saving the doc out to XML and bringing it back into doc again doesn't help either, so I'm not sure it's a typical corruption.
    The attachment is a .docx, but the .doc version of this file behaves the same way, and I can reproduce the problem in 2003 and 2007.

    Any suggestions would be appreciated.
    For instance, if there were a non-time-consuming way to detect if the whole table or whole rows were selected, I could back the end of the selection one character to the left, and deselect the row ends -- then, there's no problem.
    Attached Files Attached Files

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    This isn't a solution but it may provide a path past this buggy behaviour. It appears to be somehow related to the table style. If I change the table style to something other than 'Table Normal' then the code doesn't appear to throw an error (with my brief testing) unless the selection goes beyond the end of the table.
    Selection.Tables(1).Style appears to work regardless so maybe that will allow you to achieve your aims

    Perhaps you could put the code inside an if statement
    If Selection.tables(1).style = "Table Normal"
    'code may fail depending on selection
    Else
    msgbox selection.style
    End if
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi joelfinkle,

    I'm not seeing the behaviour you describe. Using the document attached to your post, I ran the following code with a variety of selections:
    Code:
    Sub Test()
    Dim oPara As Paragraph, sStyles As String
    For Each oPara In Selection.Paragraphs
    sStyles = sStyles & oPara.Style & "*"
    Next
    MsgBox Replace(Trim(sStyles), "*", Chr(10))
    End Sub
    Each time, I was able to retrieve the style names without difficulty.

    Perhaps we'd get a better idea of where the problem lies if you post your code.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='macropod' post='786496' date='28-Jul-2009 00:35']Hi joelfinkle,

    I'm not seeing the behaviour you describe. Using the document attached to your post, I ran the following code with a variety of selections:
    Code:
    Sub Test()
    Dim oPara As Paragraph, sStyles As String
    For Each oPara In Selection.Paragraphs
    sStyles = sStyles & oPara.Style & "*"
    Next
    MsgBox Replace(Trim(sStyles), "*", Chr(10))
    End Sub
    Each time, I was able to retrieve the style names without difficulty.

    Perhaps we'd get a better idea of where the problem lies if you post your code.[/quote]
    Macropod,
    The problem occurs when the entire table is selected, on Selection.Tables. I'm going to check on the Table Style issue -- perhaps that's what's causing my problem.
    Part of my code must deal with users selecting arbitrary text -- which could be the table, it could be a few random paragraphs.

    In one part of code, I apply or remove (the latter is tricky, involves a temp doc where I delete the style -- you can't just do a .font.reset or you lose bold, fonts, etc.) a character style to whatever is selected, based on whether the text is already in the character style, e.g.

    sStyle = Selection.Style ' Throws error
    if sStyle <> "My Character Style" then
    Selection.Style = "My Character Style"
    else
    RemoveStyleFromText Selection.Range, "My Character Style"
    end if

    Checking each paragraph individually is not acceptable: it may not be entire paragraphs, and I want to know if *all* of the text shares the style.
    Selection.Style ought to work, but doesn't.

  5. #5
    2 Star Lounger
    Join Date
    Jan 2004
    Location
    Mount Prospect, Illinois
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Through the assistance of a partner company of ours, I have the (lousy) answer:

    The Selection.Style method fails with error 91 if more than 50 paragraphs are selected.

    That's lame, lame, lame.
    But at least I can test for it.

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='joelfinkle' post='786536' date='28-Jul-2009 07:33']The Selection.Style method fails with error 91 if more than 50 paragraphs are selected.

    That's lame, lame, lame.[/quote]
    But I believe it is consistent with how selections work interactively, at least through Word 2003.

    Edit: I note that in tables, the toolbar shows the table style, so it isn't as obvious what's happening.

Posting Permissions

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