2016-04-23 16 views
0

Wordマクロで次の操作を行う必要があります。Word VBAマクロ:新しい行を段落に置き換えます。

私はWord文書を読み、そのパラメタに基づいて特定の段落を変更する必要があります。段落のフォントサイズが19.5の場合、段落はスタイル見出し1を取得する必要があります。次の段落は見出し2になり、次の段落は見出し3になります。他のテキストはスタイル「標準」になります。

は、次のマクロを書いた:

Sub styles_temp() 

' Declare a paragraph 
Dim p As Paragraph 

' Declare the current size. 
Dim currentSize As Single 

'Iterate through the text and print each paragraph 
For Each p In ActiveDocument.Paragraphs 

' Determine current size of the paragraph 
currentSize = p.Range.Font.Size 

' If size is 19.5, it will be Heading 1 
If currentSize = 19.5 Then 

    p.Range.Style = ActiveDocument.Styles("Heading 1") 

    ' Next Line is Heading 2 
    p.Next.Range.Style = ActiveDocument.Styles("Heading 2") 


ElseIf p.Range.Style = "Heading 2" Then 
    p.Next.Range.Style = ActiveDocument.Styles("Heading 3") 

End If 

Next p 

End Sub 

問題は時々テキストは、段落、時にはちょうど新しい行が含まれていることです。すべての新しい行を段落に置き換えることを試みること。助けていただければ幸いです。

ありがとうございました!

ActiveDocument.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll 

注意「の項で、すべての新しい行を置き換える」:あなたのコードはActiveDocument多くを使用している

答えて

1

は、それはあなたが文書全体を意味するように聞こえます。これは、変数にこれを割り当てるために、より効率的かつより安全になります。あなたの応答のための

Dim doc as Word.Document 
Set doc = ActiveDocument 
doc.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll 
+0

多くの感謝! –

関連する問題