所以,有几种方法可以让代码更具可扩展性或可重用性。您可以使用通配符搜索来最小化实际需要的搜索数量。或者你可以把你的文本字符串放到一个你循环的数组中,以使实际代码保持最小。为了您的目的,并尽可能使这一点尽可能清楚,我没有这样做。这只需要您的搜索,并使其实际搜索并替换,以便仅在找到文本时才进行更改。为了将搜索限制在文本上,我添加了特殊的“^ p”查找序列。这将搜索您的文本,然后是段落中断。这并不完美,但它应该更接近你要找的东西。如果您仍然在运行此应用程序后看到仅应用了标题2,则可能需要在您的问题中包含文档的一部分文本,以确切地说明它的外观。
Sub QOS_Headings()
Dim objDoc As Document
Dim head1 As Style, head2 As Style, head3 As Style, head4 As Style
'
' QOS_Headings Macro
' Converts section headings in eCTD to usable navigation headings in Word.
'
' Using variables here just simplifies the typing further on, and allows
' you to easily change, for instance, "Heading 4" to "My Personal Heading 4"
' if you were creating your own styles.
Set objDoc = ActiveDocument
' This code does *NOT* protect against the possibility that these styles don't
' appear in the document. That's probably not a concern with built-in styles,
' but be aware of that if you want to expand upon this for other uses.
Set head1 = ActiveDocument.Styles("Heading 1")
Set head2 = ActiveDocument.Styles("Heading 2")
Set head3 = ActiveDocument.Styles("Heading 3")
Set head4 = ActiveDocument.Styles("Heading 4")
' This searches the entire document (not including foot/endnotes, headers, or footers)
' for your text string. Putting "^p" at the end of the string limits it to text strings
' that fall at the end of a paragraph, which is likely the case as your headings sit on
' their own line. You might want to experiment with that. Note that putting ^p at the
' beginning of the text will NOT work; that will apply your style to the previous
' paragraph as well.
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2^p"
With .Replacement
.ClearFormatting
.Style = head1
End With
' Here we do the actual replacement. Based on your requirements, this only replaces the
' first instance it finds. You could also change this to Replace:=wdReplaceAll to catch
' all of them.
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S^p"
With .Replacement
.ClearFormatting
.Style = head2
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.1^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.2^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.3^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4.1^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4.2^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4.3^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4.4^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.4.5^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.6^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.S.7^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P^p"
With .Replacement
.ClearFormatting
.Style = head2
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.1^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.2^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.3^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.4^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.1^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.2^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.3^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.4^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.5^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.5.6^p"
With .Replacement
.ClearFormatting
.Style = head4
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.6^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.7^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.P.8^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.A^p"
With .Replacement
.ClearFormatting
.Style = head2
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.A.1^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.A.2^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.A.3^p"
With .Replacement
.ClearFormatting
.Style = head3
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
With objDoc.Content.Find
.ClearFormatting
.Text = "3.2.R^p"
With .Replacement
.ClearFormatting
.Style = head2
End With
.Execute Wrap:=wdFindContinue, Format:=True, Replace:=wdReplaceOne
End With
End Sub
最后一个建议:开始使用VBA编程的一种方法是使用宏记录器。这并不完美,但它会为您提供基本结构,例如,如果您记录自己在做一个搜索和替换,就会进行搜索和替换。
你确定所有这些文本串中实际存在的文件?在应用样式之前,您没有检查它们是否被发现。 – Christina
嗨克里斯蒂娜。 感谢您的回复。如果你的意思是这些3.2 *号码存在于他的文档中,那么他们确实是这样。正如我所说,我对这个编程的东西很陌生。 )。从本质上讲,我试图挑选出总是在这些文档中的数字,然后将它们格式化为分层标题样式,而仅将这些文本留在部分内。我是否必须告诉程序选择要搜索的整个文档?您可以提供的任何信息都会有所帮助。 – DP7
每个数字是否只出现一次,并且它们是否出现在他们自己的行上? – Christina