VB如何读取word中表格第一列、第二列内容?
VB如何读取word中表格第一列、第二列内容?
2019-05-24 15:56
程序代码:
Option Explicit
Dim str1 As String
Dim str2 As String
Private Sub Command1_Click()
Dim myword As Object, mydocument As Object, mytable As Object
Set myword = CreateObject("Word.Application") '创建word对象
myword.Visible = True
Set mydocument = myword.Documents.Open(App.Path & "\123.docx")
Dim i As Byte
Dim j As Byte
For i = 1 To mydocument.Tables.Count
Set mytable = mydocument.Tables(i)
For j = 1 To mytable.Rows.Count
str1 = mytable.Cell(j, 1).Range
str1 = Left(str1, Len(str1) - 2)
Print str1
str2 = mytable.Cell(j, 2).Range
str2 = Left(str2, Len(str2) - 2)
Print str2
Next
Next
End Sub

2019-05-24 21:00
2019-05-25 20:48
程序代码:
Option Explicit
Dim str1 As String
Dim str2 As String
Private Sub Command1_Click()
On Error Resume Next
Dim k As Integer
Dim filepath As String
Dim myword As Object, mydocument As Object, mytable As Object
filepath = App.Path & "\123.docx"
If Dir(filepath) <> "" Then
Name filepath As App.Path & "\1232.docx"
End If
If Err.Number <> 0 Then
Print "文件打开了"
Set myword = GetObject(, "Word.Application")
For Each mydocument In myword.documents
k = k + 1
If mydocument.FullName = filepath Then
Exit For
End If
Next
Set mydocument = myword.documents(k)
Else
Print "文件没打开"
Name App.Path & "\1232.docx" As filepath
Set myword = CreateObject("Word.Application") '创建word对象
myword.Visible = True
Set mydocument = myword.documents.Open(filepath)
End If
Dim i As Byte
Dim j As Byte
For i = 1 To mydocument.Tables.Count
Set mytable = mydocument.Tables(i)
For j = 1 To mytable.Rows.Count
str1 = mytable.Cell(j, 1).Range
str1 = Left(str1, Len(str1) - 2)
Print str1
str2 = mytable.Cell(j, 2).Range
str2 = Left(str2, Len(str2) - 2)
Print str2
Next
Next
End Sub

2019-05-25 23:56
2019-05-26 12:05
2019-05-26 22:19

2019-05-27 00:02