Alexander
Alexander

Reputation: 313

Change working directory from VBA to a UNC

Is there a way to change Excel's working directory using a UNC name?..

This: Changing working directory from Excel vba shell explains how to use ChDir / ChDrive. However, there's no such thing as "Drive"† for a UNC link.

The following naïve code does not change CurDir:

ChDir "\\testserver\longunc\directory"
Debug.Print CurDir()
C:\Documents\ZZZ

And the following obviously fails:

ChDrive "\\testserver\longunc\directory"
Run-time error '5': Invalid procedure call or argument

"Drive"?.. Changing "drive" in 2019?.. How daft is that! Why does this concept even still exist, Microsoft?..

Upvotes: 0

Views: 1192

Answers (1)

EylM
EylM

Reputation: 6103

You can the SetCurrentDirectory function.

Import to VBA:

Private Declare Function SetCurrentDirectoryA Lib "kernel32" (ByVal lpPathName As String) As Long

Upvotes: 2

Related Questions