• Selection.Style fails on table???

    Author
    Topic
    #461499

    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.

    Viewing 2 reply threads
    Author
    Replies
    • #1171286

      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

    • #1171287

      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
      [Fmr MS MVP - Word]

      • #1171308

        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.

        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.

    • #1171319

      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.

      • #1171347

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

        That’s lame, lame, lame.

        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.

    Viewing 2 reply threads
    Reply To: Selection.Style fails on table???

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: