Force to Directory

L

Ludo

Hi,

I'm trying to write some code to force the current directory to an
other one, where my data files reside.

I have following code

In a module i have :
Public Const sFMTLogFile = "D:\Files\FMTLogFiles\"
Public Const sFMTPrefix = "FmtLog_Snr"

In another module i gave:

Sub OpenOneFile()
Dim fn As Variant
Dim OldDir As String

OldDir = ThisWorkbook.path
ChDir sFMTLogFile
fn = Application.GetOpenFilename("Text-files,*.txt", _
1, "Select One File To Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
Workbooks.Open fn
End Sub

I was hoping that the ChDir would change the dir to the one preset in
the constant sFMTLogFile so that with the GetOpenFilename, the form
point to the desired directory, witch ist'n the case.

What goes wrong?

any help welcome

ludo
 
B

Bob Phillips

See if this does it


Sub OpenOneFile()
Dim fn As Variant
Dim OldDir As String

OldDir = ThisWorkbook.Path
ChDrive sFMTLogFile
ChDir sFMTLogFile
fn = Application.GetOpenFilename("Text-files,*.txt", _
1, "Select One File To Open", , False)
If TypeName(fn) = "Boolean" Then Exit Sub
Workbooks.Open fn
End Sub
 
B

Bernie Deitrick

Ludo,

Use ChDrive instead of ChDir:

ChDrive sFMTLogFile

Since it appears you are, in fact, changing the drive and not just the folder.

HTH,
Bernie
MS Excel MVP
 
D

Dave Peterson

And I'd change the drive/folder back before the subroutine finished:

Sub OpenOneFile()
Dim fn As Variant
Dim OldDir As String

OldDir = ThisWorkbook.Path
'did you mean this?????
'OldDir = CurDir

ChDrive sFMTLogFile
ChDir sFMTLogFile

fn = Application.GetOpenFilename("Text-files,*.txt", _
1, "Select One File To Open", , False)

ChDrive OldDir
ChDir OldDir

If TypeName(fn) = "Boolean" Then
Exit Sub
End If

Workbooks.Open fn
End Sub
 

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