我需要找到我的Word文档中的一行是否以单词Picture开头,如果是,则将该行的样式更改为内置样式。我相信在返回键之后开始的行被Word解释为新段落,并且该段落的结尾由另一个返回键表示。
我有一个句子段,如果它以单词Picture开头,我必须更改它的样式。如何搜索文档的每一行以查看它是否以单词Picture开头? - 编辑 - 我已将图片改为图 我试图找到图形的所有实例,然后将包含图形的线转换为粗体(粗体代码将被更改样式的代码替换)。现在我已经跳过所有的错误检查,并试图让它找到图的所有实例,然后将句子字符转换为粗体,从图中出现单词的位置开始。
Sub Macro1()
' ' Macro1 Macro ' '
Selection.Find.ClearFormatting
Do
With Selection.Find
.Text = "figure"
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Loop
End Sub
这是该文档的屏幕截图 它只是检测红色圆圈中显示的第一个图。 我发现代码一遍又一遍地从插入点的当前位置检测到图的第一个实例。 在检测到其中一个后,如何搜索下一个图实例? 将wdFindStop更改为wdFindContinue不起作用;两者都产生相同的结果。
我对你的代码做了一些修改,从头到尾搜索。我也改变了你选择整个句子的方式来标记它的粗体。
Sub Macro1()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "figure"
.Forward = True
.Wrap = wdFindStop
End With
'changed the loop
Do While Selection.Find.Execute
'changed the way we select sentence
Selection.Expand wdSentence
Selection.Font.Bold = True
Selection.Collapse wdCollapseEnd
Loop
End Sub
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Picture"
.Replacement.Text = "Figure"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll
尝试以下宏;它将处理以“图片”或“图片”开头的段落,将Word的“强”样式(粗体字)应用于它们。
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13[FP]i[gct]{1,2]ure [0-9]*^13"
.Replacement.Text = ""
.Forward = True
.Format = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found = True
.Start = .Start + 1
.Style = "Strong"
.Collapse (wdCollapseEnd)
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub