View Single Post
 
Old 01-23-2015, 05:01 AM
Story11 Story11 is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Jan 2015
Posts: 28
Story11 is on a distinguished road
Default

@charlesdh: Everything is messed up again. Maybe I should show you the code that worked on Windows 7, then you should help me incorporate in it the code that will make it work on both Windows and MAC.


Code:
Sub ImportWordTables()

'Imports cells (1,2), (2,2),(3,2) and (4,2) from Word document Tables 1-50

   Dim wdDoc         As Object
   Dim wdFileName    As Variant
   Dim TableNo       As Integer  'number of tables in Word doc
   Dim iTable        As Integer  'table number index
   Dim iRow          As Long     'row index in Excel
   Dim iCol          As Integer  'column index in Excel
   
   wdFileName = Application.GetOpenFilename("Word files (*.doc*),*.doc*", , _
         "Browse for file containing table to be imported")
         
   If wdFileName = False Then Exit Sub '(user cancelled import file browser)
   
   Set wdDoc = GetObject(wdFileName)   'open Word file
   
   With wdDoc
      TableNo = wdDoc.tables.Count
      If TableNo = 0 Then
         MsgBox "This document contains no tables", _
               vbExclamation, "Import Word Table"
      ElseIf TableNo > 50 Then
         TableNo = 50
      'Else TableNo is actual number of tables between 1 and 9
      End If
      
      
               Range("A1") = "S/N"
               Range("B1") = "Name"
      Range("C1") = "Age"
      Range("D1") = "Sex"
      Range("E1") = "CGPA"
      For iTable = 1 To TableNo
         With .tables(iTable)
            'copy cell contents from Word table cells to Excel cells in column B and C
            Cells(iTable + 1, "A") = iTable
            Cells(iTable + 1, "B") = WorksheetFunction.Clean(.cell(1, 2).Range.Text)
            Cells(iTable + 1, "C") = WorksheetFunction.Clean(.cell(2, 2).Range.Text)
            Cells(iTable + 1, "D") = WorksheetFunction.Clean(.cell(3, 2).Range.Text)
            Cells(iTable + 1, "E") = WorksheetFunction.Clean(.cell(4, 2).Range.Text)
         End With
         Next iTable
   End With
   
   Set wdDoc = Nothing
   
End Sub

Last edited by Story11; 01-23-2015 at 04:28 PM.
Reply With Quote