CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: file path

  1. #1
    Join Date
    Apr 2001
    Posts
    17

    file path

    I have tried to take the drive letter path(currently my CD drive) off the this file to run in order to not get a "path not found" error, but i get an error for "1004" "masterschedule.xls" could not be found. I also have same file(masterschedule.xls) located in the file from which the program runs,
    but doesn't seem to find it. I'm sure I have something worded wrong in code below. have tried
    "masterschedule.xls","\masterschedule.xls"
    Would like it to read file with no path errors, since it will be run on computer at work.code below, any suggestions welcome.

    Set m_objApp = GetObject(, "Excel.Application")
    m_objApp.Workbooks.Open "m:masterschedule.xls"
    Set m_objSheet = m_objApp.ActiveSheet


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: file path

    for example:
    dim myPathName as string
    myPathName="m:\masterschedule.xls"
    if dir(myPathName)<>"" then 'file exists, path is correct
    else
    'find the right path
    'ie: mypathname = inputbox "Insert disk:\path\filename.xls"
    ...
    end if
    m_objApp.Workbooks.Open myPathName


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: file path

    The file will be searched for on the drive that Excel is installed. This is because excel will search it's own application folder when you do not specify the full path. If the file is in the same directory as your application, you can use the app.path property to get the full path:

    ' get path
    dim strPath as string
    strPath = app.path
    ' if the application is in a root directory (like c:\) the app.path ends with a \
    ' if it's not in a root (like c:\temp), the app.path ends without a \, so we need to add it
    If right(strPath,1) = "\" then
    strPath = strPath & "masterschedule.xls"
    else
    strPath = strPath & "\masterschedule.xls"
    end if

    ' instanciate excel
    set m_objApp = GetObject(, "Excel.Application")
    ' open the file
    m_objApp.Workbooks.Open strPath
    set m_objSheet = m_objApp.ActiveSheet



    I hope this is what you mean...



    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  4. #4
    Join Date
    Apr 2001
    Posts
    17

    Re: file path

    now how can i have program open file itself.
    i have to have .xls open in order for VB to read it.


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

Featured