• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Transfer of data from word doc to excel tables are not being read

xljgd

Member
I am trying to open a word doc and read the table and transfer the contents in an excel.

I am interested to get the first column of the table only that I the Task #.

when I input this code and ran an F8 the file is being picked up but when it reaches the table Dim Tble As Integer the value is 0.

how can I get the first column into excel. Word file attched

Again appreciate your time and help on this request.




Code:
'We declare object variables for Word Application and document
 Dim WdApp As Object, wddoc As Object
'Declare a string variable to access our Word document
 Dim strDocName As String
'Error handling
 On Error Resume Next
'Activate Word it is already open
 Set WdApp = GetObject(, “Word.Application”)
If Err.Number = 429 Then
 Err.Clear
'Create a Word application if Word is not already open
 Set WdApp = CreateObject(“Word.Application”)
End If
 WdApp.Visible = True

strDocName = "c:\xxx\xxx\task log draft.doc"
 'Check relevant directory for relevant document
'If not found then inform the user and close program
 If Dir(strDocName) = "" Then
 MsgBox "The file does not exist"
 
Exit Sub
 End If

WdApp.Activate

Set wddoc = WdApp.Documents(strDocName)

If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
 wddoc.Activate
'define variables to access the tables in the word document
 Dim Tble As Integer
 Dim rowWd As Long
 Dim colWd As Integer
 Dim x As Long, y As Long
 x = 1
 y = 1 '
 With wddoc
 Tble = wddoc.tables.Count
 If Tble = 0 Then

MsgBox "No Tables found in the Word document”, vbExclamation, “No Tables to Import”"
Exit Sub
 End If
'start the looping process to access tables and their rows, columns
 
 For i = 1 To Tble
 With .tables(i)
 For rowWd = 1 To .Rows.Count
 For colWd = 1 To .Columns.Count
 Cells(x, y) = WorksheetFunction.Clean(.cell(rowWd, colWd).Range.text)
'Access next column
 y = y + 1

Next colWd
'go to next row and start from column 1
 y = 1
 x = x + 1

Next rowWd

 End With

Next
 End With
'we don’t need to save the word document
 wddoc.Close Savechanges:=False
'we quit Word
 WdApp.Quit
'We finally release system memory allocated to the 2 object variables
 Set wddoc = Nothing
 Set WdApp = Nothing
 End Sub [code\]
 

Attachments

  • Task Log Sheet-draft.doc
    53 KB · Views: 1
Back
Top