以编程方式添加图像和标题,带有粗体标签
Add images and captions programmatically, with bold label
我想为数字添加标题,其中章节将包含在编号中,以及文本“图 x.x”。大胆:
Figure 1.1. Sample figure.
自动字幕是不可能的,因为它只允许将名为标题 1-9 的样式视为章节,而我使用的是自定义样式。据我了解,无法将任何个性化样式添加到列表中。
请考虑到我对 VBA 的了解实际上是不存在的(我通常会尝试在多个论坛中找到类似的问题并使用指南或其他类似的解决问题来调整它),所以我的错误可能是对于那些更有经验的人来说微不足道。我可以设法编写一个宏来完成我需要的几乎所有事情,但有一件事没有按预期工作。
理想情况下,宏将:
提示用户select一张图片
插入具有特定段落样式的图像
插入包含章节编号的标题和自定义段落样式,而不是内置段落样式
搜索“图 x.x”。文本并使用通配符查找和替换将其加粗 <== 这是我遇到问题的地方
Sub PicCaption()
Dim intChoice As Integer
Dim strPath As String
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
strPath, LinkToFile:=False, _
SaveWithDocument:=True
Selection.Range.Style = "Figures"
'Add caption in the form of "Figure x.x. "
Selection.TypeParagraph
Selection.TypeText Text:="Figure "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ChapNum \n \t", PreserveFormatting:=False
Selection.TypeText Text:="."
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ Figure \* ARABIC", PreserveFormatting:=False
Selection.TypeText Text:="."
Selection.Style = ActiveDocument.Styles("Figures")
Selection.TypeText Text:=" "
'Make "Figure x.x." bold (last space not included)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = False
.Text = "Figure*.*"
.Font.Bold = False
.MatchWildcards = True
.Replacement.Text = "^&"
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceOne, Forward:=True, _
Wrap:=wdFindContinue
End With
End Sub
替换位不会使最近插入的“图x.x”。粗体,但文本中的下一个,即使我指定搜索是向后的。如果我键入 .Execute Replace:=wdReplaceOne, Forward:=False, _
,它会转到文档的末尾并向上移动,使所有内容变为粗体。
在我的示例文档中,我有多个已经加了标题的图片,但通常情况下不会出现这种情况;我想在插入字幕时格式化它们,而不是在文档完成后重新格式化它们。
我的错误在哪里,为什么,如果你能解释一下?
谢谢你。
我找到了我的答案:无论出于何种原因,一旦涉及到字段,查找和替换就不会很好地工作;即它不会在“1.1.”中正确找到句点。我尝试了使用通配符和不使用通配符,使用 ?、* 和我能想到的任何东西。
我又用了一个方法:
- Select整行
- 加粗
- 转到行尾
- 取消勾选粗体,使描述具有正常的字体宽度
'Code before this point remains identical
'Make "Figure x.x." bold (last space not included)
'Select from cursor point to beginning of line; make bold
Selection.MoveStart Unit:=wdLine, Count:=-1
Selection.Font.Bold = True
'Move cursor to end of the line; uncheck bold format
Selection.EndKey Unit:=wdLine
Selection.Font.Bold = wdToggle
这样,光标就放在字幕标签后面,粗体没有选中。看起来笨拙且非常不专业,但有效。
谢谢大家!
当 Word 插入标题时,它基本上为插入多个字段及其相关开关提供了快捷方式。
因此,如果我们插入引用章节编号标题 3 样式的图标题,我们会得到类似于
的内容
Figure 2.1.3-1: Text for the caption
如果我们在 Word 文档中突出显示 'Figure 2.1.3-1' 并按 Shift-F9 这将显示标题编号由 styleref 字段和 seq 字段组成
Figure {Styleref 3 \w}-{Seq Figure}
显示字段代码后,我们可以轻松地使用 word 的内置 Find/Replace 来更改字段括号之间的文本。所以我们可以搜索 'Styleref 3' 并将其替换为 'Styleref "Heading 2"' 或者实际上是 'Styleref "myStyle"'.
如果使用了Word通配符搜索,则可以同时将style ref更改为所需的样式并应用粗体效果,从而达到OP想要的效果。我将把它留给 OP 进行一些研究。
如果我们必须转换现有文档,这很好。如果我们在键入时插入字幕,那么最好使用宏来插入所需的字幕编号,方法是触发一个宏,该宏从一组击键中插入适当的字幕 numbering/formatting。
下面的宏将插入所需类型的标题,使用定义的章节编号样式并将粗体效果应用于所有编号,直到分隔标签。
Option Explicit
' Any Leading and Trailing spaces in the Const definition strings are deliberate
' Heading 2 is used for ease of demonstration. Heading 2 should be replaced by the style
' from which you wish to take the heading numbers.
Const SpecialCaptionStyle As String = """Heading 2""" ' Name of the style to reference for the heading number
Const CaptionType As String = "Figure " ' The trailing space is required
Const CaptionNUmberingStyle As String = " \w " ' see switches for the styleref field
Const CaptionNumberSeparator As String = "-"
Public Sub InsertSpecialCaption()
' Get the range into which we insert the styleref and seq fields
Dim myFieldRange As Word.Range
Set myFieldRange = Selection.Range
'Preserve the srarting range for later use
Dim myEffectRange As Word.Range
Set myEffectRange = Selection.Range.Duplicate
'Set the style to Caption style.
'Caption style will be applied to any text in the paragraph of the selection point
myFieldRange.Collapse direction:=wdCollapseEnd
myFieldRange.Paragraphs.Item(1).Style = myFieldRange.Document.Styles(wdStyleCaption)
'Insert the label of the caption type. In this case it is the text 'Figure'
myFieldRange.InsertAfter Text:=CaptionType
myFieldRange.Collapse direction:=wdCollapseEnd
Dim myField As Word.Field
' Insert the styleref field to obtain the heading number of the style we specify
Set myField = myFieldRange.Document.Fields.Add(Range:=myFieldRange, Preserveformatting:=False)
myField.Code.Text = "Styleref " & SpecialCaptionStyle & CaptionNUmberingStyle
Set myFieldRange = myField.Result
'Insert the text string used as a seperator between the chapter number and the captiontype number
myFieldRange.InsertAfter Text:=CaptionNumberSeparator
myFieldRange.Collapse direction:=wdCollapseEnd
' Insert the Seq field to get the sequential number of the caption
' in this case we use the same name of the label but it could be different
Set myField = myFieldRange.Document.Fields.Add(Range:=myFieldRange, Type:=wdFieldEmpty, Preserveformatting:=False)
myField.Code.Text = "Seq " & CaptionType
Set myFieldRange = myField.Result
myFieldRange.Collapse direction:=wdCollapseEnd
' Insert the seperator text from the number to the Caption text NB I always use : followed by a tab
myFieldRange.InsertAfter Text:=":" & vbTab
' Adjust the range to omit the tab from formatting
' update the fields
' Apply bold effect to the inserted caption label
myFieldRange.MoveEnd unit:=wdCharacter, Count:=-1
myEffectRange.End = myFieldRange.End
myEffectRange.Fields.Update
myEffectRange.Font.Bold = True
End Sub
所需要的只是 link 宏到合适的键序列,这是 OP 的出处。
不过,首先,我强烈建议使用 F8 逐步执行宏以查看如何插入标题编号。
我想为数字添加标题,其中章节将包含在编号中,以及文本“图 x.x”。大胆:
Figure 1.1. Sample figure.
自动字幕是不可能的,因为它只允许将名为标题 1-9 的样式视为章节,而我使用的是自定义样式。据我了解,无法将任何个性化样式添加到列表中。
请考虑到我对 VBA 的了解实际上是不存在的(我通常会尝试在多个论坛中找到类似的问题并使用指南或其他类似的解决问题来调整它),所以我的错误可能是对于那些更有经验的人来说微不足道。我可以设法编写一个宏来完成我需要的几乎所有事情,但有一件事没有按预期工作。
理想情况下,宏将:
提示用户select一张图片
插入具有特定段落样式的图像
插入包含章节编号的标题和自定义段落样式,而不是内置段落样式
搜索“图 x.x”。文本并使用通配符查找和替换将其加粗 <== 这是我遇到问题的地方
Sub PicCaption()
Dim intChoice As Integer
Dim strPath As String
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
strPath, LinkToFile:=False, _
SaveWithDocument:=True
Selection.Range.Style = "Figures"
'Add caption in the form of "Figure x.x. "
Selection.TypeParagraph
Selection.TypeText Text:="Figure "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"STYLEREF ChapNum \n \t", PreserveFormatting:=False
Selection.TypeText Text:="."
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SEQ Figure \* ARABIC", PreserveFormatting:=False
Selection.TypeText Text:="."
Selection.Style = ActiveDocument.Styles("Figures")
Selection.TypeText Text:=" "
'Make "Figure x.x." bold (last space not included)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = False
.Text = "Figure*.*"
.Font.Bold = False
.MatchWildcards = True
.Replacement.Text = "^&"
.Replacement.Font.Bold = True
.Execute Replace:=wdReplaceOne, Forward:=True, _
Wrap:=wdFindContinue
End With
End Sub
替换位不会使最近插入的“图x.x”。粗体,但文本中的下一个,即使我指定搜索是向后的。如果我键入 .Execute Replace:=wdReplaceOne, Forward:=False, _
,它会转到文档的末尾并向上移动,使所有内容变为粗体。
在我的示例文档中,我有多个已经加了标题的图片,但通常情况下不会出现这种情况;我想在插入字幕时格式化它们,而不是在文档完成后重新格式化它们。
我的错误在哪里,为什么,如果你能解释一下?
谢谢你。
我找到了我的答案:无论出于何种原因,一旦涉及到字段,查找和替换就不会很好地工作;即它不会在“1.1.”中正确找到句点。我尝试了使用通配符和不使用通配符,使用 ?、* 和我能想到的任何东西。
我又用了一个方法:
- Select整行
- 加粗
- 转到行尾
- 取消勾选粗体,使描述具有正常的字体宽度
'Code before this point remains identical
'Make "Figure x.x." bold (last space not included)
'Select from cursor point to beginning of line; make bold
Selection.MoveStart Unit:=wdLine, Count:=-1
Selection.Font.Bold = True
'Move cursor to end of the line; uncheck bold format
Selection.EndKey Unit:=wdLine
Selection.Font.Bold = wdToggle
这样,光标就放在字幕标签后面,粗体没有选中。看起来笨拙且非常不专业,但有效。
谢谢大家!
当 Word 插入标题时,它基本上为插入多个字段及其相关开关提供了快捷方式。
因此,如果我们插入引用章节编号标题 3 样式的图标题,我们会得到类似于
的内容Figure 2.1.3-1: Text for the caption
如果我们在 Word 文档中突出显示 'Figure 2.1.3-1' 并按 Shift-F9 这将显示标题编号由 styleref 字段和 seq 字段组成
Figure {Styleref 3 \w}-{Seq Figure}
显示字段代码后,我们可以轻松地使用 word 的内置 Find/Replace 来更改字段括号之间的文本。所以我们可以搜索 'Styleref 3' 并将其替换为 'Styleref "Heading 2"' 或者实际上是 'Styleref "myStyle"'.
如果使用了Word通配符搜索,则可以同时将style ref更改为所需的样式并应用粗体效果,从而达到OP想要的效果。我将把它留给 OP 进行一些研究。
如果我们必须转换现有文档,这很好。如果我们在键入时插入字幕,那么最好使用宏来插入所需的字幕编号,方法是触发一个宏,该宏从一组击键中插入适当的字幕 numbering/formatting。
下面的宏将插入所需类型的标题,使用定义的章节编号样式并将粗体效果应用于所有编号,直到分隔标签。
Option Explicit
' Any Leading and Trailing spaces in the Const definition strings are deliberate
' Heading 2 is used for ease of demonstration. Heading 2 should be replaced by the style
' from which you wish to take the heading numbers.
Const SpecialCaptionStyle As String = """Heading 2""" ' Name of the style to reference for the heading number
Const CaptionType As String = "Figure " ' The trailing space is required
Const CaptionNUmberingStyle As String = " \w " ' see switches for the styleref field
Const CaptionNumberSeparator As String = "-"
Public Sub InsertSpecialCaption()
' Get the range into which we insert the styleref and seq fields
Dim myFieldRange As Word.Range
Set myFieldRange = Selection.Range
'Preserve the srarting range for later use
Dim myEffectRange As Word.Range
Set myEffectRange = Selection.Range.Duplicate
'Set the style to Caption style.
'Caption style will be applied to any text in the paragraph of the selection point
myFieldRange.Collapse direction:=wdCollapseEnd
myFieldRange.Paragraphs.Item(1).Style = myFieldRange.Document.Styles(wdStyleCaption)
'Insert the label of the caption type. In this case it is the text 'Figure'
myFieldRange.InsertAfter Text:=CaptionType
myFieldRange.Collapse direction:=wdCollapseEnd
Dim myField As Word.Field
' Insert the styleref field to obtain the heading number of the style we specify
Set myField = myFieldRange.Document.Fields.Add(Range:=myFieldRange, Preserveformatting:=False)
myField.Code.Text = "Styleref " & SpecialCaptionStyle & CaptionNUmberingStyle
Set myFieldRange = myField.Result
'Insert the text string used as a seperator between the chapter number and the captiontype number
myFieldRange.InsertAfter Text:=CaptionNumberSeparator
myFieldRange.Collapse direction:=wdCollapseEnd
' Insert the Seq field to get the sequential number of the caption
' in this case we use the same name of the label but it could be different
Set myField = myFieldRange.Document.Fields.Add(Range:=myFieldRange, Type:=wdFieldEmpty, Preserveformatting:=False)
myField.Code.Text = "Seq " & CaptionType
Set myFieldRange = myField.Result
myFieldRange.Collapse direction:=wdCollapseEnd
' Insert the seperator text from the number to the Caption text NB I always use : followed by a tab
myFieldRange.InsertAfter Text:=":" & vbTab
' Adjust the range to omit the tab from formatting
' update the fields
' Apply bold effect to the inserted caption label
myFieldRange.MoveEnd unit:=wdCharacter, Count:=-1
myEffectRange.End = myFieldRange.End
myEffectRange.Fields.Update
myEffectRange.Font.Bold = True
End Sub
所需要的只是 link 宏到合适的键序列,这是 OP 的出处。
不过,首先,我强烈建议使用 F8 逐步执行宏以查看如何插入标题编号。