I have written a code which loops through all word documents in a folder. I am getting an error 'Object Required'. What I am trying to do here is: I have 200 word files in a folder containing same types of table. I want to open one file, import all the tables of that file into one sheet of excel and then from that sheet, copy particular cells to another sheet. These should be done in loop for all the word files. I am new to programming. Please provide your inputs to resolve this. Thanks.
Sub DoVBRoutineNow()
Dim file
Dim path As String
path = "D:\Ujjivan\CAM to MIS\CAM Unsecured\CAM\"
file = Dir(path & "*.docx")
Do While file <> ""
Documents.Open Filename:=path & file
Call ImportWordTable
ActiveDocument.Save
ActiveDocument.Close
file = Dir()
Loop
End Sub
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"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
tableTot = wdDoc.tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
'tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
tableNo = 1
End If
resultRow = 4
For tableStart = 1 To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = resultRow + 1
Next tableStart
End With
'Call simpleXlsMerger
End Sub
Sub simpleXlsMerger()
'Dim bookList As Workbook
'Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As
Object
Dim n As Integer
n = 3
Dim oFound, pFound As Range
Dim oLookin As Range
Dim sLookFor, tLookFor As String
sLookFor = "Loan amount (Rs)"
Set oLookin = Worksheets("Tables").UsedRange
Set oFound = oLookin.Find(what:=sLookFor, LookIn:=xlValues, LookAt:=xlPart,
MatchCase:=False)
Worksheets("Tables").Cells(oFound.Row + 1, oFound.Column).Copy
If Not oFound Is Nothing Then
ThisWorkbook.Worksheets("Data").Cells(n, 1).PasteSpecial xlPasteValues
'MsgBox oLookin.Range("A" & oFound.Row).Value
End If
n = n + 1
ThisWorkbook.Worksheets("Data").Activate
'bookList.Close savechanges:=False
End Sub
Related
I use this code in order to import to excel some data and to calculate the total of a row however I have a ''Object required'' error when running pointing the For cycle. Any idea?
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim A As Integer
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"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 > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If
If TableNo = 1 Or TableNo = 2 Then
With .Tables(TableNo).Range.Copy
Range("A1").Activate
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
A = "=SUM(C6:L6)"
InputBox ("Total" & A)
End With
End If
End With
Use the following code as a starting point sample. It is using early binding, but if that's not what you want it is easily changed. I also set it up with the Word document already open, but again, it can be easily changed to whatever way you want to access the Word document from Excel.
Sub GetWordTable()
Dim wApp As Word.Application, doc As Word.Document
Set wApp = GetObject(, "Word.Application")
Set doc = wApp.ActiveDocument
doc.Tables(1).Range.Copy
Range("A1").PasteSpecial xlPasteValues
End Sub
I have a script that extracts all tables from a selected word doc (user selects doc) and then prompts you to state what table you want to start at i.e. table 1, table 2, or table 3 etc.
I'm trying to figure out how to paste formats as well as it's currently only pasting values.
Thanks.
Option Explicit
Sub Macro1()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"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
tableTot = wdDoc.tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
resultRow = 4
For tableStart = 1 To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = resultRow + 1
Next tableStart
End With
End Sub
I'm using this VBA script to insert all the tables from a word document into separate excel worksheets. Because none of these tables are named, I want to label the sheets by the Page number you find the table on.
The error is that it doesn't know what the variable wdActiveEndPageNumber is.
If I change tableNo to PageNo, it also doesn't rename the sheets. It just uses the default Sheet1, Sheet2, Sheet3, etc.
A better name for each sheet would be to use the "Header 3" value at the top of each of the pages the tables are on if that's possible.
Here is my code:
Option Explicit
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
Dim sheet_i As Worksheet
Dim PageNo As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
tableNo = wdDoc.Tables.Count
tableTot = wdDoc.Tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
For tableStart = tableNo To tableTot
resultRow = 1
With .Tables(tableStart)
PageNo = .Range.Information(wdActiveEndPageNumber)
Set sheet_i = Sheets.Add(after:=Sheets(Worksheets.Count)).Name = "Page_No_" & CStr(PageNo)
sheet_i.Activate
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
Next tableStart
End With
End Sub
Thanks
Set sheet_i = Sheets.Add(after:=Sheets(Worksheets.Count)).Name = "Page_No_" & CStr(PageNo)
The Name property doesn't return the sheet, so you need to do this in two steps:
With ThisWorkbook
Set sheet_i = .Sheets.Add(after:=.Sheets(.Worksheets.Count))
sheet_i.Name = "Page_No_" & CStr(PageNo)
End With
I have used a macro that runs through each cell in a word table and pastes into excel, however one of my documents has 96 pages and it will take literally 40 mins to copy it all into the spreadsheet. I have found it is much faster if the tables are converted to text (comma delimited) then saved as a .txt file, then imported into the spreadsheet, however I cannot figure out how to write a macro or vbscript to do it all at once. any ideas??
Private Sub ImportWordTable()
Dim wddoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer
Dim iRow As Long
Dim iCol As Integer
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
Application.ScreenUpdating = False
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*DOC),*.doc", , _
"Browse for file containing table to be imported")
If wdFileName = "" Then Exit Sub
Set wddoc = GetObject(wdFileName)
With wddoc
tableNo = wddoc.Tables.Count
tableTot = wddoc.Tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = 1
End If
resultRow = 1
For tableStart = 1 To tableTot
With .Tables(tableStart)
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
Cells(resultRow) = WorksheetFunction.Clean(.Cell(iRow).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = resultRow + 1
Next tableStart
End With
Set wddoc = Nothing
End Sub
Try this...
Sub ImportWordTable()
Dim wddoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer
Dim iRow As Long
Dim iCol As Integer
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
Application.ScreenUpdating = False
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*DOC),*.doc", , _
"Browse for file containing table to be imported")
If wdFileName = "" Then Exit Sub
Set wddoc = GetObject(wdFileName)
With wddoc
tableNo = wddoc.Tables.Count
tableTot = wddoc.Tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = 1
End If
For tableStart = 1 To tableTot
Application.StatusBar = "Processing " & tableStart & "of (" & tableTot & ") Tables"
.Tables(tableStart).Range.Copy
resultRow = Range("A" & Rows.Count).End(xlUp).Offset(2).Row
DoEvents
On Error Resume Next
Range("A" & resultRow).PasteSpecial xlPasteValues
On Error GoTo 0
Next tableStart
End With
Set wddoc = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub
I exported all of the tables from a large Word document a few months ago, then my computer went and lost the VBA I had used...Note ++ decided to disappear and take all my files with it.
So, I have looked up several different options that are out there to pull all the tables from a Word file to Excel.
Every one I try says there are no tables in the Word file. I have tried multiple files, all the same thing.
The only other change in my environment is I upgraded to Windows 10.
I have racked my brain and cannot figure out why it is not seeing the tables?
I can copy the tables to a different word file:
Sub CopyTables()
Dim Source As Document
Dim Target As Document
Dim tbl As Table
Dim tr As Range
Set Source = ActiveDocument
Set Target = Documents.Add
For Each tbl In Source.Tables
Set tr = Target.Range
tr.Collapse wdCollapseEnd
tr.FormattedText = tbl.Range.FormattedText
tr.Collapse wdCollapseEnd
tr.Text = vbCrLf
Next
End Sub
And I can find the Table Number in the word file:
Sub FindTableNumber()
Dim J As Integer
Dim iTableNum As Integer
Dim oTbl As Table
Selection.Bookmarks.Add ("TempBM")
For J = 1 To ActiveDocument.Tables.Count
Set oTbl = ActiveDocument.Tables(J)
oTbl.Select
If Selection.Bookmarks.Exists("TempBM") Then
iTableNum = J
Exit For
End If
Next J
ActiveDocument.Bookmarks("TempBM").Select
ActiveDocument.Bookmarks("TempBM").Delete
MsgBox "The current table is table " & iTableNum
End Sub
This is an example of the VBA I have tried, and received the No Tables in Document:
Option Explicit
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"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
tableTot = wdDoc.tables.Count
If tableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf tableNo > 1 Then
tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
"Enter the table to start from", "Import Word Table", "1")
End If
resultRow = 4
For tableStart = 1 To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell (iRow, iCol).Range.Text)
Next iCol
resultRow = resultRow + 1
Next iRow
End With
resultRow = resultRow + 1
Next tableStart
End With
End Sub