Results 1 to 11 of 11

Thread: [RESOLVED] Opening excel from specified location

  1. #1

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Resolved [RESOLVED] Opening excel from specified location

    Hello everyone,

    I've been trying to figure this out myself, but have just run into an issue I can't resolve with searching and examples. I'm trying to open an excel file to read from it, and I've figured out how to open a NEW excel file. I can't, however, seem to figure out how to open an excel file that already exists.

    Here is what I have so far:
    Code:
    Dim objApp As Excel.Application
    Dim objBook As Excel._Workbook
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim objBooks As Excel.Workbooks
            Dim objSheets As Excel.Sheets
            Dim objSheet As Excel._Worksheet
            Dim range As Excel.Range
    
            ' Create a new instance of Excel and start a new workbook.
            objApp = New Excel.Application()
            objBooks = objApp.Workbooks
            objBook = objBooks.Add
            objSheets = objBook.Worksheets
            objSheet = objSheets(1)
            objApp.Visible = True
    
        End Sub

  2. #2
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Opening excel from specified location


  3. #3

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Opening excel from specified location

    Okay so now I have:

    Code:
    Dim objBooks As Excel.Workbooks
            Dim objSheets As Excel.Sheets
            Dim objSheet As Excel._Worksheet
            Dim range As Excel.Range
    
            objBooks = objApp.Workbooks.Open(Filename:="C:/Documents/FIJI/SoberMonitorSelection/roster.xls")
            objApp.Visible = True
    And it's giving me the error ("Object reference not set to an instance of an object")

  4. #4
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Opening excel from specified location

    If it's just to open a file, depending if you are using VB.NET (I'm on 2008) there is a simple code used in various flavours.

    Process.Start("http://www.google.co.uk")

    Process.Start("NotePad.exe", (myFolderName & "\Paidrate.doc"))

    Process.Start(myFolderName & "\Circulate.xls")

  5. #5

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Opening excel from specified location

    I'm trying to open the exisiting instance of the excel file to read/write to it

    I figured out that I was using the wrong slashes: (/) instead of (\). But I am still getting that error.
    Last edited by paralinx; Feb 14th, 2010 at 11:50 AM.

  6. #6
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Opening excel from specified location

    This is the right code, but you'll just have to keep trying to find the correct file path. I've been in this situation before myself and know how frustrating it can be. Good luck. You can try various options with the filename in a textbox as in:
    objWorkbook= objExcel.Workbooks.Open(Me.TextBox1.Text)

  7. #7
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Opening excel from specified location

    You can also try this.
    HTML Code:
    Dim XLApp As Object
            Dim WkBk As Object
            XLApp = CreateObject("Excel.Application")
    
            XLApp.Visible = False
    
            WkBk = XLApp.Workbooks.Open(Filename:=(Application.StartupPath) & "\Myfile.xls")
            WkBk.SaveAs(Application.StartupPath & "\Myfile.xls")
    
            ' Close Excel with the Quit method on the Application object.
            XLApp.Application.Quit()
            ' Release the object variable.
            XLApp = Nothing

  8. #8

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Opening excel from specified location

    I definitely have the correct path to the file, and have data in the file. I did a check to see if it was "nothing"

    Code:
    Dim objBooks As Excel.Workbooks
            Dim objSheets As Excel.Sheets
            Dim objSheet As Excel._Worksheet
            Dim range As Excel.Range
    
            'objBooks = objApp.Workbooks.Open(Filename:="C:\Documents\FIJI\SoberMonitorSelection\roster.xls")
            'objApp.Visible = True
    
            ' Create a new instance of Excel and start a new workbook.
            If "C:\Documents\Visual Basic.NET\SoberMonitor\fijiroster.xls" IsNot Nothing Then
                objBooks = objApp.Workbooks.Open(Filename:="C:\Documents\Visual Basic.NET\SoberMonitor\fijiroster.xls")
                objBook = objBooks.Add
                objSheets = objBook.Worksheets
                objSheet = objSheets(1)
                objApp.Visible = True
            End If
    It runs through the If statement, so that pathway and file does exist, but still gets to the filename:="C:...." part and says "Object reference not set to an instance of an object"

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Opening excel from specified location

    This line

    Code:
    If "C:\Documents\Visual Basic.NET\SoberMonitor\fijiroster.xls" IsNot Nothing Then
    Does not check to see if the file exists, it checks to see if the string literal "C:\Documents\Visual Basic.NET\SoberMonitor\fijiroster.xls" is nothing, which it obviously isn't, regardless of whether or not there is a file with that name. You could put any string you like there and it would return true!

  10. #10

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Opening excel from specified location

    Quote Originally Posted by Kochanski View Post
    You can also try this.
    HTML Code:
    Dim XLApp As Object
            Dim WkBk As Object
            XLApp = CreateObject("Excel.Application")
    
            XLApp.Visible = False
    
            WkBk = XLApp.Workbooks.Open(Filename:=(Application.StartupPath) & "\Myfile.xls")
            WkBk.SaveAs(Application.StartupPath & "\Myfile.xls")
    
            ' Close Excel with the Quit method on the Application object.
            XLApp.Application.Quit()
            ' Release the object variable.
            XLApp = Nothing
    Ah! Bravo! This is what ended up working for me:
    Code:
    objApp = CreateObject("Excel.Application")
            Dim objBooks As Excel.Workbooks
            Dim objSheets As Excel.Sheets
            Dim objSheet As Excel._Worksheet
            Dim range As Excel.Range
    
            WkBk = objApp.workbooks.open(Filename:="C:\Documents\Visual Basic.NET\SoberMonitor\fijiroster.xls")
            objApp.visible = True
    Thank you, Kochanski.

  11. #11
    Addicted Member
    Join Date
    Jan 2009
    Posts
    243

    Re: Opening excel from specified location

    Well that makes me feel better. There's also a variation for file exists. Imagine your filename is in a textbox.

    HTML Code:
    If Not (System.I.O.FileExists(TextBox1.text)) Then

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width