G
Guest
I am trying to read data from a Word Table with VBA that is written in
MSProject. One would think that the object library would be the same but…..
So the below code works like a charm when it is run as a Word Macro but under
MSProject I can not seem to find the tables in the Word Doc.
******************This is the code that works in Word****************
Sub RetrieveTableItems()
On Error GoTo ErrorHandler
i = 1
' Loop through each row in the table.
For Each oRow In ActiveDocument.Tables(8).Rows 'Index # finds the Table
in question
C = 0
For Each oCell In oRow.Cells ' Loop through each cell in the
current row.
If C = 0 Then
ColOne(i) = oCell.Range
ElseIf C = 1 Then
ColTwo(i) = oCell.Range
ElseIf C = 2 Then
ColThree(i) = oCell.Range
ElseIf C = 3 Then
ColFour(i) = oCell.Range
ElseIf C = 4 Then
ColFive(i) = oCell.Range
ElseIf C = 5 Then
ColSix(i) = oCell.Range
End If
C = C + 1
Next oCell
C = 7
i = i + 1
Next oRow
End Sub
************this is almost the same code but can not see the
table**************
Sub ReadWordTables()
Dim CurrentTable As Word.Table
Set WordApp = Word.Application
Set myDoc = Word.ActiveDocument
WordApp.Activate
WordApp.Visible = True
myDoc.Activate
Set CurrentTable = myDoc.Tables(1)
On Error GoTo ErrorHandler
i = 1
' Loop through each row in the table.
For Each oRow In CurrentTable 'Index # finds the Table in question
C = 0
For Each oCell In oRow.Cells ' Loop through each cell in the
current row.
If C = 0 Then
ColOne(i) = oCell.Range
ElseIf C = 1 Then
ColTwo(i) = oCell.Range
ElseIf C = 2 Then
ColThree(i) = oCell.Range
ElseIf C = 3 Then
ColFour(i) = oCell.Range
ElseIf C = 4 Then
ColFive(i) = oCell.Range
ElseIf C = 5 Then
ColSix(i) = oCell.Range
End If
C = C + 1
Next oCell
C = 7
i = i + 1
Next oRow
End If
End Sub
MSProject. One would think that the object library would be the same but…..
So the below code works like a charm when it is run as a Word Macro but under
MSProject I can not seem to find the tables in the Word Doc.
******************This is the code that works in Word****************
Sub RetrieveTableItems()
On Error GoTo ErrorHandler
i = 1
' Loop through each row in the table.
For Each oRow In ActiveDocument.Tables(8).Rows 'Index # finds the Table
in question
C = 0
For Each oCell In oRow.Cells ' Loop through each cell in the
current row.
If C = 0 Then
ColOne(i) = oCell.Range
ElseIf C = 1 Then
ColTwo(i) = oCell.Range
ElseIf C = 2 Then
ColThree(i) = oCell.Range
ElseIf C = 3 Then
ColFour(i) = oCell.Range
ElseIf C = 4 Then
ColFive(i) = oCell.Range
ElseIf C = 5 Then
ColSix(i) = oCell.Range
End If
C = C + 1
Next oCell
C = 7
i = i + 1
Next oRow
End Sub
************this is almost the same code but can not see the
table**************
Sub ReadWordTables()
Dim CurrentTable As Word.Table
Set WordApp = Word.Application
Set myDoc = Word.ActiveDocument
WordApp.Activate
WordApp.Visible = True
myDoc.Activate
Set CurrentTable = myDoc.Tables(1)
On Error GoTo ErrorHandler
i = 1
' Loop through each row in the table.
For Each oRow In CurrentTable 'Index # finds the Table in question
C = 0
For Each oCell In oRow.Cells ' Loop through each cell in the
current row.
If C = 0 Then
ColOne(i) = oCell.Range
ElseIf C = 1 Then
ColTwo(i) = oCell.Range
ElseIf C = 2 Then
ColThree(i) = oCell.Range
ElseIf C = 3 Then
ColFour(i) = oCell.Range
ElseIf C = 4 Then
ColFive(i) = oCell.Range
ElseIf C = 5 Then
ColSix(i) = oCell.Range
End If
C = C + 1
Next oCell
C = 7
i = i + 1
Next oRow
End If
End Sub