What is the meaning of this sub

F

Frank Situmorang

Hello,

Can anyone explain, what is the meaning/purpose of this sub:
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long

Sub ChDirNet(szPath As String)
SetCurrentDirectoryA szPath
End Sub

Waht is " Kernel32"

Thanks for any idea

Frank
 
J

Jacob Skaria

Frank

Kernel32.dll is a dynamic link library found in the windows operating system
kernel which handles memory management, IO operrations etc; The below
function will set the current directory as what is specified. For example the
below code will change the directory to \\fnp01\common

ChDirNet "\\fnp01\common"

If this post helps click Yes
 
C

Chip Pearson

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top