我有一个文档,我想从第3节开始搜索到文档的末尾,并根据我的用户定义样式来加粗文本,因为使用的其他样式可能包含我不想加粗的相同单词-包括标题/节的可能性。
我确实有一些VBA代码,它可以在整个文档中查找我的用户定义的样式,并只在一个样式匹配时将其加粗。然而,.find中的.Style不允许指定通配符样式,即我的用户定义的样式集"Requirement1 to Requirement9“,它基于Heading1-9样式。我假设我将不得不在"with“之外创建一个for循环来查找我的用户定义的样式,该样式一次传递一个来加粗文本,以一次匹配一个样式,即Requirement1到Requirement9。有没有更好的方法?请参阅下面的代码。
下一个问题是将此文本的搜索重点放在粗体上,以便从特定部分开始搜索到文档的结尾。对我来说,它的第3节一直到文档的末尾,或者直到它遇到另一个定义为Appendix1和stop的样式。我确实有用户定义的样式Appendix1到Appendix9,也是基于标题1-9。搜索样式作为起点的原因是因为section3之前的这些其他部分,或者第3节及以后的描述性文本可能包含我试图加粗的单词-我不想这样做,除非在第3节及以后的部分中应用了我特定的用户定义样式请求1-9。在进行单词搜索和加粗之前尝试实现这一部分时,我一直在基于" Heading1“样式进行搜索,当我找到第三个部分时,我知道我在第3节中。我不能使用subsections搜索,因为它包括文档的所有子部分,这可能会因文档而异,指定sections.item( 3 )并不意味着我将获得与第三种Heading1样式相对应的文档的第三部分。
因为我对VBA的各个部分、段落、句子、字符和其他所有东西都不是很熟练,所以在应用“范围”之前、之后或两者时(似乎从来都不会以人们期望的方式工作。甚至VBA提示工具也说可以这样做,但运行时显示错误)我从指定整个activedocument.range的开始/结束开始。然后,我在for循环中执行了三次"Heading1“样式的".find”,它确实找到了它们。然后,我尝试将最后一个查找找到的范围值指定为起始值,以便在下一个嵌入式“Heading1”中使用,它将搜索并将粗体应用于"Requirement1“样式。(可能需要创建一个循环来搜索Requirement1-9样式,如上所述)。为了帮助我知道我所处的位置,我尝试通过debug.print转储范围位置,但它将整个文档文本转储到调试窗口,而不是我认为会得到的整数值。这样做有什么问题吗?我甚至尝试过使用selection.range并将检索到的文本转储到debug.print窗口,但没有成功。这使情况变得更糟,因为处理时间似乎永远都在运行。wdFindStop也不会强制查找在第三个查找时停止。
那么如何: a)。查找范围位置并将其与此位置的文本一起转储到调试窗口。数字和文本都会告诉我我在哪里。c)。如何根据文档中第三个"Heading1“位置的查找来分配范围位置,以便第二个"with”可以搜索并将粗体应用于我想要加粗的文本。d)。如何在.Style用户定义的Requirement1-9样式上执行通配符搜索,而不是创建一个for循环来一次搜索一个?
以下是我编写的VBA代码。
Attribute VB_Name = "BoldMustShall2_M"
Option Explicit
Public Sub BoldMustShall2()
' If .Parent.Bold = True is used with wdReplaceall, the whole
' document is bolded even when the sentence doesn't have any of the words
' being searched for.
Dim myRange, rngSel As Range
Dim oDoc As Word.Document
Dim pos1, pos2 As Long
Dim numchars As Long
Set oDoc = ActiveDocument
Set rngSel = Selection.Range
Application.ScreenUpdating = False
'Set the starting Heading to search as Section 3
pos1 = oDoc.Range.Start
pos2 = oDoc.Range.End
Set myRange = oDoc.Range(Start:=pos1, End:=pos2)
'Another way is to find the "Heading1" style and set the range start to the third one found which is section 3.
'Counting Word sections can vary alot so its not the best way.
'The same would apply if Appendix1 style is used and assuming that requiremens start in the third one.
Debug.Print "My Start Range " + myRange
With myRange
.TextRetrievalMode.IncludeFieldCodes = False ' don't want to search fieldcodes for must/shall
.TextRetrievalMode.IncludeHiddenText = False ' don't want to search hiddentext for must/shall
' Get the range position for Heading1 style for section 3 of PRD.
Dim i As Integer
Dim ReqHDR As Range
Dim bFind As Boolean
With .Find
.ClearFormatting
.Forward = True
'.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.Style = "Heading1"
'Loop three times for Style
For i = 1 To 3 Step 1
'bFind = .Execute
.Execute Wrap:=wdFindStop
If .Found = True Then
rngSel = oDoc.Range 'This should be the current position in the Document for the requirements.
' When found it outputs the message once
Debug.Print "Found a Heading1 style "
.Replacement.Font.Size = 20
End If
'.Wrap = wdFindStop
Next i
End With
Debug.Print "End of Heading1 Search "
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.Replacement.Font.Bold = True
.Replacement.Font.Name = "Times New Roman"
.Replacement.Font.Size = 12
.Replacement.Font.Italic = True
'.Style = "Requirement1" ' A loop is needed to cycle thru all the Requirement1-9 styles. No wildcard.
'.Style = wdStyleNormal
.Replacement.Text = "^&" ' This is the contents of the find what box in word.
' In this case the .find.text "must" statement.
' Alternate is to specify "must" or "shall" but this
' would require two replace.text statements instead of
' just this one. The ^& is a special command that eliminates
' the need to set the replacement.Text info.
'.Text = "must"
.Execute FindText:="must", Replace:=wdReplaceAll
If .Found = True Then
' When found it outputs the message once
Debug.Print "Found one or more must "
End If
'.Text = "shall"
.Execute FindText:="shall", Replace:=wdReplaceAll
If .Found = True Then
' When found it outputs the message once
Debug.Print "Found one or more shall "
End If
End With
End With
Application.ScreenUpdating = True
Debug.Print "Completed searching for must/shall in document."
End Sub发布于 2021-06-04 09:00:35
假设,当您引用“节”时,您指的是文档中由节分隔符描述的部分,可能是这样的内容:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
With ActiveDocument
Set Rng = .Range(.Sections(3).Range.Start, .Range.End)
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Format = True
.Wrap = wdFindContinue
.Style = "Appendix1"
.Execute
End With
Rng.End = .Start
End With
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = True
.Replacement.Style = "Strong"
.Wrap = wdFindStop
.Replacement.Text = "^&"
.Text = "must"
For i = 1 To 9
.Style = "Requirement" & i
.Execute Replace:=wdReplaceAll
Next
.Text = "shall"
For i = 1 To 9
.Style = "Requirement" & i
.Execute Replace:=wdReplaceAll
Next
End With
End With
Application.ScreenUpdating = True
End Sub请注意,我没有使用硬格式替换,而是简单地应用了Word内置的“强”样式。你自己的代码也应该使用一种样式,无论是Word的“强”样式还是你自己定义的另一种字符样式。
https://stackoverflow.com/questions/67828699
复制相似问题