+ Reply to Thread
Results 1 to 4 of 4

error 1004 using variable as a filename in opentext

Hybrid View

  1. #1
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    error 1004 using variable as a filename in opentext

    I am getting an error when I try to use a filename as a variable with the workbooks.opentext command. If I hardcode the file path and file name, it will work. The variable is being taken from a table on an xlsx file using the CELLS(1,1) and/or RANGE("A2") commands.

    I get the following error--that also includes my FILENAME in the error message:

    Runtime Error '1004'
    File could not be accessed. try one of the following
    - make sure the specified folder exists
    - make sure the folder that contains the file is not read-only
    - make sure the filename does not contain any of the characters < > ? [ ] : Sub01.txt or *
    - make sure file/path name does not contain more than 128
    WHY am I getting this error? How can I pass my filename as a variable into the OpenText command using VBA?

    Other code online shows that it should work... Should my fname and fpath variables not be declared as STRING?

    Please help! Thanks.

    Here is a snipet of my code:

    ' open topzerotable file
    ChDir "F:\Data\AllPMapTogether"
    Workbooks.Open Filename:="F:\Data\AllPMapTogether\TopZeroTable.xlsx"
    
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    f = 2       ' start with second row since first row are headings
    
    Do While (f <= 2) = True
    
        ' pull information for filename from xlsx file
        fname = Range("A" & f).Text
        fPath = Range("G" & f).Text
         
         ' opentext file
        ChDir "F:\Data\AllPMapTogether\AllExport"
        
        Workbooks.OpenText Filename:=fPath & fname, _
            Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
            , Space:=False, Other:=False, OtherChar:="x", FieldInfo:=Array(Array(1, _
            1), Array(2, 1)), TrailingMinusNumbers:=True

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: error 1004 using variable as a filename in opentext

    Just checking, but does your fPath end with "\"? If not, then you might be trying to open something like:

    "F:\Data\AllPMapTogether\AllExportTopZeroTable.xlsx"

    instead of

    "F:\Data\AllPMapTogether\AllExport\TopZeroTable.xlsx"

  3. #3
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: error 1004 using variable as a filename in opentext

    Quote Originally Posted by Paul View Post
    Just checking, but does your fPath end with "\"? If not, then you might be trying to open something like:

    "F:\Data\AllPMapTogether\AllExportTopZeroTable.xlsx"

    instead of

    "F:\Data\AllPMapTogether\AllExport\TopZeroTable.xlsx"
    Yes, my path has the quote included with it. I have also tried to omit the "\" and add it by:

    Workbooks.OpenText Filename:=fpath & "\" & fname,...
    but neither solved the error.

  4. #4
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: error 1004 using variable as a filename in opentext

    As I have troubleshot the problem, it appears to be an issue with reading a value into a program from a txt or xlsx file. The Workbook.OpenText will not work when I read the value using Cells() or Range(), and then put that variable in as the filename.

    When I hardcode the filename or declare it in the code, the Workbook.OpenText will work.

    Any thoughts as to why it won't read in the value from a txt/xlsx file? Any ideas how to solve it?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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