K
-Karl
I know this seems to be a huge topic but so little working code on
this. I found a script for access 2007 that will allow me to open an
excel file.
The problem is now I need to tell it to focus (Activate) on a
particular sheet abnd copy all the data from it then put it into an
Access table that I have already created.
Below is the code I have that actually opens the file. I have a form
that has INPUT1 for the path and INPUT 2 for the file name.
Can anyone explain what I need to do to finish the process?
Many thanks!!
Sub test()
' Late Binding (Needs no reference set)
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String
Dim rs, intMaxCol
'Dim objWkb As Workbook
'Dim objSht As Worksheet
Set rs = CurrentDb.OpenRecordset("Test", dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast:
End If
' Create a new Excel instance
Set oXL = CreateObject("Excel.Application")
' Only XL 97 supports UserControl Property
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
' Full path of excel file to open
On Error GoTo ErrHandle
sFullPath = """" & Forms.Form1!Text1.Value & "\" & Forms.Form1!
Text2.Value & ".xlsm" & """"
'MsgBox sFullPath
'End
' Open it
With oXL
.Visible = True
.Workbooks.Open (sFullPath)
'.Workbooks.Activate ("switches") <-- Not supported?!
End With
ErrExit:
Set oXL = Nothing
Exit Sub
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Sub
this. I found a script for access 2007 that will allow me to open an
excel file.
The problem is now I need to tell it to focus (Activate) on a
particular sheet abnd copy all the data from it then put it into an
Access table that I have already created.
Below is the code I have that actually opens the file. I have a form
that has INPUT1 for the path and INPUT 2 for the file name.
Can anyone explain what I need to do to finish the process?
Many thanks!!
Sub test()
' Late Binding (Needs no reference set)
Dim oXL As Object
Dim oExcel As Object
Dim sFullPath As String
Dim sPath As String
Dim rs, intMaxCol
'Dim objWkb As Workbook
'Dim objSht As Worksheet
Set rs = CurrentDb.OpenRecordset("Test", dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast:
End If
' Create a new Excel instance
Set oXL = CreateObject("Excel.Application")
' Only XL 97 supports UserControl Property
On Error Resume Next
oXL.UserControl = True
On Error GoTo 0
' Full path of excel file to open
On Error GoTo ErrHandle
sFullPath = """" & Forms.Form1!Text1.Value & "\" & Forms.Form1!
Text2.Value & ".xlsm" & """"
'MsgBox sFullPath
'End
' Open it
With oXL
.Visible = True
.Workbooks.Open (sFullPath)
'.Workbooks.Activate ("switches") <-- Not supported?!
End With
ErrExit:
Set oXL = Nothing
Exit Sub
ErrHandle:
oXL.Visible = False
MsgBox Err.Description
GoTo ErrExit
End Sub