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
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值