Every process runs with a current default directory (folder). This
folder is used for file operations if you don't specify another
folder. You can view the name of the default directory with the CurDir
function:
Sub AAA()
Debug.Print CurDir
End Sub
When working only on the local drives, you can use the ChDir function
for changing the current directory to another (existing) folder:
Sub AAA()
ChDir "C:\Test\This"
End Sub
However, you cannot use ChDir to set the current directory to a folder
using a UNC or network file name. It won't cause an error but it won't
do anything.
Sub AAA()
' This does not work....
ChDir "\\SecurityJoan\MainShare\Test"
End Sub
The Windows API function SetCurrentDirectory works with UNC names.
E.g.,
Sub AAA()
SetCurrentDirectoryA "\\Aja\MainShare\Test"
End Sub
The ChrDirNet sub that you refer to simply calls SetCurrentDirectoryA.
If you need SetCurrentDirectoryA, there is no reason to wrap it up in
another sub such as your ChDirNet sub. You can call
SetCurrentDirectoryA directly.
Windows itself is composed of dozens of DLL files which are,
basically, libraries of functions that both Windows and other programs
can call upon. "Kernel32" is one of those libraries and
SetCurrentDirectory is one of the functions within Kernel32. Calling
upon Windows DLLs allows you to do things that are not otherwise
available with VBA, but there is little if any error checking on these
function. Pass an incorrect value to one of these functions and you'll
likely crash out Excel. Use with caution.
The "Declare" statement tells the VBA compiler in which DLL a function
resides and what the parameters to that function and their data types
are.
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)