+ Reply to Thread
Results 1 to 2 of 2

Path/file error and catastrophic failure after inactivitiy

Hybrid View

nomorefun Path/file error and... 08-25-2012, 10:56 AM
snb Re: Path/file error and... 08-25-2012, 11:40 AM
  1. #1
    Registered User
    Join Date
    08-07-2012
    Location
    blah
    MS-Off Ver
    Excel 2003
    Posts
    2

    Path/file error and catastrophic failure after inactivitiy

    Added some functionality to a project I am working on and now I'm getting the above error if I leave the project open over night.

    I added a userform that allows the user to change the default save directory. Anyone see the problem?

    userform code:
    Private Sub UserForm_Initialize()
        Dim myString As String
        myString = Range("ctrWorkDir")
        
        If myString = "" Then
            myString = ActiveWorkbook.Path
        End If
        
        dirBox.Text = myString
    End Sub
    
    Private Sub editButton_Click()
    Dim myString As String
        Me.Hide
        myString = GetFolder(dirBox.Text)
        dirBox.Text = myString
        Me.Show
    End Sub
    
    
    Private Sub saveButton_Click()
    Dim myRange As Range
    Set myRange = Range("ctrWorkDir")
    
        
        If Not FolderExists(dirBox.Text) Then
            MsgBox "Invalid folder, try again."
            Exit Sub
        End If
        Me.Hide
        myRange = dirBox.Text
        Load formOptions
        
        With formOptions
          .StartUpPosition = 0
          .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
          .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
          .Show
        End With
            
        Unload dirEdit
    End Sub
    
    
    Private Sub cancelButton_Click()
    Dim myString As String
    myString = Range("ctrWorkDir")
    
        Me.Hide
        
        If myString = "" Then
            myString = ActiveWorkbook.Path
        End If
        dirBox.Text = myString
        
        Load formOptions
        
        With formOptions
          .StartUpPosition = 0
          .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
          .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
          .Show
        End With
        
        Unload dirEdit
    End Sub
    "folder picker"
    Function GetFolder(InitDir As String) As String
    
    Dim fldr As FileDialog
    Dim sItem As String
    sItem = InitDir
    
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        
        If Right(sItem, 1) <> "\" Then
            sItem = sItem & "\"
        End If
    
        .InitialFileName = sItem
    
        If .Show <> -1 Then
            sItem = InitDir
            Else
            sItem = .SelectedItems(1)
        End If
    End With
    
    GetFolder = sItem
    Set fldr = Nothing
    
    End Function
    folder validation
    Option Explicit
    
    Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
    Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
    
    Public Function FileExists(pstrFile As String) As Boolean
        FileExists = (PathFileExists(pstrFile) = 1)
        If FileExists Then FileExists = (PathIsDirectory(pstrFile) = 0)
    End Function
    
    Public Function FolderExists(pstrFolder As String) As Boolean
        FolderExists = (PathFileExists(pstrFolder) = 1)
        If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
    End Function
    Thanks in advance!

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Path/file error and catastrophic failure after inactivitiy

    Avoid more than 1 userform in a workbook.

    Private Sub UserForm_Initialize()
      dirBox.Text = iif(Range("ctrWorkDir").value="",ThisWorkbook.Path,Range("ctrWorkDir").value)
    End Sub
    Private Sub editButton_Click()
      do
         dirBox.Text = GetFolder(dirBox.Text)
      loop until dir(dirbox.Text,16)<>""
    End Sub
    Private Sub saveButton_Click()
      Hide
      formOptions.show
    End Sub

    Private Sub cancelButton_Click()
      Hide
      formOptions.show
    End Sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1