none
Word Macro RRS feed

  • Question

  • Hi all! I have a question. 

    Now I want to write a macro to select all the text except the tables in Word Document. Is it achievable? Please help me.

    Now I have a macro to select all the tables,

    Sub selectalltables()
    '
    '
    Dim mytable As Table
     
    For Each mytable In ActiveDocument.Tables
    mytable.Range.Editors.Add wdEditorEveryone
    Next
    ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
    ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
    
    End Sub


    Saturday, March 30, 2019 4:27 AM

Answers

  • If all you want to do is to apply a heading Style to is the paragraph preceding each table, you might use something like:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table
    For Each Tbl In ActiveDocument.Tables
      Tbl.Range.Characters.First.Previous.ParagraphFormat.Style = wdStyleHeading1
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 31, 2019 7:38 AM

All replies

  • You can't select discontiguous ranges with VBA. Rather, you'd need to loop through the tables and process them iteratively. For example:

    Sub Demo()
    Dim Tbl As Table
    For Each Tbl In ActiveDocument.Tables
      With Tbl
        MsgBox Split(.Cell(1, 1).Range.Text, vbCr)(0)
      End With
    Next
    End Sub

    or:

    Sub Demo()
    With ActiveDocument
      Do While .Tables.Count > 0
        .Tables(1).ConvertToText
      Loop
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, March 30, 2019 4:44 AM
  • Much obliged. By the way, I just want to apply the same style to the texts except tables(change them to headings) and automatic number them. Now I've done my best but have no way out. Could you please help me? Thanks in advance~~
    Saturday, March 30, 2019 11:03 AM
  • Dear macropod,

    Hi! I've achieved it by recording macro. First, I use the macro to select all the tables and change texts in tables to red. Then I use Find&Replace to reply numbering and title style to the black texts in Word document and then it did achieve. Because I'm not familiar with Word macro so I have to brainstorm to get some original ideas. Anyway, thanks for your help! Have a nice day!


    Saturday, March 30, 2019 3:13 PM
  • If all you want to do is to apply a heading Style to is the paragraph preceding each table, you might use something like:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Tbl As Table
    For Each Tbl In ActiveDocument.Tables
      Tbl.Range.Characters.First.Previous.ParagraphFormat.Style = wdStyleHeading1
    Next
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 31, 2019 7:38 AM
  • Awesome! Could you please explain it to me? I want to learn more.
    Sunday, March 31, 2019 7:48 PM
  • The code simply loops though all tables, applying the Heading 1 Style to whatever paragraph immediately precedes them. There is no need to mess with the tables themselves.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 31, 2019 9:42 PM