English 中文(简体)
VBA将MS WORD表从11列编辑到6列
原标题:VBA to edit MS WORD table from 11 cols to 6 cols
  • 时间:2012-05-27 17:44:18
  •  标签:
  • vba
  • ms-word

我有很多MS Word表格,有11个栏目需要编辑。

每份文件将包含1个表格。

我想把表格从11列编辑到6列。

VBA执行以下任务:

  1. Rename col 2 heading from Time to Item
  2. Rename col 3 heading from Time Zone to Comment
  3. 将第5列标题从类型重命名为E或A

  4. 删除第2、3和5列中的所有文本(不包括重命名的标题)

  5. Delete cols 6, 7, 9, 10, 11

我已经按照我认为最有效的方式列出了编辑表格的顺序,但这真的不重要。

我不知道第2、3、5列中的文本是否可以在不删除标题的情况下删除。

最后,我想我可以在第2、3、5列中输入我想要的新标题,如果这些标题在删除这些列中的文本时被删除。

谢谢你的帮助。

问题回答

希望这能有所帮助,

Option Explicit

Private Sub myTableMacro()
    Dim tmpTable As Word.Table, tmpRange As Word.Range
    With ActiveDocument
        If .Tables.Count > 0 Then
            For Each tmpTable In .Tables
                     Rename col 2 heading from Time to Item
                tmpTable.Cell(1, 2).Range.Text = "Item"
                     Rename col 3 heading from Time Zone to Comment
                tmpTable.Cell(1, 3).Range.Text = "Comment"
                     Rename col 5 heading from Type to E or A
                tmpTable.Cell(1, 5).Range.Text = "E or A"
                     Delete all text in cols 2, 3 (but not the headers)
                Set tmpRange = .Range(tmpTable.Cell(2, 2).Range.Start, tmpTable.Cell(tmpTable.Rows.Count, 3).Range.End)
                tmpRange.Select
                Selection.Delete Unit:=wdCharacter, Count:=1
                     Delete all text in cols 5 (but not the headers)
                Set tmpRange = .Range(tmpTable.Cell(2, 5).Range.Start, tmpTable.Cell(tmpTable.Rows.Count, 5).Range.End)
                tmpRange.Select
                Selection.Delete Unit:=wdCharacter, Count:=1
                     Delete cols 10, 11
                Set tmpRange = .Range(tmpTable.Cell(1, 9).Range.Start, tmpTable.Cell(1, 11).Range.End)
                tmpRange.Select
                Selection.Cells.Delete wdDeleteCellsEntireColumn
                     Delete cols 6, 7, 9
                Set tmpRange = .Range(tmpTable.Cell(1, 6).Range.Start, tmpTable.Cell(1, 7).Range.End)
                tmpRange.Select
                Selection.Cells.Delete wdDeleteCellsEntireColumn
            Next  tmpTable
        End If
    End With
    Set tmpRange = Nothing
    Set tmpTable = Nothing
End Sub

我已经测试过了,发现还可以。





相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签