Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default setting current directory

Hello all,

I can set the current directory using the following

myCurrentFolder = ActiveWorkbook.Path
ChDir myCurrentFolder

this works great for my 'file choser' dialog box on my local drive c:
the problem occurs for a mapped network drive... using the same line of
code and running the macro from a mapped network drive it sets my
dialog window to my documents.

does anyone know why this is the case? or any direction to solve this?

regards,
jerm

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default setting current directory

Saved from a previous post:

If you refer to the folder\file by its UNC path
(\\something\somethingelse\filename.xls), you can use an API call. In fact,
this works with mapped drives, too:

Here's a sample, but with getsaveasfilename.

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

wrote:

Hello all,

I can set the current directory using the following

myCurrentFolder = ActiveWorkbook.Path
ChDir myCurrentFolder

this works great for my 'file choser' dialog box on my local drive c:
the problem occurs for a mapped network drive... using the same line of
code and running the macro from a mapped network drive it sets my
dialog window to my documents.

does anyone know why this is the case? or any direction to solve this?

regards,
jerm


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default setting current directory

Each drive has a CurDir, so if you are moving to a drive use a ChDrive also.

myCurrentFolder = ActiveWorkbook.Path
ChDrive myCurrentFolder
ChDir myCurrentFolder

Not sure how/if this works with mapped.

NickHK

wrote in message
ps.com...
Hello all,

I can set the current directory using the following

myCurrentFolder = ActiveWorkbook.Path
ChDir myCurrentFolder

this works great for my 'file choser' dialog box on my local drive c:
the problem occurs for a mapped network drive... using the same line of
code and running the macro from a mapped network drive it sets my
dialog window to my documents.

does anyone know why this is the case? or any direction to solve this?

regards,
jerm



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default setting current directory

This will work for a drive\folder that isn't mapped--or even one that is.

But if your drive is mapped, then it's kind of overkill.


Dave Peterson wrote:

Saved from a previous post:

If you refer to the folder\file by its UNC path
(\\something\somethingelse\filename.xls), you can use an API call. In fact,
this works with mapped drives, too:

Here's a sample, but with getsaveasfilename.

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

wrote:

Hello all,

I can set the current directory using the following

myCurrentFolder = ActiveWorkbook.Path
ChDir myCurrentFolder

this works great for my 'file choser' dialog box on my local drive c:
the problem occurs for a mapped network drive... using the same line of
code and running the macro from a mapped network drive it sets my
dialog window to my documents.

does anyone know why this is the case? or any direction to solve this?

regards,
jerm


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
current directory Ross[_2_] Excel Discussion (Misc queries) 1 April 2nd 07 10:00 PM
Current Directory Mallasch Excel Discussion (Misc queries) 4 September 15th 06 03:05 AM
get current directory Souris Excel Programming 1 September 27th 05 11:53 AM
How to force current directory CLR Excel Programming 8 November 30th 04 01:11 AM
changing current directory to that of the current open file unnameable Excel Programming 2 May 19th 04 11:14 AM


All times are GMT +1. The time now is 04:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"