要用vba在空白的word文档中逐段落的添加文本内容,可以使用word的Selection对象的TypeText方法和TypeParagraph方法。
其中TypeText方法是在当前光标所在的位置入插要添加的文本内容,TypeParagraph方法是在当前光标所在的位置插入一个新的空白的段落。
要在空白的word文档中逐段落的添加文本内容,可以先用TypeText方法插入文本,然后再用TypeParagraph方法插入下一个段落,如此循环即可。
以下是一个在word中使用vba在空白的word文档中逐段落的添加文本内容的代码:
Sub QQ1722187970()
Dim arr
arr = Array("我和你", "exceloffice", "test")
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
With Word.Selection
For I = 0 To UBound(arr)
.TypeText arr(I)
.TypeParagraph
Next I
End With
MsgBox "处理完成!!!"
End Sub
如果要在excel中调用word vba可以使用如下的代码:
Sub QQ1722187970()
Set oWord = VBA.CreateObject("word.application")
oWord.Visible = True
Dim arr
arr = Array("我和你", "exceloffice", "test")
Set oDoc = oWord.Documents.Add
With oWord.Selection
For I = 0 To UBound(arr)
.TypeText arr(I)
.TypeParagraph
Next I
End With
Set oDoc = Nothing
Set oWord = Nothing
MsgBox "处理完成!!!"
End Sub


发表评论