+ Reply to Thread
Results 1 to 6 of 6

Value or purpose of ChDir

  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Value or purpose of ChDir

    I have never needed to use ChDir before a command like Workbooks.Open

    I gave a macro that worked perfectly for me to my husband and his won't work without the ChDir.

    Where I work there are public drives on the server, like K, M, N, Q.
    If I want to open a file on a network drive, I use
    Please Login or Register  to view this content.

    Where he works he "maps a drive" (whatever that means) and this long path becomes "X:\MyWorkbook.xls"
    (now the path on somebpdy elses PC wouldn't be X:\..., it would have to be mapped and it would be something else, like W:\....... why IDK ???)
    In order to open the file from his PC the code must read
    Please Login or Register  to view this content.

    why do I need the ChDir for him but not me?
    Is all the code I ever wrote "wrong" because it doesn't include ChDir?
    Last edited by carsto; 03-01-2010 at 11:57 PM. Reason: solved

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Value or purpose of ChDir

    Hi Carsto,

    with local drives like C: or D: the path to a file is pretty straightforward.

    If you want to access a folder on a networked file server, then this is easiest to do if you map the network folder as a drive. Now, on the file server, there is a C: drive, and it has several folders. From the file server perspective, the complete path to the file may be

    C:\folder1\folder2\folder3\folder4\file.xls

    Depending on how the network admin allows you to map the drive, you can have different "entry points" into the path above. Drive K: could be

    File Server C:\folder1\

    Then the path to your file would be

    K:\folder2\folder3\folder4\file.xls

    Or, the network admin may offer an entry point deeper in the path, say drive W: is mapped to

    File Server C:\folder1\folder2\folder3

    In that case, from a user's perspective, the path to the file would be

    W:\folder4\file.xls

    It seems that in your husband's case, the drive mapping leads directly to the innermost folder, so his drive X: is mapped to

    File Server C:\folder1\folder2\folder3\folder4

    So he can find the file directly at

    X:\file.xls

    because the X: already includes the rest of the full server path.

    Does that make it any clearer?

  3. #3
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: Value or purpose of ChDir

    great, so now I understand what map means. THANKS

    but why do I sometimes need ChDir and othertimes not?

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Value or purpose of ChDir

    If he has the X: drive already mapped, I don't really see a reason for the Chdir, since you are specifying the path to the file.

    I'm not a VBA buff. Maybe someone else can help. I'm calling in the cavalry. Hang in there.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Value or purpose of ChDir

    Hello carsto,

    The network drive may or may not be mapped automatically when the user logs on. It depends on how it has been setup. The ChDir statement will change the working directory, the directory you are currently in, to the directory you specify. This allows you to override the system default for the working directory.

    Since you don't have a mapped drive, there is no need to change your working directory. In the case of your husband, the directory exits on a network server, not his computer. Since this directory doesn't exist on his computer, we force the system to look on the network drive by using ChDir.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: Value or purpose of ChDir

    THANKS (again)

+ 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