用文本替换所有表

时间:2019-05-30 23:05:54

标签: vba ms-word

我想用以下文本替换文档中的所有表:

“表1”(替换第一个表) “表2”(替换第二个表) ...

有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

像这样的东西更优雅,可能会帮助您更好地学习这些概念。

Sub LoopThroughAllTablesinWorkbook()

'PURPOSE: Loop through and apply a change to all Tables in the Excel Workbook
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim tbl As ListObject
Dim sht As Worksheet

'Loop through each sheet and table in the workbook
  For Each sht In ThisWorkbook.Worksheets
    For Each tbl In sht.ListObjects

      'Do something to all the tables...
        tbl.ShowTotals = True

    Next tbl
  Next sht

End Sub

'或者...

Sub tableAllSheet()

    Dim sh As Worksheet
    Dim tbl As ListObject

    'Loop through all sheets
    For Each sh In ThisWorkbook.Worksheets
        'Loop through all table on a sheet
        For Each tbl In sh.ListObjects
            'Print table name, table header row address and data range address to Immediate window
            Debug.Print tbl.Name & vbTab & tbl.HeaderRowRange.Address & vbTab & tbl.DataBodyRange.Address
        Next tbl
    Next sh

End Sub

答案 1 :(得分:0)

我在这里提供由我自己开发的解决方案:

Sub MyMacro()

Dim i As Integer

For i = 1 To ActiveDocument.Tables.Count
    ActiveDocument.Tables(1).Select
    ActiveDocument.Tables(1).Delete
    Selection.TypeText Text:="Table" & i
    Selection.InsertParagraphAfter
Next i

End Sub