+ Reply to Thread
Results 1 to 3 of 3

Run Time Error 52- bad file name or number

Hybrid View

Keruck Run Time Error 52- bad file... 12-02-2008, 04:33 PM
Leith Ross Hello Keruck, I am... 12-02-2008, 06:56 PM
Keruck Leith- Thank you for your... 12-02-2008, 09:25 PM
  1. #1
    Registered User
    Join Date
    11-17-2008
    Location
    WA
    Posts
    11

    Run Time Error 52- bad file name or number

    I'm using the following code to locate all target xls files, open them, and check to see the last used cell in column A.

    Everything was working until I added the code to actually open the files and check the last used row; but now I'm getting a RTE 52 (bad file name or number), most often at the line marked with '*** below.

    The weird thing is that when I select 'debug' at the error, then debug.print Fil or debug.print UCase(Left(Dir(Fil), 5)), it gives me a valid result- it has the next filename just fine, but something else is going on and I haven't been able to figure it out. I tried with and without setting a new excel application for opening new files, and neither made a difference.

    I've also tried adding a 5 second wait where the DoEvents are, that didn't help. Is there a way to set timeout settings for a network drive, similar to what is shown here for http servers? http://msdn.microsoft.com/en-us/library/ms760403.aspx

    Any help would be greatly appreciated!
    Using XL2003 on WinXP
    Keith
    (apologies- at least in preview mode, all my code is left justified, even though in the text section I'm typing in, it is all indented)

    Option Explicit
     
    Sub SrchForFiles()
         ' Searches the selected folders and sub folders for files with the specified
         'extension.  .xls, .doc, .ppt, etc.
         'A new worksheet is produced called "File Search Results".  You can click on the link and go directly
         'to the file you need.
        Dim i As Long, z As Long, Rw As Long
        Dim ws As Worksheet
        Dim y As Variant
        Dim fLdr As String, Fil As String, FPath As String
        Dim LocName As String
        Dim PString As String
        Dim SummaryWB As Workbook
        Dim SummaryWS As Worksheet
        
        'grab current location for later reference, for where to paste final data
        Set SummaryWB = Application.ActiveWorkbook
        Set SummaryWS = Application.ActiveWorkbook.ActiveSheet
            
        y = "xls"
        'actual directory path masked for public posting
        fLdr = "\\share.companyname.com\directory\subdirectory\"
        
        'Application.ScreenUpdating = False
         
        With Application.FileSearch
            .NewSearch
            .LookIn = fLdr
            .SearchSubFolders = True
            .Filename = y
            Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
            On Error GoTo 1
    2:                              ws.Name = "FileSearch Results"
            On Error GoTo 0
            If .Execute() > 0 Then
                For i = 1 To .FoundFiles.Count
                    Fil = .FoundFiles(i)
                    'screen for target file names ("Multi*.xls")
                    If UCase(Left(Dir(Fil), 5)) = "MULTI" Then '*** THIS IS WHERE IT BAILS
                        'Exclude the template file that has no unique file ID, and is therefore only 34 chars long
                        If Len(Dir(Fil)) > 34 Then
                            'Remove the standard naming convention (left 31 characters of filename)
                            LocName = Right(Dir(Fil), Len(Dir(Fil)) - 31)
                            'Remove the ".xls" from the end of the filename
                            'Remainder of the filename (LocName) is the unique file ID
                            LocName = Left(LocName, Len(LocName) - 4)
                            
                            'Get file path from file name
                            FPath = Left(Fil, Len(Fil) - Len(Split(Fil, "\")(UBound(Split(Fil, "\")))) - 1)
                            'Get file information
                            If Left$(Fil, 1) = Left$(fLdr, 1) Then
                                If CBool(Len(Dir(Fil))) Then
                                    z = z + 1
                                    ws.Cells(z + 1, 1).Resize(, 5) = _
                                    Array(Dir(Fil), _
                                    LocName, _
                                    FileLen(Fil) / 1000, _
                                    FileDateTime(Fil), _
                                    FPath)
                                    ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
                                    Address:=.FoundFiles(i)
                                End If
                            End If
                            
                            'open the file and grab the data
                            'Dim oExcel As Excel.Application
                            'Dim oWB As Workbook
                            'Set oExcel = New Excel.Application
                            Workbooks.Open (Fil)
                            DoEvents
                            'Set oWB = oExcel.Workbooks.Open(Fil) 'Workbooks.Open(.FoundFiles(i1))
    
                            Dim LastRow As Long
                            LastRow = Sheets(1).Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                            LastRow = qexc_GetLastFilledCellInColumn 'Sheets(1).Range("A1").End(xlDown).Select
                            PString = PString & vbCrLf & LastRow
    
                            'Workbooks.Close 'Fil
                            Workbooks(Dir(Fil)).Close SaveChanges:=False
                            DoEvents
    
                            
                        End If
                    End If
                Next i
            End If
        End With
         
        MsgBox PString
              
        With ws
            Rw = .Cells.Rows.Count
            With .[A1:E1]
                .Value = [{"Full Name","Location","Kilobytes","Last Modified", "Path"}]
                .Font.Underline = xlUnderlineStyleSingle
                .EntireColumn.AutoFit
                .HorizontalAlignment = xlCenter
            End With
            .[F1:IV1 ].EntireColumn.Hidden = True
            On Error Resume Next
            Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
            Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
        End With
         
        'Application.ScreenUpdating = True
        Exit Sub
    1:          Application.DisplayAlerts = False
        Worksheets("FileSearch Results").Delete
        Application.DisplayAlerts = True
        GoTo 2
    End Sub
    Function qexc_GetLastFilledCellInColumn() As Integer
        Sheets(1).Cells(65536, 1).Select
        Selection.End(xlUp).Select
        qexc_GetLastFilledCellInColumn = Selection.Row
    End Function
    Last edited by Keruck; 12-02-2008 at 05:35 PM. Reason: Added code tags as per forum rules

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Keruck,

    I am assuming since these files are on a server that they are shared workbooks. If a workbook is in use when your macro attempts to retrieve it, an error will occur.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    11-17-2008
    Location
    WA
    Posts
    11
    Leith-

    Thank you for your response. I'm 100% certain that the error was not occuring due to other users, although maybe I was leaving hidden instances of Excel open? Since my original post, I've eliminated all the file manipulation other than

    Application.Workbooks.Open (Fil), False, True
    and
    Workbooks(Dir(Fil)).Close SaveChanges:=False
    At the moment, the code has quit crashing with the RTE52, but I haven't been able to figure out why, which means I'm all the more nervous about handing this off to another user for regular processing. I'll play a little more tomorrow and see if I can figure out what was causing the problem- maybe mid-day network traffic was just causing too much of a lag.

    When I was getting the error, I was running the macro several times consecutively (restart after each error), and it always stopped on a different file (I have 57 files total that meet my search criteria, and on any given run it might stop on 10-20 different files). These are files that are only updated by end users about once a month. Since I was getting stuck at a different file each time, I'm convinced that it is hardware or my code, since it would stop me on different files every few seconds.

    The weird part for me is that the line the debugger highlighted made no sense, because I could see that the variable had a valid value, and on that particular line I wasn't trying to open or close files, just run an if statement. Ah, the many intricacies of Excel that I just don't understand...

    Thanks again for your suggestion,
    Keith

+ 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