Home All Groups Group Topic Archive Search About

Accessing existing Excel Workbook from complete path

Author
2 May 2006 11:47 AM
Lucile
Hi all,

I am trying to open a Excel Workbook to check data in worksheets. The
ONLY info I have about the EXCEL file is its entire path. I tried to do
the following, but an exception occurs:


Dim _objXLApp As Excel.ApplicationClass
        Dim _objXLFile As Excel.Workbook
        Dim _objWorkSheets As Excel.Worksheets
        Dim _objXLSheet As Excel.Worksheet
        Dim _objRange As Excel.Range
        Dim _strPath As String

        _strPath = "C:\LOCALDATA\FCP_93.xls"
        _objXLApp = New Excel.ApplicationClass
        _objXLApp.Visible = False
        Try
            _objXLFile = _objXLApp.Workbooks(_strPath) 'EXCEPTION HERE:
"Invalid Index"
            _objWorkSheets = _objXLFile.Worksheets()

' code continues here
Catch ex As Exception
            Show(ex.Message)
        End Try

Any idea?

Thanks.

L.

Author
2 May 2006 12:46 PM
Paul Clement
On 2 May 2006 04:47:35 -0700, "Lucile" <lucile.di***@dexia-am.com> wrote:

¤ Hi all,
¤
¤ I am trying to open a Excel Workbook to check data in worksheets. The
¤ ONLY info I have about the EXCEL file is its entire path. I tried to do
¤ the following, but an exception occurs:
¤
¤
¤ Dim _objXLApp As Excel.ApplicationClass
¤         Dim _objXLFile As Excel.Workbook
¤         Dim _objWorkSheets As Excel.Worksheets
¤         Dim _objXLSheet As Excel.Worksheet
¤         Dim _objRange As Excel.Range
¤         Dim _strPath As String
¤
¤         _strPath = "C:\LOCALDATA\FCP_93.xls"
¤         _objXLApp = New Excel.ApplicationClass
¤         _objXLApp.Visible = False
¤         Try
¤             _objXLFile = _objXLApp.Workbooks(_strPath) 'EXCEPTION HERE:
¤ "Invalid Index"
¤             _objWorkSheets = _objXLFile.Worksheets()
¤
¤ ' code continues here
¤ Catch ex As Exception
¤             Show(ex.Message)
¤         End Try
¤

I believe you're looking for the Open method of the Workbooks collection object.


Paul
~~~~
Microsoft MVP (Visual Basic)