+ Reply to Thread
Results 1 to 3 of 3

A file iis found if I open Excel FIRST, but not if I open the file by double-clicking

  1. #1
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    A file iis found if I open Excel FIRST, but not if I open the file by double-clicking

    Greetings,

    If I FIRST open Excel, and then click OPEN and navigate to the folder where my Excel file, is, and open it that way, and then click a button to run some VBA code, the code runs fine... no problems at all.

    HOWEVER, I've discovered if I use Windows Explorer to navigate to the folder where my Excel file is, and then double-click on it to open it up that way, the program crashes when I click the same button and run the very same VBA code.

    When the program crashes it crashes because it can't find a file it's trying to open.

    The filename it's trying to open is listed in my program as "folder 1\folder.xlsx". Notice there is no letter drive in the filename. (Furthermore, that's not even the full path. I'm actually nested a bit further deeper, on my E: drive.) That's just the path starting where my excel vba program is located.

    But there's noting wrong with that as my filename, because, as I mentioned above, opening that filename DOES work when I open Excel first and use Excel to open my vba program.


    Now, I already have this coded, prior to opening the file:

    ChDir ThisWorkbook.Path

    This should, and it looks like it does, change the computer's path to the folder I'm in. This is why I can successfully open "folder 1\folder.xlsx"

    So shouldn't that statement take care of my problem?

    Why in blazes does my program work when I open Excel first, and use Excel to open the program, and not when I just open my Excel vba program just by double-clicking it via Windows Explorer?

    As always, thanks in advance for any replies.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: A file iis found if I open Excel FIRST, but not if I open the file by double-clicking

    ChDir ThisWorkbook.Path does not change the dive to E. The default drive is still the drive the the Application is launched from. Probably C.

    If you double click on a file to launch Excel, the current drive and path are still the Application's default drive and path.

    If you open Excel and then open a File, the current drive and path become the drive and path of that file. The Open File process changes it.


    Put this in your code to change the Drive as well.
    ChDrive "E"
    ChDir ThisWorkbook.Path



    This is from the Remarks section for the ChDir help.
    Remarks

    The ChDir statement changes the default directory but not the default drive. For example, if the default drive is C, the following statement changes the default directory on drive D, but C remains the default drive:
    ChDir "D:\TMP"
    Last edited by AlphaFrog; 05-11-2013 at 01:38 AM.

  3. #3
    Forum Contributor
    Join Date
    10-13-2012
    Location
    Southern California
    MS-Off Ver
    Excel 2007
    Posts
    401

    Re: A file iis found if I open Excel FIRST, but not if I open the file by double-clicking

    Good work Frog, that was it. It works fine now, when double-clicking it.

    However, now I have another quick, related question, but before I answer it I want to test a few things and attempt to figure out the answer on my own. Stay tuned...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1