vba 在 Word 中选择一个段落并更改其样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16641999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:11:16  来源:igfitidea点击:

Select a paragraph in Word and change it's style

vbams-wordword-vba

提问by user13267

I need to find if a line in my Word document starts with the word Picture, and if it does, change the Style of that line to a built in style. I believe a line that begins after a return key is interpreted by Word as new paragraph and the end of this paragraph is signified by another return key.

我需要查找 Word 文档中的一行是否以单词 Picture 开头,如果是,则将该行的样式更改为内置样式。我相信在返回键之后开始的一行被 Word 解释为新段落,而该段落的结尾由另一个返回键表示。

I have a single sentence paragraph, whose style I have to change if it begins with the word Picture. How can I search every line of the document to see if it begins with the word Picture?
--EDIT--
I have changed the word Picture into Figure
I'm trying to find all instances of figure and then convert the line containing figure into boldface (the boldface code will be replaced by code that changes style). For now I have skipped all error checks, and am trying to get it to find all instances of Figure and then convert the sentence characters into boldface, starting from the point where the word Figure is present.

我有一个句子段落,如果它以单词 Picture 开头,我必须改变它的风格。如何搜索文档的每一行以查看它是否以单词 Picture 开头?
--EDIT--
我已将单词 Picture 更改为 Figure
我正在尝试查找 figure 的所有实例,然后将包含 figure 的行转换为粗体(粗体代码将被更改样式的代码替换)。现在我已经跳过了所有错误检查,并试图让它找到 Figure 的所有实例,然后从出现 Figure 一词的点开始将句子字符转换为粗体。

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 

Here's a screenshot of the document
enter image description hereIt just detects the first Figure in the line shown in the red circle.
I found the code keeps detecting the first instance of Figure from the current position of the insertion point over and over again.
How do I search for the next instance of Figure after detecting one of them?
Changing wdFindStop to wdFindContinue does not work; both produce the same result.

这是文档的屏幕截图
在此处输入图片说明它只是检测到红色圆圈中显示的行中的第一个图。
我发现代码不断地从插入点的当前位置一遍又一遍地检测 Figure 的第一个实例。
检测到其中一个后,如何搜索 Figure 的下一个实例?
将 wdFindStop 更改为 wdFindContinue 不起作用;两者产生相同的结果。

回答by Kazimierz Jawor

I made some modification of your code which searches from first to last. I also change the way you could select the whole sentence to mark it bold.

我对您的代码进行了一些修改,从头到尾搜索。我还改变了选择整个句子的方式,将其标记为粗体。

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

回答by user3204804

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

回答by macropod

Try the following macro; it will process paragraphs beginning with either 'Picture' or 'Figure', applying Word's 'Strong' Style (a bold font) to them.

试试下面的宏;它将处理以“图片”或“图形”开头的段落,对它们应用 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