Search code examples
vbawindowsexcelfilenames

Excel VBA code won't open a file named "CON.txt"


I'm processing a large text file with pipe-delimited data in it where each line starts with a text "record type" field--e.g., "APP", "ATT", "CON", "A", "T"... I need to split the file into a series of files with specific record types, so "APP.txt" has all the APP record types, etc.

What I'm doing is reading a line from the source file, grabbing the first field, checking if the token is in a collection and then, if the token is in the collection, opening that file and writing to it, or if the token is not in the collection, creating a new file, writing to it, and adding the token to the collection. My program works fine, except when creating the "CON.txt" file:

  • If I delete all the "CON" record types out of the source file, it works fine.

  • If a "CON" record appears on line 2, it bombs out on line 2 creating the file.

  • If a "CON" record appear on line 50, it bombs out on line 50.

  • If I add a p in front of each token so the CON tokens are now pCON tokens, everything works fine.

I'm mystified. Any idea why one specific file name would give this program conniptions? The code that actually opens the file (which throws the 75 run-time error) is:

Open sTgtDir & sToken & ".txt" For Append As #iFileNo

Where sTgtDir is a path ending with "\" that works in all the other cases, sToken is the record type token, and iFileNo is an integer based on the count of items in the Token collection.


Solution

  • DOS and Windows platforms won't open a disk file named CON (with any extension), because that is a special name referring to the "CONsole" (the screen). Sorry - you're out of luck! You'll simply have to use different token names (assuming you don't have the option of changing operating system). For example, before the Open line you could add:

    If UCase(sToken) = "CON" then sToken = "pCON"