-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetrieveWordTableItems
60 lines (40 loc) · 1.36 KB
/
RetrieveWordTableItems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Sub RetrieveWordTableItems()
Dim oRow As Row
Dim oCell As Cell
Dim sCellText As String
Dim II
'--- error handler
On Error GoTo ErrorHandler
Set oxlApp = CreateObject("Excel.Application")
'---- specify the location of the excel file
FN = "...\tables.xlsx"
Set oxlWbk = oxlApp.Workbooks.Open(FileName:=FN) ' Open the Workbook
excelRow = 0
count_tbl = ActiveDocument.Tables.Count
'--- loop through all the tables in the Word document
II = 1
For II = 1 To count_tbl
excelRow = excelRow + 1
excelCell = 0
'---- go through each row in the table.
For Each oRow In ActiveDocument.Tables(II).Rows
'--- go throu each cell of the row
For Each oCell In oRow.Cells
sCellText = oCell.Range
' Remove table cell markers from the text.
sCellText = Left$(sCellText, Len(sCellText) - 2)
excelCell = excelCell + 1
oxlWbk.ActiveSheet.Cells(excelRow, excelCell).Value = sCellText
Next oCell
Next oRow
Next
oxlWbk.Save
MsgBox "End of the script"
ErrorHandler:
If Err <> 0 Then
Dim Msg As String
Msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description _
& Chr(13) & "Make sure there is a table in the current document."
MsgBox Msg, , "Error"
End If
End Sub