用word vba 最经常的一个操作就是对word文档中的文本内容进行修改、删除、添加等。
本文将举例说明各种情况的文本内容的修改、删除、添加,其中段落分隔符为一个字符。
1.在word文档的开头添加文本内容
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Range(0, 0)
With oRng
'在文档的开头插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertBefore "test" & Chr(13)
'获取现在的oRng对象的文本内容,含刚才插入的内容。
sText = .Text
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Range(0, 0)
With oRng
'在文档的开头插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertBefore "test" & Chr(13)
'获取现在的oRng对象的文本内容,含刚才插入的内容。
sText = .Text
Debug.Print sText, Len(sText)
End With
End Sub
2.在word文档的末尾添加文本内容
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Content
With oRng
'在文档的末尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取文档末尾的5个字符,最后一个字符为段落分隔符,看是否是刚刚插入的test。
sText = Right(.Text, 5)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Content
With oRng
'在文档的末尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取文档末尾的5个字符,最后一个字符为段落分隔符,看是否是刚刚插入的test。
sText = Right(.Text, 5)
Debug.Print sText, Len(sText)
End With
End Sub
3.在某个段落的开头添加文本内容
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'在文档的第2个段落的开头插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertBefore "test"
'获取当前oRng对象的前4个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'在文档的第2个段落的开头插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertBefore "test"
'获取当前oRng对象的前4个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
4.在某个段落的结尾添加文本内容
添加的内容还在当前段落:
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'重置为不包含最后一个结尾段落分隔符
.SetRange oRng.Start, oRng.End - 1
'在文档的第2个段落的结尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Right(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'重置为不包含最后一个结尾段落分隔符
.SetRange oRng.Start, oRng.End - 1
'在文档的第2个段落的结尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Right(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
添加的内容在下一个段落的开头:
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'在文档的第2个段落的结尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Right(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Set oRng = oDoc.Paragraphs(2).Range
With oRng
'在文档的第2个段落的结尾插入插入字符,oRng对象将扩展为包含新插入的的字符
.InsertAfter "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Right(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
5.在某个表格的开头插入文本内容
在某个表格内,第一个单元格字符前插入文本内容:
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Dim oTable As Table
Set oDoc = Word.ActiveDocument
Set oTable = oDoc.Tables(1)
Set oRng = oTable.Range
With oRng
'在表格的第一个单元格内的字符前插入字符
.InsertBefore "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Dim oTable As Table
Set oDoc = Word.ActiveDocument
Set oTable = oDoc.Tables(1)
Set oRng = oTable.Range
With oRng
'在表格的第一个单元格内的字符前插入字符
.InsertBefore "test"
'获取当前oRng对象的后4个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
在某个表格外,在第一个单元格字符的前面插入文本内容:
Visual Basic
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Dim oTable As Table
Set oDoc = Word.ActiveDocument
Set oTable = oDoc.Tables(1)
Set oRng = oTable.Range
With oRng
.SetRange .Start - 1, .Start - 1
'在表格外第一个单元格内的字符前插入字符
.InsertBefore "test"
'获取当前oRng对象的4前个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
Sub QQ1722187970()
Dim oRng As Range
Dim oDoc As Document
Dim oTable As Table
Set oDoc = Word.ActiveDocument
Set oTable = oDoc.Tables(1)
Set oRng = oTable.Range
With oRng
.SetRange .Start - 1, .Start - 1
'在表格外第一个单元格内的字符前插入字符
.InsertBefore "test"
'获取当前oRng对象的4前个字符,看是否是刚刚插入的test字符
sText = Left(.Text, 4)
Debug.Print sText, Len(sText)
End With
End Sub
本页共216段,4761个字符,6955 Byte(字节)