Option Compare Database
Option Explicit
Private Sub CheckFolder()
[COLOR=red]'this loops through the excel directory and gets each file[/COLOR]
Dim fdr As String
Dim sPath As String
Dim sFile As String
sPath = "C:\openxmltest\" [COLOR=red]'change this to your path[/COLOR]
fdr = Dir(sPath)
Do While fdr <> ""
sFile = sPath & fdr
ProcessExcelFile (sFile, fdr) 'this opens the file - finds cell A3 and creates a new table
fdr = Dir
Loop
End Sub
Public Sub CreateTableDef(strTableName As String) [COLOR=red]'this creates a new table definition in access based on your fields[/COLOR]
Dim tbl As DAO.TableDef
Dim fld As New DAO.Field
Dim rs As DAO.Recordset
Dim strSQL As String
Set tbl = CurrentDb.CreateTableDef(strTableName)
Set fld = tbl.CreateField("Field1", dbText, 100)
Set fld = tbl.CreateField("Field1", dbText, 100)
'you need to create your own fields here that match up with the columns
'in your excel spreadsheet -
'assuming all the spreadsheets have the same columns
Set fld = tbl.CreateField("Field3", dbText, 100)
'see [URL="http://allenbrowne.com/ser-49.html"][COLOR=#0066cc]http://allenbrowne.com/ser-49.html[/COLOR][/URL] for more field type references
tbl.Fields.Append fld
CurrentDb.TableDefs.Append tbl
CurrentDb.TableDefs.Refresh
End Sub
Public Sub ProcessExcelFile(sFile As String, sFileName as String)
'sFile is the full path of the file, sFileName is just the filename
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim sA3Val As String
Dim fieldArray() As String
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
If Err.Number > 0 Then 'Excel was not open
Set objXL = New Excel.Application
End If
objXL.Visible = False
Set objWkb = objXL.Workbooks.Open(sFile)
With objXL
With objSht
[COLOR=red] 'create table with excel filename[/COLOR]
sFileName = Replace(sFileName,".xls","") [COLOR=red]' removes xls from filename[/COLOR]
CreateTableDef (sFileName)
[COLOR=red] 'do some code to read the rest of the cell values for each row and insert into the new table[/COLOR]
End With
End With 'End objXL
objXL.Quit
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
End Sub