Vba代码读取word数据
时间: 2023-08-29 22:13:06 浏览: 125
以下是使用VBA代码读取Word数据的示例:
1. 打开Word文档:
```vb
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open("C:\test.docx")
```
2. 读取文档中的文本:
```vb
Dim strText As String
strText = wdDoc.Content.Text
```
3. 读取文档中的表格数据:
```vb
Dim tbl As Word.Table
Dim iRow As Integer, iCol As Integer
Set tbl = wdDoc.Tables(1)
For iRow = 1 To tbl.Rows.Count
For iCol = 1 To tbl.Columns.Count
Debug.Print tbl.Cell(iRow, iCol).Range.Text
Next iCol
Next iRow
```
4. 读取文档中的图片:
```vb
Dim shp As Word.Shape
For Each shp In wdDoc.Shapes
If shp.Type = msoPicture Then
Debug.Print shp.Name & " - " & shp.Width & " x " & shp.Height
End If
Next shp
```
5. 关闭Word文档:
```vb
wdDoc.Close SaveChanges:=False
Set wdDoc = Nothing
wdApp.Quit
Set wdApp = Nothing
```
阅读全文