PC Review


Reply
Thread Tools Rate Thread

Change current directory to the directory that the workbook loads from!

 
 
alondon
Guest
Posts: n/a
 
      15th Apr 2007
Folks,

I need to accomplish a very simple thing. When I load a workbook from a
directory, I need to know the directory that the workbook was loaded from
and change the current directory to that directory. Most of the time the
load directory turns out to be the current directory, but NOT always. My
app will fail unless it knows where its related files are located.

I do not want to force the user to load the workbooks in any particular
directory, any directory should be OK as long as all the workbooks,
documents and PowerPoint files in the app are on that same directory.

My solution is convoluted and ridicules:

Private Sub Workbook_Open()
ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
(Len(Dir(ActiveWorkbook.FullName)))))
End Sub

I am embarrassed that I cant seem to find a simple solution to this common
problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
accomplish this than having to manipulate the ActiveWorkbook.FullName
string?

Thanks for your help.

Allan P. London, CPA
San Francisco, CA


 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      15th Apr 2007
Hello Allan,
Maybe this example will help.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

Sub WhereAreWe()
Dim strHere As String
Dim strThere As String

'We are here.
strHere = CurDir

'Change location
strThere = ActiveWorkbook.Path
ChDrive strThere
ChDir strThere
MsgBox CurDir

'Return home
ChDrive strHere
ChDir strHere
MsgBox CurDir
End Sub
----------


"alondon"
<(E-Mail Removed)>
wrote in message
Folks,
I need to accomplish a very simple thing. When I load a workbook from a
directory, I need to know the directory that the workbook was loaded from
and change the current directory to that directory. Most of the time the
load directory turns out to be the current directory, but NOT always. My
app will fail unless it knows where its related files are located.

I do not want to force the user to load the workbooks in any particular
directory, any directory should be OK as long as all the workbooks,
documents and PowerPoint files in the app are on that same directory.
My solution is convoluted and ridicules:

Private Sub Workbook_Open()
ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
(Len(Dir(ActiveWorkbook.FullName)))))
End Sub

I am embarrassed that I cant seem to find a simple solution to this common
problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
accomplish this than having to manipulate the ActiveWorkbook.FullName
string?
Thanks for your help.
Allan P. London, CPA
San Francisco, CA


 
Reply With Quote
 
=?Utf-8?B?SmF5?=
Guest
Posts: n/a
 
      15th Apr 2007
Hi alondon -

I suspect you are looking for the Path property. Try one of these:

ChDir ActiveWorkbook.Path
or
ChDir ThisWorkbook.Path

--
Jay


"alondon" wrote:

> Folks,
>
> I need to accomplish a very simple thing. When I load a workbook from a
> directory, I need to know the directory that the workbook was loaded from
> and change the current directory to that directory. Most of the time the
> load directory turns out to be the current directory, but NOT always. My
> app will fail unless it knows where its related files are located.
>
> I do not want to force the user to load the workbooks in any particular
> directory, any directory should be OK as long as all the workbooks,
> documents and PowerPoint files in the app are on that same directory.
>
> My solution is convoluted and ridicules:
>
> Private Sub Workbook_Open()
> ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
> (Len(Dir(ActiveWorkbook.FullName)))))
> End Sub
>
> I am embarrassed that I cant seem to find a simple solution to this common
> problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
> accomplish this than having to manipulate the ActiveWorkbook.FullName
> string?
>
> Thanks for your help.
>
> Allan P. London, CPA
> San Francisco, CA
>
>
>

 
Reply With Quote
 
RB Smissaert
Guest
Posts: n/a
 
      15th Apr 2007
There are some advantages in using the API to change the current directory:

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

Function ChDirAPI(strFolder As String) As Long
'will return 1 on success and 0 on failure
'will work with a UNC path as well
'-----------------------------------------
ChDirAPI = SetCurrentDirectoryA(strFolder)
End Function


RBS


"alondon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Folks,
>
> I need to accomplish a very simple thing. When I load a workbook from a
> directory, I need to know the directory that the workbook was loaded from
> and change the current directory to that directory. Most of the time the
> load directory turns out to be the current directory, but NOT always. My
> app will fail unless it knows where its related files are located.
>
> I do not want to force the user to load the workbooks in any particular
> directory, any directory should be OK as long as all the workbooks,
> documents and PowerPoint files in the app are on that same directory.
>
> My solution is convoluted and ridicules:
>
> Private Sub Workbook_Open()
> ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
> (Len(Dir(ActiveWorkbook.FullName)))))
> End Sub
>
> I am embarrassed that I cant seem to find a simple solution to this common
> problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
> accomplish this than having to manipulate the ActiveWorkbook.FullName
> string?
>
> Thanks for your help.
>
> Allan P. London, CPA
> San Francisco, CA
>


 
Reply With Quote
 
R1C1
Guest
Posts: n/a
 
      15th Apr 2007
Dim varPath As String
varPath = Activeworkbook.Path

Regards,

Alan


"alondon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Folks,
>
> I need to accomplish a very simple thing. When I load a workbook from a
> directory, I need to know the directory that the workbook was loaded from
> and change the current directory to that directory. Most of the time the
> load directory turns out to be the current directory, but NOT always. My
> app will fail unless it knows where its related files are located.
>
> I do not want to force the user to load the workbooks in any particular
> directory, any directory should be OK as long as all the workbooks,
> documents and PowerPoint files in the app are on that same directory.
>
> My solution is convoluted and ridicules:
>
> Private Sub Workbook_Open()
> ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
> (Len(Dir(ActiveWorkbook.FullName)))))
> End Sub
>
> I am embarrassed that I cant seem to find a simple solution to this common
> problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
> accomplish this than having to manipulate the ActiveWorkbook.FullName
> string?
>
> Thanks for your help.
>
> Allan P. London, CPA
> San Francisco, CA
>



 
Reply With Quote
 
alondon
Guest
Posts: n/a
 
      17th Apr 2007
Thanks Folks,

The Path property is exactly what I was looking for. I just forgot then got
really frustrated with the on-line help.

Also, it never occured to me to use the API. Thanks RB

Cheers,

Allan P. London, CPA


"alondon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Folks,
>
> I need to accomplish a very simple thing. When I load a workbook from a
> directory, I need to know the directory that the workbook was loaded from
> and change the current directory to that directory. Most of the time the
> load directory turns out to be the current directory, but NOT always. My
> app will fail unless it knows where its related files are located.
>
> I do not want to force the user to load the workbooks in any particular
> directory, any directory should be OK as long as all the workbooks,
> documents and PowerPoint files in the app are on that same directory.
>
> My solution is convoluted and ridicules:
>
> Private Sub Workbook_Open()
> ChDir (Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) -
> (Len(Dir(ActiveWorkbook.FullName)))))
> End Sub
>
> I am embarrassed that I cant seem to find a simple solution to this common
> problem - e.g.Activeworkbook.WhereAmI. There must be a better way to
> accomplish this than having to manipulate the ActiveWorkbook.FullName
> string?
>
> Thanks for your help.
>
> Allan P. London, CPA
> San Francisco, CA
>



 
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
Change Current Directory cory.j.layman@gmail.com Microsoft Excel Programming 4 7th Jan 2009 05:58 PM
Chdir does not seem to change the current directory -- ?? plh Microsoft Excel Programming 5 2nd May 2008 01:08 AM
Current Directory (System.IO.Directory.GetCurrentDirectory throws NotSupportedexeption) Gustavo Curve Microsoft Dot NET Compact Framework 2 17th Feb 2006 02:54 PM
Open Workbook in current directory Elijah Microsoft Excel Programming 3 30th Nov 2004 09:05 PM
opening a workbook in the current directory Richard Microsoft Excel Programming 2 30th Aug 2003 02:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:47 PM.