900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > word中通过宏对某章节下图片批量插入题注

word中通过宏对某章节下图片批量插入题注

时间:2019-10-10 05:08:50

相关推荐

word中通过宏对某章节下图片批量插入题注

提前定义好题注的样式,一般为图-章节编号-序号 题注标题。

将鼠标焦点选中到在某一个章节内容的第一段,然后执行以下宏脚本,即可自动批量生成图片的题注,题注的标题自动为该章节的标题,并根据图片的顺序在标题中自动加序号。

宏脚本如下:

‘获取当前位置的章节标题

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。