快捷鍵
選區
- 選擇塊:
[Shift]
+click
,光標放到塊的一端,然后按住Shift
,然后光標放到塊的另一端。
更新域: F9
右鍵沒有更新域選項時可以使用,如更新全部域先Ctrl + A
然后F9
VBA 技巧
- 某個操作不知要調用什么方法可以先錄制宏然后查看宏的代碼
圖片
自動編號
插入 - 題注
批量修改大小
-
在 word 中按 alt+f11 組合鍵,進入 VBA 模式
-
在左邊的工程資源管理器中找到你的 word 文檔,在其上右鍵 / 添加 / 模塊
-
復制,粘貼如下代碼,修改 Mywidth 和 Myheigth 為圖片寬高
Sub Macro() Mywidth=200 '圖片寬度 Myheigth=200 '圖片高度 For Each iShape In ActiveDocument.InlineShapes iShape.Height = Myheigth iShape.Width = Mywidth Next iShape End Sub
-
f5 運行
批量居中
Sub ImageCenter()
For Each iShape In ActiveDocument.InlineShapes
iShape.Range.Paragraphs.Alignment = wdAlignParagraphCenter
Next iShape
End Sub
表格
設置表頭(第一行)和內容(其余行)樣式
(推薦)添加 tableBody,tableHead 樣式,給表頭和表內容添加樣式
Sub setTableStyle()
For Each aTable In ActiveDocument.Tables
'表內容
aTable.Select
With Selection
.Style = "tableBody"
End With
'表頭
aTable.Cell(1, 1).Select
With Selection
.SelectRow
.Style = "tableHead"
End With
Next aTable
End Sub
(不推薦)加粗表格第一行,直接修改樣式
Sub BoldTablesFristRow()
For Each aTable In ActiveDocument.Tables
aTable.Cell(1, 1).Select
With Selection
.SelectRow
For Each aCell In .Cells
aCell.Range.Bold = True
Next aCell
End With
Next aTable
End Sub
(不推薦)加粗表格第一行,直接修改樣式,會報 \<無法訪問此集合中單獨的行,因為表格有縱向合並的單元格。> 錯誤
Sub BoldTablesFristRow()
For Each aTable In ActiveDocument.Tables
For Each aCell In aTable.Rows.First.Cells
aCell.Range.Bold = True
Next aCell
Next aTable
End Sub
段落
遍歷全部段落正則修改內容
要先在 vba 的菜單上工具 - 引用 - 添加 Microsoft VBScript Regular Express 這個引用才能用
Sub add_caption()
Dim title As String
'正則
Dim regExp As New regExp
regExp.Pattern = "^圖(.*)[\d ]*?(.*?)(?<!。)$"
Application.ScreenUpdating = False
For Each par In ActiveDocument.Paragraphs
If regExp.test(par) Then
title = " " & regExp.Replace(par, "$1")
Selection.InsertCaption Label:="圖", TitleAutoText:="", title:=title, _
Position:=wdCaptionPositionAbove, ExcludeLabel:=0
End If
Next
Application.ScreenUpdating = True
End Sub
批量修改表名格式(表名在表上方)
設置樣式為 "題注"
Sub setTableNameStyle()
For Each aTable In ActiveDocument.Tables
With aTable.Range
.Collapse Direction:=wdCollapseStart
.Move Unit:=wdParagraph, Count:=-1
.Select
.Style = "題注"
End With
Next aTable
End Sub
批量修改圖片名格式(圖片名在圖片下方)
設置樣式為 "題注"
Sub setImageNameStyle()
For Each iShape In ActiveDocument.InlineShapes
With iShape.Range
.Collapse Direction:=wdCollapseStart
.Move Unit:=wdParagraph, Count:=1
.Select
.Style = "題注"
End With
Next iShape
End Sub
題注
批量添加表名題注(表名在表上方)
Sub setTableName()
For Each aTable In ActiveDocument.Tables
With aTable.Range
.Collapse Direction:=wdCollapseStart
.Move Unit:=wdParagraph, Count:=-1
.Select
.Style = "正文"
End With
Selection.InsertCaption Label:="表", TitleAutoText:="", title:=" ", _
Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.Text = ""
Next aTable
End Sub
交叉引用
給每個表的題注添加交叉引用
寫文檔是經常遇到表格上面一段是表名的 “題注”,再上面一段的結尾是 “如表 x-x”,這里的 “表 x-x” 是” 表的題注的交叉引用 “,下面是一個自動添加這種交叉引用的例子:
Sub add_cr_of_caption()
Dim i
i = 1
For Each aTable In ActiveDocument.InlineShapes
With aTable.Range
.Collapse Direction:=wdCollapseStart
.Select
End With
Selection.MoveLeft Unit:=wdCharacter, Count:=1
'交叉引用前面的字
Selection.TypeText Text:=",如"
Selection.InsertCrossReference ReferenceType:="圖", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:=i, InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
'交叉引用后面的字
Selection.TypeText Text:="。"
i = i + 1
Next aTable
End Sub
樣式批量導入
樣式 - 樣式管理 - 導入 / 導出 - 選擇兩個文件 - 選擇樣式 - 復制
正則表達式
幫助 - 搜索 "正則"
清除空白頁眉頁腳橫線
選中空白頁眉頁腳 - 清除格式
插入帶樣式的代碼
從別處復制
查看全部格式標記
設置 - 顯示 - 查看全部格式標記
修訂
修訂的簡單標記會在左邊顯示紅線,點擊紅線會查看修訂的詳細情況
想要不顯示左邊的紅線選無標記就行
參考
News, Tips, and Advice for Technology Professionals - TechRepublic