+ Reply to Thread
Results 1 to 9 of 9

Runtime 1004 after prompting for filename but not when filename hard coded.

  1. #1
    Registered User
    Join Date
    12-03-2013
    Location
    Davis, CA
    MS-Off Ver
    Excel 2013
    Posts
    8

    Runtime 1004 after prompting for filename but not when filename hard coded.

    I'm using VBA in Excel 2013 to do something that shouldn't be hard. I want the user to be able to choose a file. When I open I dialog box to choose the file, and then try to open the file, I get a Runtime 1004 error "Sorry, we could find <filename>. Is it possible it was moved, renamed, or deleted?" However, when I hard code workbooks.open <filename with path> it opens fine. Here is the code to prompt for the file:

    Sub GetImportFileName2(spath, UFileName)
    Dim Filt As String
    Dim FilterIndex As Integer
    Dim FileName As Variant
    Dim Title As String
    Dim i As Integer
    Dim Msg As String
    ' Set up list of file filters
    Filt = "Text Files (*.txt),*.txt," & _
    "Comma Separated Files (*.csv),*.csv," & _
    "Excel Files (*.xlsx),*.xlsx," & _
    "All Files (*.*),*.*"
    ' Display *.xlsx by default
    FilterIndex = 4
    ' Set the dialog box caption
    Title = "Select a file to open:"
    ' Get the file name
    FileName = Application.GetOpenFilename _
    (FileFilter:=Filt, _
    FilterIndex:=FilterIndex, _
    Title:=Title, _
    MultiSelect:=True)
    ' Exit if dialog box canceled
    If Not IsArray(FileName) Then
    MsgBox "No file was selected."
    Exit Sub
    End If
    ' Display full path and name of the files
    For i = LBound(FileName) To UBound(FileName)
    Msg = Msg & FileName(i) & vbCrLf
    Next i
    spath = Left(Msg, InStrRev(Msg, "\"))
    MsgBox "Path: " & spath
    UFileName = Mid(Msg, InStrRev(Msg, "\") + 1)
    MsgBox "File: " & UFileName
    End Sub

    As you can see I have inserted a bunch of MsgBox calls so I can check the path and filename. Everything is fine. Then I do:

    Workbooks.Open spath & UFileName

    And I get the error (the path and filename shown in the error dialog box as not being found is the correct path and filename, so I'm concatenating fine). However, if I just do:

    Workbooks.Open FileName:="path and filename exactly as it shows in the error message"

    The file opens fine.

    Thanks for listening :-)

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    How exactly are you calling GetImportFilename2?

    PS Can you add code tags?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-03-2013
    Location
    Davis, CA
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    Sorry, Norie. I'm not a big forum question asker, obviously. OK:

    Please Login or Register  to view this content.
    And it is called thusly:

    Please Login or Register  to view this content.
    The MsgBox calls again show the correct path and filename.

    Thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    Have you tried just returning the filename and path in one string rather than separating them?
    Please Login or Register  to view this content.

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    Is the msgbox displaying the path showing it has a backslash at the end?
    It could be because a backslash is missing, just a thought.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    12-03-2013
    Location
    Davis, CA
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    Hi everyone. Thanks for the help. Norie, your easy fix did the trick.

    As it turns out, the culprit was extra spaces inserted when I converted the Variant that is returned by Application.GetOpenFilename to a String with this little loop:

    Please Login or Register  to view this content.
    I copied this code from somewhere and I can't say I understand why one would do it this way, now that I'm looking at it. And why the carriage returns (VbCrLf) is a mystery. Removing the carriage returns also ends up with working code but Norie's simple way is best.

    Thanks again.

  7. #7
    Registered User
    Join Date
    12-03-2013
    Location
    Davis, CA
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    For completeness: what am I doing when I write UFileName = FileName(1)? What does the (1) do? FileName is a Variant, UFileName is a String. I tried UFileName = CStrg(FileName) but it was unhappy.

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    FileName is given an array of values by Application.GetOpenFileName
    FileName(1) would refer to the first value in there

    If more than one file is selected, you can refer to them with FileName(2), FileName(3), etc.

  9. #9
    Registered User
    Join Date
    12-03-2013
    Location
    Davis, CA
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Runtime 1004 after prompting for filename but not when filename hard coded.

    Thanks, millz! That would have been my next issue - opening multiple files.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Find if filename exists and update variable within the filename structure
    By Lungfish in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-18-2013, 02:42 AM
  2. Replies: 0
    Last Post: 11-01-2012, 09:28 AM
  3. Code to create a custom .csv filename with version stamp in filename
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-10-2012, 04:07 AM
  4. Replies: 3
    Last Post: 07-09-2012, 03:09 PM
  5. Prompting a predefined filename to be saved
    By Bart V in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-19-2006, 05:40 AM

Tags for this Thread

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