ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to change directory (https://www.excelbanter.com/excel-programming/424341-how-change-directory.html)

Varun

How to change directory
 
Folks,

VBA newbie here...I am unable to "change directory" to a folder name that (I
think) is (already) being returned to me via the Sub (I think I need a
function in order to do this but not sure).

The code is below. The main program right below calls out 2 subs and 1
function. What I need to do is chdir to a folder that is returned to me by
the Sub GetMentDesContPath. Do I need to change the Sub to Function in order
to accomplish this?

The program errors out at ChDir MentorDirectory

Thanks,
Varun




Private Sub CommandMAINBUTTON_Click()

Call GetMentDesContPath
MentorDirectory = MentDesContPath

'Check to see if GEOMS ASCII and error out if it does not geoms_ascii
'Check to see if design container has /pcb subdir

If PathExists(MentDesCont & "pcb") = False Then
MsgBox "Invalid Mentor Design container specified, exiting"
Exit Sub
Else
ChDir MentorDirectory

If FileExists(MentDesCont, geoms_ascii) = False Then
MsgBox "Couldn't find geoms_ascii file, exiting"
Exit Sub
Else
'open geoms_ascii file

End If

End If

End Sub


Sub GetMentDesContPath()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "My Computer"
.Title = "Please navigate to the Mentor Design Container"
.Show
If .SelectedItems.Count < 0 Then
MentDesCont = .SelectedItems(1)
End If
End With
End Sub



Function PathExists(pname) As Boolean
' returns true if the path exists
On Error Resume Next
PathExists = (GetAttr(pname) And vbDirectory) = vbDirectory
End Function



Function FileExists(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.LookIn = MentDesCont
.Execute
If .FoundFiles.Count = 1 Then
FileExists = True
Else
FileExists = False
End If
End With
End Function


Tom Hutchins

How to change directory
 
You are assigning values to local variables, which have no value when you
reference them from other procedures. You refer to MentDesContPath but have
never assigned a value to it. You assign a value to MentDesCont in the
GetMentDesContPath procedure, but it ceases to exist when that sub ends. The
MentDesCont you refer to elsewhere is a different variable. The following
works for me if I have assigned a value to geoms_ascii:

Option Explicit
Dim MentDesContPath As String

Private Sub CommandMAINBUTTON_Click()
Call GetMentDesContPath
'Check to see if GEOMS ASCII and error out if it does not geoms_ascii
'Check to see if design container has /pcb subdir
If PathExists(MentDesContPath & "pcb") = False Then
MsgBox "Invalid Mentor Design container specified, exiting"
Exit Sub
Else
ChDir MentDesContPath
If FileExists(MentDesContPath, geoms_ascii) = False Then
MsgBox "Couldn't find geoms_ascii file, exiting"
Exit Sub
Else
'open geoms_ascii file
End If
End If
End Sub

Private Sub GetMentDesContPath()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "My Computer"
.Title = "Please navigate to the Mentor Design Container"
.Show
If .SelectedItems.Count < 0 Then
MentDesContPath = .SelectedItems(1)
End If
End With
End Sub

Function PathExists(pname) As Boolean
' returns true if the path exists
On Error Resume Next
PathExists = (GetAttr(pname) And vbDirectory) = vbDirectory
End Function

Function FileExists(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.LookIn = MentDesContPath
.Execute
If .FoundFiles.Count = 1 Then
FileExists = True
Else
FileExists = False
End If
End With
End Function

Hope this helps,

Hutch

"Varun" wrote:

Folks,

VBA newbie here...I am unable to "change directory" to a folder name that (I
think) is (already) being returned to me via the Sub (I think I need a
function in order to do this but not sure).

The code is below. The main program right below calls out 2 subs and 1
function. What I need to do is chdir to a folder that is returned to me by
the Sub GetMentDesContPath. Do I need to change the Sub to Function in order
to accomplish this?

The program errors out at ChDir MentorDirectory

Thanks,
Varun




Private Sub CommandMAINBUTTON_Click()

Call GetMentDesContPath
MentorDirectory = MentDesContPath

'Check to see if GEOMS ASCII and error out if it does not geoms_ascii
'Check to see if design container has /pcb subdir

If PathExists(MentDesCont & "pcb") = False Then
MsgBox "Invalid Mentor Design container specified, exiting"
Exit Sub
Else
ChDir MentorDirectory

If FileExists(MentDesCont, geoms_ascii) = False Then
MsgBox "Couldn't find geoms_ascii file, exiting"
Exit Sub
Else
'open geoms_ascii file

End If

End If

End Sub


Sub GetMentDesContPath()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "My Computer"
.Title = "Please navigate to the Mentor Design Container"
.Show
If .SelectedItems.Count < 0 Then
MentDesCont = .SelectedItems(1)
End If
End With
End Sub



Function PathExists(pname) As Boolean
' returns true if the path exists
On Error Resume Next
PathExists = (GetAttr(pname) And vbDirectory) = vbDirectory
End Function



Function FileExists(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.LookIn = MentDesCont
.Execute
If .FoundFiles.Count = 1 Then
FileExists = True
Else
FileExists = False
End If
End With
End Function


Varun

How to change directory
 
Thanks Tom. Yeah, I had multiple errors there.

Modifying per your suggestions works now.

Thanks again.

"Tom Hutchins" wrote:

You are assigning values to local variables, which have no value when you
reference them from other procedures. You refer to MentDesContPath but have
never assigned a value to it. You assign a value to MentDesCont in the
GetMentDesContPath procedure, but it ceases to exist when that sub ends. The
MentDesCont you refer to elsewhere is a different variable. The following
works for me if I have assigned a value to geoms_ascii:

Option Explicit
Dim MentDesContPath As String

Private Sub CommandMAINBUTTON_Click()
Call GetMentDesContPath
'Check to see if GEOMS ASCII and error out if it does not geoms_ascii
'Check to see if design container has /pcb subdir
If PathExists(MentDesContPath & "pcb") = False Then
MsgBox "Invalid Mentor Design container specified, exiting"
Exit Sub
Else
ChDir MentDesContPath
If FileExists(MentDesContPath, geoms_ascii) = False Then
MsgBox "Couldn't find geoms_ascii file, exiting"
Exit Sub
Else
'open geoms_ascii file
End If
End If
End Sub

Private Sub GetMentDesContPath()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "My Computer"
.Title = "Please navigate to the Mentor Design Container"
.Show
If .SelectedItems.Count < 0 Then
MentDesContPath = .SelectedItems(1)
End If
End With
End Sub

Function PathExists(pname) As Boolean
' returns true if the path exists
On Error Resume Next
PathExists = (GetAttr(pname) And vbDirectory) = vbDirectory
End Function

Function FileExists(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.LookIn = MentDesContPath
.Execute
If .FoundFiles.Count = 1 Then
FileExists = True
Else
FileExists = False
End If
End With
End Function

Hope this helps,

Hutch

"Varun" wrote:

Folks,

VBA newbie here...I am unable to "change directory" to a folder name that (I
think) is (already) being returned to me via the Sub (I think I need a
function in order to do this but not sure).

The code is below. The main program right below calls out 2 subs and 1
function. What I need to do is chdir to a folder that is returned to me by
the Sub GetMentDesContPath. Do I need to change the Sub to Function in order
to accomplish this?

The program errors out at ChDir MentorDirectory

Thanks,
Varun




Private Sub CommandMAINBUTTON_Click()

Call GetMentDesContPath
MentorDirectory = MentDesContPath

'Check to see if GEOMS ASCII and error out if it does not geoms_ascii
'Check to see if design container has /pcb subdir

If PathExists(MentDesCont & "pcb") = False Then
MsgBox "Invalid Mentor Design container specified, exiting"
Exit Sub
Else
ChDir MentorDirectory

If FileExists(MentDesCont, geoms_ascii) = False Then
MsgBox "Couldn't find geoms_ascii file, exiting"
Exit Sub
Else
'open geoms_ascii file

End If

End If

End Sub


Sub GetMentDesContPath()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "My Computer"
.Title = "Please navigate to the Mentor Design Container"
.Show
If .SelectedItems.Count < 0 Then
MentDesCont = .SelectedItems(1)
End If
End With
End Sub



Function PathExists(pname) As Boolean
' returns true if the path exists
On Error Resume Next
PathExists = (GetAttr(pname) And vbDirectory) = vbDirectory
End Function



Function FileExists(path, fname) As Boolean
With Application.FileSearch
.NewSearch
.Filename = fname
.LookIn = MentDesCont
.Execute
If .FoundFiles.Count = 1 Then
FileExists = True
Else
FileExists = False
End If
End With
End Function



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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com