所以,有幾種方法可以讓代碼更具可擴展性或可重用性。您可以使用通配符搜索來最小化實際需要的搜索數量。或者你可以把你的文本字符串放到一個你循環的數組中,以使實際代碼保持最小。爲了您的目的,並儘可能使這一點儘可能清楚,我沒有這樣做。這隻需要您的搜索,並使其實際搜索並替換,以便僅在找到文本時才進行更改。爲了將搜索限制在文本上,我添加了特殊的「^ 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