PDA

View Full Version : [SOLVED:] How to correct the VBA error



idnoidno
06-16-2017, 08:38 PM
Sub ttxx()
Dim oWord As Object
Dim vFiles
Dim iFile As Integer
Dim R As Range
Dim irow As Integer, i As Integer, j As Integer
vFiles = Application.GetOpenFilename("Word files (*.doc*),*.doc*", Title:="Please select the files you want to copy from", MultiSelect:=True)
If TypeName(vFiles) = "Boolean" Then Exit Sub ' Cancelled
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
irow = Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row)
'Set R = Worksheets.Add.Range("A1:a" & irow)
For iFile = LBound(vFiles) To UBound(vFiles)
oWord.Documents.Open vFiles(iFile)
oWord.ActiveDocument.tables(1).Select
With Selection
If irow > 1 Then
For i = 2 To .Rows.Count
For j = 1 To .Columns.Count
Cells(irow + i - 1, j) = WorksheetFunction.Clean(.cell(i, j).Range.Text)
Next j
Next i
Else
For irow = 1 To .Rows.Count
For j = 1 To .Columns.Count
Cells(i, j) = WorksheetFunction.Clean(.cell(i, j).Range.Text)
Next j
Next irow
End If
End With
oWord.ActiveDocument.Close False
Next
oWord.Quit
Set oWord = Nothing
ActiveSheet.Columns.AutoFit
End Sub

I am trying to import WORD table into EXCEL worksheet, but it can not work, please help me how to correct the wrong code?

mdmackillop
06-17-2017, 12:07 AM
Option Explicit


Sub ttxx()
Dim oWord As Object
Dim vFiles
Dim iFile As Integer, x As Long, y As Long
Dim R As Range
Dim irow As Integer, i As Integer, j As Integer
vFiles = Application.GetOpenFilename("Word files (*.doc*),*.doc*", Title:="Please select the files you want to copy from", MultiSelect:=True)
If TypeName(vFiles) = "Boolean" Then Exit Sub ' Cancelled
Set oWord = CreateObject("Word.Application")
'oWord.Visible = True
irow = Cells(Rows.Count, 1).End(xlUp).Row
'Set R = Worksheets.Add.Range("A1:a" & irow)
For iFile = LBound(vFiles) To UBound(vFiles)
oWord.Documents.Open vFiles(iFile)
'oWord.ActiveDocument.tables(1).Select
With oWord.ActiveDocument.tables(1)
If irow > 1 Then
For i = 2 To .Rows.Count
For j = 1 To .Columns.Count
Cells(irow + i - 1, j) = WorksheetFunction.Clean(.cell(i, j).Range.Text)
Next j
Next i
Else
For irow = 1 To .Rows.Count
For j = 1 To .Columns.Count
Cells(irow, j) = WorksheetFunction.Clean(.cell(i, j).Range.Text)
Next j
Next irow
End If
End With
oWord.ActiveDocument.Close False
Next
oWord.Quit
Set oWord = Nothing
ActiveSheet.Columns.AutoFit
End Sub

idnoidno
06-17-2017, 01:53 AM
Thank you for helping me to answer several questions, I would like to ask you about "Excel VBA Dictionary", I learn it form "https://excelmacromastery.com/vba-dictionary/" website, probably because of the mother language , I always can not fully understand the KEY and ITEM syntax for practical application,google a lot of information, but still do not understand, you can introduce where I can learn more correct?

mdmackillop
06-17-2017, 02:23 AM
http://www.snb-vba.eu/VBA_Dictionary_en.html#L_0