word文档中可以插入文本框。
如果要遍历word文档中的文本框可以先遍历word中的shape对象,然后通过shape对象的type属性判断是否等于msoTextBox,如果是,则表示为文本框。
之后可以用TextFrame属性返回文本框的文字部分对象,并对其进行操作。
如下的代码:
Sub QQ1722187970()
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
Dim oSP As Shape
With oDoc
For Each oSP In .Shapes
With oSP
'如果类型是文本框则...
If .Type = msoTextBox Then
Debug.Print oSP.Name
'修改文本框中的文本
.TextFrame.TextRange.Text = "测试"
End If
End With
Next
End With
End Sub
以上代码是word vba,如果要在excel vba中遍历word文档的文本框可以使用如下的代码:
Sub QQ1722187970()
Const msoTextBox = 17
Dim oWord
Set oWord = VBA.CreateObject("word.application")
oWord.Visible = True
sPath = Excel.ThisWorkbook.Path & "\test.docx"
Dim oDoc
Set oDoc = oWord.Documents.Open(sPath)
With oDoc
For Each oSP In .Shapes
With oSP
'如果类型是文本框则...
If .Type = msoTextBox Then
Debug.Print .Name
'修改文本框中的文本
.TextFrame.TextRange.Text = "测试"
End If
End With
Next
End With
End Sub


发表评论