How to change directory

V

Varun

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
 
T

Tom Hutchins

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
 
V

Varun

Thanks Tom. Yeah, I had multiple errors there.

Modifying per your suggestions works now.

Thanks again.
 

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