用段落替换新行
Replace new lines with paragraphs
我需要在 Word 宏 中执行以下操作。
我需要浏览 Word 文档并根据参数更改某些段落。如果段落的字体大小为 19.5,则该段落的样式必须为标题 1。下一段将为标题 2,下一段为标题 3。其他文本的样式仍为 "Normal"。
编写了以下宏:
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
问题是有时文本包含一个段落,有时只是换行。试图弄清楚用段落替换所有新行。非常感谢任何帮助。
谢谢!
听起来你指的是整个文档:"replace all new lines with paragraphs"
ActiveDocument.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll
注意:您的代码经常使用 ActiveDocument
。将其分配给变量会更有效 更安全 :
Dim doc as Word.Document
Set doc = ActiveDocument
doc.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll
我需要在 Word 宏 中执行以下操作。
我需要浏览 Word 文档并根据参数更改某些段落。如果段落的字体大小为 19.5,则该段落的样式必须为标题 1。下一段将为标题 2,下一段为标题 3。其他文本的样式仍为 "Normal"。
编写了以下宏:
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
问题是有时文本包含一个段落,有时只是换行。试图弄清楚用段落替换所有新行。非常感谢任何帮助。
谢谢!
听起来你指的是整个文档:"replace all new lines with paragraphs"
ActiveDocument.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll
注意:您的代码经常使用 ActiveDocument
。将其分配给变量会更有效 更安全 :
Dim doc as Word.Document
Set doc = ActiveDocument
doc.Content.Find.Execute FindTExt:="^l", ReplaceWith:="^p", Replace:=wdReplaceAll