Word图注排版技巧:编号与内容间空格处理
word插入图注,保证图注编号与内容之间空一个半角空格,保证每次打开word后插入图注时有图、表默认标签
直接上干货!!!
启发来源于这个文章:
Word插入题注优化(自动删除标签与编号前的空格,编号后添加空格).
Sub InsertCaption() '修改系统插入“题注”命令'功能:自动删除标签与编号间的空格(英文除外),并在题注数字后添加一个空格;'同时自动创建“图”“表”两个题注标签'适用于:Word 2003 - 2013,不兼容WPS文字!Dim Lab As String, startPt As Long, endPt As Long, myrang As Range'每次打开“插入题注”对话框前,先确保有“图”“表”两个标签Call EnsureCaptionLabel("图")Call EnsureCaptionLabel("表")Call EnsureCaptionLabel("Fig")Call EnsureCaptionLabel("Tab")startPt = Selection.Start 'startPt标注起始点If Application.Dialogs(357).Show = -1 Then '插入“题注”对话框秀出来,如果按确定结束时执行以下程序Application.ScreenUpdating = False '关闭屏幕更新Lab = Dialogs(357).LabelendPt = Selection.Start 'endPt标记插入的题注部分终点Selection.Start = startPt '选定插入的整个题注'删除标签与编号间的空格(英文后的保留)With Selection.Find.Text = Lab & " ".Forward = True.MatchWildcards = FalseIf Lab Like "*[0-9a-zA-Z.]" Then'如果标签最后一个字符是英文、数字或点号,则保留空格Else.Replacement.Text = Lab.Execute Replace:=wdReplaceOneendPt = endPt - 1Selection.End = endPtEnd IfEnd With'在题注数字后添加一个空格Selection.Fields.ToggleShowCodes '切换域代码,这样才能用^d查找域With Selection.Find.Text = "^d".Replacement.Text = "^& ".Forward = False.MatchWildcards = False.Execute Replace:=wdReplaceOneEnd With'选定整个插入的题注内容,将域代码切换回来endPt = endPt + 1With Selection.Start = startPt.End = endPt.Fields.ToggleShowCodesEnd With'将光标定位至题注所在段尾处With Selection.Find.Text = "^13".Forward = True.MatchWildcards = False.Wrap = wdFindContinue.ExecuteEnd WithSelection.MoveLeft Unit:=wdCharacter, Count:=1 '定位到段尾回车前End IfApplication.ScreenUpdating = True '恢复屏幕更新End SubPrivate Sub EnsureCaptionLabel(ByVal LabelName As String)'如果题注标签不存在,则自动创建;'如果已经存在,则不重复创建,避免报错。Dim cap As captionLabelDim exists As Booleanexists = FalseFor Each cap In Application.CaptionLabelsIf cap.Name = LabelName Thenexists = TrueExit ForEnd IfNext capIf exists = False ThenApplication.CaptionLabels.Add Name:=LabelNameEnd IfEnd Sub
将创建的宏运行即可。
夜雨聆风