Open a excel file

Declano

New member
Local time
Today, 02:48
Joined
Sep 17, 2006
Messages
4
I am trying to open a specific excel file with the following code:
Code:
Private Sub cmdExcel_Click()
OpenExcelFile ("C:\Documents and Settings\Planner.xls")
Dim objXL As Object

On Error Resume Next

Set objXL = GetObject(, "Excel.Application")
Set objXL = GetObject(strPathToFile)

objXL.Application.Visible = True
objXL.Parent.Windows(1).Visible = True

End Sub
When running it I receive this error:
Sub or function not defined
highlighting the OPENEXCELFILE ("C:......
How can I solve the problem?
Thanks to all.
Declan
 
Using ADO I would do something lke the following:

Public Function Read_Excel (ByVal sFile As String) As ADODB.Recordset

On Error GoTo fix_err
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim sconn As String

rs.CursorLocation = adUseClient
rs.CursorType = adOpenKeyset
rs.LockType = adLockBatchOptimistic

sconn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & sFile
rs.Open "SELECT * FROM [sheet1$]", sconn
Set Read_Excel = rs
Set rs = Nothing
Exit Function

fix_err:
Debug.Print Err.Description + " " + _
Err.Source, vbCritical, "Import"
Err.Clear
End Function

p.s don't forget to set relevant reference

cheers
 
Are you just trying to open the file or automate it after opening as well?
If just opening you can make life a lot easier using the followhyperlink or shell method e.g.:

Application.FollowHyperlink "C:\Documents and Settings\Planner.xls"

HTH
Good luck
 

Users who are viewing this thread

Back
Top Bottom