word宏 批量刪除含有關鍵字的表格列


工作中,由於每次修改word文檔,都需要進行繁瑣的修改,刪除多余的列或者調節表格的寬度。

使用列word宏編程的方式對word文檔進行對應的操作。

宏:

 1 Sub del_key()
 2     Dim str As String
 3     Dim table_count, i, j, table_lie As Integer
 4     str = "符合程度"
 5     table_count = ActiveDocument.Tables.Count
 6     For i = 1 To table_count
 7         table_lie = ActiveDocument.Tables(i).Columns.Count
 8         For j = 1 To table_lie
 9             If InStr(ActiveDocument.Tables(i).Cell(1, j), str) > 0 Then
10                 ActiveDocument.Tables(i).Cell(1, j).Select
11                 Selection.Columns.Delete
12                 With ActiveDocument.Tables(i)
13                     .PreferredWidthType = wdPreferredWidthPercent
14                     .PreferredWidth = 110
15                 End With
16                 ActiveDocument.Tables(i).Rows.Alignment = wdAlignRowCenter
17             End If
18         Next
19     Next
20 End Sub

代碼說明:

我只是去鑒別每個表格的第一行是否存在關鍵字,這是我的需求,存在就刪除,如果你們的是按照列來的話,修改Cell(1, j)中的1,仿照循環語句來寫;設置了居中屬性。

使用方法:

打開word,打開宏,將此段代碼復制到宏中,點擊運行即可(首次使用,建議使用備份文件進行測試,目前不知道有沒有bug,我目前沒碰到)

1. str為關鍵字,該代碼用來刪除含有str關鍵字的表格列
2. table_lie為表格的列數,行數可以寫為ActiveDocument.Tables(i).Rows.Count
3. .PreferredWidth為設置表格的寬度
4. ActiveDocument.Tables(i).Rows.Alignment = wdAlignRowCenter為表格的居中屬性 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM