PC Review


Reply
Thread Tools Rate Thread

ChDir and ChDrive

 
 
Conan Kelly
Guest
Posts: n/a
 
      24th Dec 2008
Hello all,

I have a some code that I use to alter a handful of personal files on a
local drive.

I'm trying to set this code up to store the current directory, change to a
specific folder on a local drive, then change the current directory back to
the one that was current before the code ran.

I just figured out that I will have to use both ChDir and ChDrive in order
to do this, and then use both to revert back to original directory before
running the code. No big deal, IF THE NETWORK SHARE IS MAPPED TO A DRIVE
LETTER!!! Most of the network locations that I work in are mapped to drive
letters. But on the rare occasion, I will be working in the network
location directly, ie: \\Server\Share.

It looks like changing CurDir from a network location to a local directory
is no big deal. But I'm having a hard time trying to figure out how to
change from a local to a network location.

If my original directory is...

\\Server\Share

....and I want to go to...

C:\Folder\Subfolder

....then my code would look something like this...

'CurDir would be "\\Server\Share" at this point.
pstrCurrFolder = CurDir
'ChDrive "C" 'it appears this line is not neccesary when going
from network to local
ChDir "C:\Folder\Subfolder"

But it doesn't look like I can revert back to my network location, after all
the processing, if it is not mapped to a drive letter.

ChDir pstrCurrFolder 'only works if pstrCurrFolder is another
location on the current drive.
ChDrive "\\Server" 'No workie!!!

Does anyone know of a way to accomplish what I'm trying to do?

Thanks for any help anyone can provide,

Conan Kelly



---------------------------
"Smokin' weed kills your brain cells. Drinkin' only screws up your
liver...ya got 2 a those."
- Earl Hickey (NBC's "My Name is Earl")


 
Reply With Quote
 
 
 
 
Jim Rech
Guest
Posts: n/a
 
      24th Dec 2008
The Windows API function SetCurrentDirectoryA is preferable for two reasons.
It works with UNC paths and it handles both ChDrive abd ChDir functions.

Add this declaration to the top of the module:

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

and then anywhere in it you can use it like this:

SetCurrentDirectoryA "\\Server\Share"

or

SetCurrentDirectoryA "C:\Folder\Subfolder"

--
Jim
"Conan Kelly" <(E-Mail Removed)> wrote in message
news:wEw4l.94136$_(E-Mail Removed)...
> Hello all,
>
> I have a some code that I use to alter a handful of personal files on a
> local drive.
>
> I'm trying to set this code up to store the current directory, change to a
> specific folder on a local drive, then change the current directory back
> to the one that was current before the code ran.
>
> I just figured out that I will have to use both ChDir and ChDrive in order
> to do this, and then use both to revert back to original directory before
> running the code. No big deal, IF THE NETWORK SHARE IS MAPPED TO A DRIVE
> LETTER!!! Most of the network locations that I work in are mapped to
> drive letters. But on the rare occasion, I will be working in the network
> location directly, ie: \\Server\Share.
>
> It looks like changing CurDir from a network location to a local directory
> is no big deal. But I'm having a hard time trying to figure out how to
> change from a local to a network location.
>
> If my original directory is...
>
> \\Server\Share
>
> ...and I want to go to...
>
> C:\Folder\Subfolder
>
> ...then my code would look something like this...
>
> 'CurDir would be "\\Server\Share" at this point.
> pstrCurrFolder = CurDir
> 'ChDrive "C" 'it appears this line is not neccesary when going
> from network to local
> ChDir "C:\Folder\Subfolder"
>
> But it doesn't look like I can revert back to my network location, after
> all the processing, if it is not mapped to a drive letter.
>
> ChDir pstrCurrFolder 'only works if pstrCurrFolder is another
> location on the current drive.
> ChDrive "\\Server" 'No workie!!!
>
> Does anyone know of a way to accomplish what I'm trying to do?
>
> Thanks for any help anyone can provide,
>
> Conan Kelly
>
>
>
> ---------------------------
> "Smokin' weed kills your brain cells. Drinkin' only screws up your
> liver...ya got 2 a those."
> - Earl Hickey (NBC's "My Name is Earl")
>
>



 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      24th Dec 2008
ChDir won't work with UNC paths. But you can use a Windows API that will work
with either UNC paths or Mapped drives.

Here's an example I saved from a previous post:


And here's an example of that API call. It uses application.getopenfilename,
but you'll see how to use it.

Option Explicit
Private Declare Function SetCurrentDirectoryA Lib _
"kernel32" (ByVal lpPathName As String) As Long
Sub ChDirNet(szPath As String)
Dim lReturn As Long
lReturn = SetCurrentDirectoryA(szPath)
If lReturn = 0 Then Err.Raise vbObjectError + 1, "Error setting path."
End Sub
Sub testme01()

Dim myFileName As Variant
Dim myCurFolder As String
Dim myNewFolder As String
Dim Wkbk as workbook

myCurFolder = CurDir
myNewFolder = "\\share\folder1\folder2"

On Error Resume Next
ChDirNet myNewFolder
If Err.Number <> 0 Then
'what should happen
MsgBox "Please change to your own folder"
Err.Clear
End If
On Error GoTo 0

myFileName = Application.GetOpenFilename(filefilter:="Excel Files, *.xls")

ChDirNet myCurFolder

If myFileName = False Then
Exit Sub 'user hit cancel
End If

'do your stuff to open it and process it.
Set wkbk = workbooks.open(filename:=myfilename)

'....

End Sub

Conan Kelly wrote:
>
> Hello all,
>
> I have a some code that I use to alter a handful of personal files on a
> local drive.
>
> I'm trying to set this code up to store the current directory, change to a
> specific folder on a local drive, then change the current directory back to
> the one that was current before the code ran.
>
> I just figured out that I will have to use both ChDir and ChDrive in order
> to do this, and then use both to revert back to original directory before
> running the code. No big deal, IF THE NETWORK SHARE IS MAPPED TO A DRIVE
> LETTER!!! Most of the network locations that I work in are mapped to drive
> letters. But on the rare occasion, I will be working in the network
> location directly, ie: \\Server\Share.
>
> It looks like changing CurDir from a network location to a local directory
> is no big deal. But I'm having a hard time trying to figure out how to
> change from a local to a network location.
>
> If my original directory is...
>
> \\Server\Share
>
> ...and I want to go to...
>
> C:\Folder\Subfolder
>
> ...then my code would look something like this...
>
> 'CurDir would be "\\Server\Share" at this point.
> pstrCurrFolder = CurDir
> 'ChDrive "C" 'it appears this line is not neccesary when going
> from network to local
> ChDir "C:\Folder\Subfolder"
>
> But it doesn't look like I can revert back to my network location, after all
> the processing, if it is not mapped to a drive letter.
>
> ChDir pstrCurrFolder 'only works if pstrCurrFolder is another
> location on the current drive.
> ChDrive "\\Server" 'No workie!!!
>
> Does anyone know of a way to accomplish what I'm trying to do?
>
> Thanks for any help anyone can provide,
>
> Conan Kelly
>
> ---------------------------
> "Smokin' weed kills your brain cells. Drinkin' only screws up your
> liver...ya got 2 a those."
> - Earl Hickey (NBC's "My Name is Earl")


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CurDir, ChDrive, ChDir error questions goaljohnbill Microsoft Excel Programming 2 13th Jan 2011 09:56 PM
ChDrive error (server path) =?Utf-8?B?TWFyY290dGUgQQ==?= Microsoft Excel Programming 6 2nd Nov 2005 07:16 PM
Problems with ChDrive and ChDir Sophie Microsoft Excel Programming 6 3rd Dec 2004 10:52 AM
Problems with ChDrive, ChDir Sophie Microsoft Excel Programming 4 29th Nov 2004 09:35 AM
Help: ChDrive and/or ChDir for a non-mapped network drive Andrew Coyle Microsoft Access VBA Modules 1 1st Oct 2003 05:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:09 AM.