VBA 在word文档中插入新行
VBA to insert new line in a word document
我有以下代码可以在 word 文档中插入行。我想输入 3 个空白行来打印 Line2。如何编码 VBA 在 word 文档中插入 3 个空行,以便我可以在之后打印 Line2。
Line1 = "Line1 hello"
Line2 = "Line2 hello"
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
' objWord.ActiveDocument.Styles ("Heading 2")
Set objSelection = objWord.Selection
objSelection.Font.Bold = True
objSelection.Font.Name = "Arial"
objSelection.TypeText (Line1)
这是一种方法
Public Sub MakeWordDoc()
Dim wd As Word.Application
Dim doc As Word.Document
Dim i As Long
Set wd = New Word.Application
Set doc = wd.Documents.Add
wd.Visible = True
doc.Paragraphs.Add.Range.Text = "Line 1 hello"
For i = 1 To 4
doc.Paragraphs.Add
Next i
doc.Paragraphs.Add.Range.Text = "Line 2 hello"
End Sub
我不知道为什么我必须添加四个段落才能得到三个空行,但是当我在最后一段添加文本时,它会删除之前的空白。
这是另一种方式
Public 子 MakeWordDoc()
Dim wd As Word.Application
Dim doc As Word.Document
Dim para As Word.Paragraph
Dim i As Long
Set wd = New Word.Application
Set doc = wd.Documents.Add
wd.Visible = True
Set para = doc.Paragraphs.Add
para.Range.Text = "Line 1 hello"
para.Range.ParagraphFormat.SpaceAfter = 18
doc.Paragraphs.Add
doc.Paragraphs.Add.Range.Text = "Line 2 hello"
End Sub
您可以随心所欲地制作 18 - 以点为单位。同样,我不得不为第 2 行添加一个空白段落才能吃完,我不明白。希望这足以让您自行解决细节问题。
我有以下代码可以在 word 文档中插入行。我想输入 3 个空白行来打印 Line2。如何编码 VBA 在 word 文档中插入 3 个空行,以便我可以在之后打印 Line2。
Line1 = "Line1 hello"
Line2 = "Line2 hello"
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
' objWord.ActiveDocument.Styles ("Heading 2")
Set objSelection = objWord.Selection
objSelection.Font.Bold = True
objSelection.Font.Name = "Arial"
objSelection.TypeText (Line1)
这是一种方法
Public Sub MakeWordDoc()
Dim wd As Word.Application
Dim doc As Word.Document
Dim i As Long
Set wd = New Word.Application
Set doc = wd.Documents.Add
wd.Visible = True
doc.Paragraphs.Add.Range.Text = "Line 1 hello"
For i = 1 To 4
doc.Paragraphs.Add
Next i
doc.Paragraphs.Add.Range.Text = "Line 2 hello"
End Sub
我不知道为什么我必须添加四个段落才能得到三个空行,但是当我在最后一段添加文本时,它会删除之前的空白。
这是另一种方式 Public 子 MakeWordDoc()
Dim wd As Word.Application
Dim doc As Word.Document
Dim para As Word.Paragraph
Dim i As Long
Set wd = New Word.Application
Set doc = wd.Documents.Add
wd.Visible = True
Set para = doc.Paragraphs.Add
para.Range.Text = "Line 1 hello"
para.Range.ParagraphFormat.SpaceAfter = 18
doc.Paragraphs.Add
doc.Paragraphs.Add.Range.Text = "Line 2 hello"
End Sub
您可以随心所欲地制作 18 - 以点为单位。同样,我不得不为第 2 行添加一个空白段落才能吃完,我不明白。希望这足以让您自行解决细节问题。