+ Reply to Thread
Results 1 to 37 of 37

Error opening text file due to encoding issue.

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Error opening text file due to encoding issue.

    hi,

    i have run into a very interesting problem and i am not sure if its my code or the text file.

    First: The text file in question has an extension of ".csv" and i can NOT do anything about it but the file is actually "tab delimited".
    When i open the file using this command in excel vba

    Please Login or Register  to view this content.


    apparently after this

    Please Login or Register  to view this content.
    x should be an array where every row is another element in the array. AFter i do the above code there is only a single element in the "x" array. Also there appears to be lots of "tab" values in the first part of the x-element. Like the first 50 or more places appear to be maybe tab characters.


    However, if i take the "same" text file open it in notepad and then save it .........

    x is as it should be where there are multiple elements in the array where each element is a row of data in the file.


    i know it would be helpful if i posted the text file but i cant as it has propriatary information ...........


    please help!!!!!!!!!!! any ideas at all.......is it the file or the way i am trying to read in the file...........
    Last edited by welchs101; 09-25-2014 at 06:48 AM.

  2. #2
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    Hi,
    Maybe the end of the line is not vbCbLf, but only one of them, try to split using only one of the two or use vbNewLine.
    Last edited by buran; 09-16-2014 at 01:19 PM.
    If you are pleased with a member's answer then use the Star icon to rate it.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    not sure what you mean...........


    also, new information: i tried the code below and i got something interesting:
    the first characters in the first row are a y with two dots over the "y" and then what looks like a "p" but is not really a p

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    i tried vbnewline as you suggested....it gave same results as the case where i used vbCrLf

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: opening text file with notepad does not show whats really in file when using excel vba


    Hi,

    seems not to be a very interesting problem but a lack of file structure knowledge like in your still opened thread …

    Join a source csv file and this time a workbook with a desired result worksheet !

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    here is a screen shot of what "txt" and "x" look like
    Attached Images Attached Images

  7. #7
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    End of line is platform specific, i.e. CrLf, Cr, Lf. So when you use
    Please Login or Register  to view this content.
    you assume it's vbCrLf, but maybe that's not the case...

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    marc, if new how to do what your saying i would......but i dont.........i really wish i did though

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    buran, what do you think i should try to figure it out?

  10. #10
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    Did you try

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    just tried both and neither worked.

  12. #12
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    without sample file I'm out of ideas.
    You didn't say what happens if you read the file line by line? and also what's the ultimate goal - you said you ahve problem when you try split the entire file but what you want to do after that?

  13. #13
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    ok....i have repeated this twice to make sure it works so here it is.......

    i open the file in question using EXCEL. I then save it as a "Text(Tab delimited)" file type and then run using my macro and IT WORKS!

    However, if i open the same file using notepad and save it it does NOT work....i get the weird characters i mentioned before in the image i sent out in another post.........could this be an "encoding" issue

  14. #14
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    ok...i was able to MAKE an input file which has the same problem as my original file............enclosed is the input file.

    does this help identify the problem?
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    09-16-2014
    Location
    United States
    MS-Off Ver
    Office 2013
    Posts
    11

    Re: opening text file with notepad does not show whats really in file when using excel vba

    This seems like an encoding issue to me. It's actually moderately similar to the thread I just started. My guess would be that Excel is interpreting the file saved by Notepad as some encoding type which doesn't actually fit. I'm not sure if there's a way to force Excel to open a file with different encoding, but if so, that might solve it. (Ironically, I didn't actually see you mention encoding before typing all that out.)

  16. #16
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    I give up :-(

  17. #17
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    well thanks for trying.......

    can someone help? anyone? i have been at this all day trying to figure this out and i have no idea how to solve it.......

    my two main questions are
    1. why does notepad not show the supposed special characters
    2. why does the code not read it in properly. the code works i know that when i open the input file and actually save it from excel as a Text(tab delimited file).

  18. #18
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    buran, did you see the special characters in the input file i sent when you run my code?

  19. #19
    Registered User
    Join Date
    09-16-2014
    Location
    United States
    MS-Off Ver
    Office 2013
    Posts
    11

    Re: opening text file with notepad does not show whats really in file when using excel vba

    Nevermind...

  20. #20
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: opening text file with notepad does not show whats really in file when using excel vba


    I got error #62 with the joined file, like it's corrupted …

  21. #21
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    figured out problem and someone found soln for me


    it was the encoding!!!!!!! that was causing the problem. My original input file was enclosed as "UCS-2 Little Endian". However, when i imported the file into excel and then saved it as a "Text(tab delimited)" file my program worked the encoding of this file was "ANSI as UTF-8". So the ansi utf-8 worked but the other did not........

    in my code i was using the following to open the file.

    Please Login or Register  to view this content.
    a friend looked it up at this location
    HTML Code: 
    and from this i changed my code to the following

    Please Login or Register  to view this content.
    and it worked!!!!!!!!!!!!!!!!!!!!!!!!!! no special characters found..............

  22. #22
    Registered User
    Join Date
    09-16-2014
    Location
    United States
    MS-Off Ver
    Office 2013
    Posts
    11

    Re: opening text file with notepad does not show whats really in file when using excel vba

    Alright, I managed to get it working (to the point that I can see your problem, not to the point of solving it, sorry). I do see the messed up characters. Not sure what's causing it yet. Anyway, open a new spreadsheet, attach this subroutine, modify it so it points to the file (from this post), and then run it. The first cell ends up with "ÿþTrigger Name".
    Please Login or Register  to view this content.
    EDIT: Oh, good

  23. #23
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: opening text file with notepad does not show whats really in file when using excel vba


    The method is documented in the VBA inner help …

  24. #24
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    marc, what method are you referring to.........if you can forward a link that would be great.

    i still wonder why notepad does not show the "extra characters" associated with the encoding......what is encoding anyway.

    also, and maybe this should be a new thread........but is there a way to identify what type of encoding a text file has so that if it has different types i could select different formats in the fileopen command so that it gets read in correctly?

  25. #25
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    i think i pasted the wrong html link from before below is the opentextfile link

    HTML Code: 

  26. #26
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    so interesting result and again fuels my desire to determine encoding of text file.........

    recall that my original code statement

    Please Login or Register  to view this content.
    and in this code the format is "-2 = TristateUseDefault" works with those files saved from excel as Text(Tab delimited)

    and the new code

    Please Login or Register  to view this content.
    and in this code above the format is set to "-1 = TristateTrue"........using the "-1" will not open a text file correctly if this text file was created from excel saved as Text(Tab delimited).


    so if i get a file like i am supposed to then i should use the "-1" however, if for some reason i get a different type of file then my code wont work.

  27. #27
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: opening text file with notepad does not show whats really in file when using excel vba


    No link needed 'cause method is already in VBA inner help …

    Notepad doesn't show that special characters 'cause they are the encoding code and its detects it …

  28. #28
    Registered User
    Join Date
    09-16-2014
    Location
    United States
    MS-Off Ver
    Office 2013
    Posts
    11

    Re: opening text file with notepad does not show whats really in file when using excel vba

    This page suggests a solution for checking encoding.

    Quote Originally Posted by Marc L View Post

    No link needed 'cause method is already in VBA inner help …

    Notepad doesn't show that special characters 'cause they are the encoding code and its detects it …
    He asked which method you were referring to.
    Last edited by Xander9009; 09-16-2014 at 07:29 PM.

  29. #29
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    thanks xander i will check out the page

  30. #30
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    xander just an fyi........i tried running the code provided in that link you sent and it did not run........i am thinking this code may not work for excel vba.......

  31. #31
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    apprarently it program, when i try to run it, stops at this step

    Please Login or Register  to view this content.
    i am not sure what ADODM.Stream is but my guess is that is not compatible with excel vba.

  32. #32
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    got program to run but not sure its running correctly..........i changed some code to this

    Please Login or Register  to view this content.

    but it does not seem to output anything useful

  33. #33
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: opening text file with notepad does not show whats really in file when using excel vba

    You need to set a reference to one of the Microsoft ActiveX Data Objects libraries, or late bind the code.
    Remember what the dormouse said
    Feed your head

  34. #34
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    you must create reference to Microsoft ActiveX Data Objects Library.
    In VBE -> Tools -> References -> scroll until you find Microsoft ActiveX Data Objects Library and check it.
    usually there are severl such libraries e.g. 2.5, 2.6, etx.

  35. #35
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: opening text file with notepad does not show whats really in file when using excel vba

    What you have done is late binding that was mentioned by romperstomper

  36. #36
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: opening text file with notepad does not show whats really in file when using excel vba

    does anyone know how to edit the title........i was thinking this title does not really go with the thread.....but i did not know that when i first started it but now i do and i was thinking of changing the title

  37. #37
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: opening text file with notepad does not show whats really in file when using excel vba

    It's probably too late unless you get a Mod to do it, or simply start a new thread and link back to this one if the topic has changed.

+ 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] Loading the value of text file (notepad) to a textbox
    By nathan0519 in forum Excel General
    Replies: 4
    Last Post: 07-17-2014, 10:48 PM
  2. text file into Excel when opening Excel file or template
    By MarkInKeizer in forum Excel General
    Replies: 7
    Last Post: 07-17-2014, 04:07 PM
  3. Replies: 1
    Last Post: 08-30-2009, 11:26 AM
  4. [SOLVED] opening an excel file opens a duplicate file of the same file
    By skm in forum Excel General
    Replies: 1
    Last Post: 12-07-2005, 01:55 PM
  5. Print text file with notepad
    By side_ in forum Excel General
    Replies: 0
    Last Post: 02-04-2005, 02:01 PM

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