提前定义好题注的样式,一般为图-章节编号-序号 题注标题。
将鼠标焦点选中到在某一个章节内容的第一段,然后执行以下宏脚本,即可自动批量生成图片的题注,题注的标题自动为该章节的标题,并根据图片的顺序在标题中自动加序号。
宏脚本如下:
‘获取当前位置的章节标题
Function GetListString()
Dim lngNumOfParagraphs As Long
Dim strListValue As String
On Error Resume Next
Do
If Err.Number Then Exit Do
lngNumOfParagraphs = lngNumOfParagraphs + 1
If (Selection.Previous(wdParagraph, lngNumOfParagraphs).Paragraphs.OutlineLevel <> wdOutlineLevelBodyText) Then
strListValue = Selection.Previous(wdParagraph, lngNumOfParagraphs).Paragraphs(1).Range.Text
GoTo Report_ListValue
End If
Loop
Exit Function
Report_ListValue:
' MsgBox "The selected table is in chapter: " & strListValue
strListValue = Left(strListValue, Len(strListValue) - 1)
GetListString = strListValue
End Function
Sub Example()
Dim titleA As String '初始章节标题
Dim titleB As String '当前图片所在章节标题
Dim s
Dim t
t = 0
titleA = GetListString()
For s = 1 To 10
'查找下一个图片
With Selection.Find
.ClearFormatting
.Text = "^g"
.Execute Forward:=True
End With
titleB = GetListString()
'如果图片已切换章节,则退出
If (titleB <> titleA) Then
Exit For
' t = 0
' titleA = titleB
End If
'判断是否为图片
If Selection.Type = 7 Then
'在图片下方换行
Selection.MoveRight
Selection.TypeParagraph
'输入题注标题
t = t + 1
Selection.TypeText titleA & t
'插入题注
Selection.HomeKey Unit:=wdLine
Selection.InsertCaption Label:="图", TitleAutoText:="InsertCaption3", _
title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
'题注居中
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next s
End Sub