+ Reply to Thread
Results 1 to 13 of 13

Help with adapting a text import code?

  1. #1
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Question Help with adapting a text import code?

    Hi all,

    I found this great script that imports the content of multiple text files into rows including the file name:

    Please Login or Register  to view this content.
    As you can see, I'm using this to import HTML files instead of text. This works ok, however,

    - Is there a way of changing this to have a file picker instead? Either multi-select HTML files from a folder or select a folder that contains all the HTML files to import?
    - How can I specify which worksheet to copy to? It looks like it's currently importing to whatever sheet is active?
    - How can I change this to recognize .txt and .htm and .html files?
    - Weirdly, on import, it doesn't seem to import in UTF8 (or equivalent) and I get a lot of random characters, e.g. "<!doctype html>", "Don’t" (should read "Don't") and if the text contains superscript it shows e.g. "¹" instead of "¹". Is there a code snippet I can add to optimise this?

    Still a bit of a newbie when it comes to VBA so hoping someone can help?

    Thank you!

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    Possibly...
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Hi dangelor,

    Thanks so much for your help! Just tried it and the folder picker seems to work - YAY!

    The only issue is that:
    1. It now seems to paste the content to the 1st worksheet in my file. Is there any way to add which worksheet to paste into? Instead if "With Sheet1" can I add a worksheet name?
    2. It imports the file path into column A now, instead of the file name without the file extension.
    3. The HTML still looks off (weird characters, such as ), plus it adds some rows about XML document properties that weren't there before (and aren't actually in the file to import), e.g.

    Please Login or Register  to view this content.
    I remember that I had used another script to export HTML in different languages a while ago and that referenced the following, which (I believe) made it export in UTF8. Maybe that can be re-used somehow also to import?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Thanks so much for the help - appreciate it!

  4. #4
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Also found this if it helps:

    Please Login or Register  to view this content.
    This is supposed to read data in UTF8 format, but don't think the loadfromfile bit is needed.

  5. #5
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    Try this version...
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Hi dangelor,

    Thanks so much for helping with this! I owe you!

    Just tested, but seeing some unexpected behaviour:

    1. It imports one file, but then throws a "run-time error '1004': That name is already taken. Try a different one." on line With Worksheets.Add .Name = "Test123"
    2. Even though I specified the .Name to be = "Test123" it seems to create that + another empty worksheet.
    3. On Re-import it overwrites the content in the "Test123" worksheet (which is the expected behaviour), but always throws the error after the first file (see 1.) and creates another empty worksheet (see 2.).
    4. It only seems to import .HTML files, but not .HTM or .TXT files - can these be added?
    5. Good news is that the import now seems to be in UTF-8 (at least not getting the weird characters anymore)

    I've attached the workbook and some examples files to import if that helps?

    Other than that, this is doing exactly what I wanted so thanks so much!!
    Cheers from London!
    Attached Files Attached Files

  7. #7
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    Try...
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Hey dangelor!

    This works PERFECTLY!

    The only thing I noticed is that keeps on adding rows on re-import rather than replacing the content, but managed to fix that by adding

    Please Login or Register  to view this content.
    ..after the first End With. Seems to have done the trick.

    Thanks so much again! I owe you a virtual beer ;-)

  9. #9
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    Glad it finally works!


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  10. #10
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Hey dangelor!

    Hope all is well and sorry for another question on this...

    How would I need to tweak this in order to select a single .txt/htm/.html file, rather than all files in a folder?

    I tried changing MsoFileDialogType.msoFileDialogFolderPicker to MsoFileDialogType.msoFileDialogFilePicker, but sadly that doesn't seem to be all :-(

    Please Login or Register  to view this content.
    Hope that's a quick fix for a pro like you?

    Thank you!!

  11. #11
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    Single file version...
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Help with adapting a text import code?

    Hi dangelor,

    ACE! That did it Thanks so much for all the help!

    Have a great Sunday!

  13. #13
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Help with adapting a text import code?

    You're welcome and thanks for the rep points!

+ 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. Adapting existing code
    By jonhat in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-06-2020, 10:12 AM
  2. Adapting this Save as PDF code to Mac
    By MagnusNovak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2020, 11:18 PM
  3. Adapting VBA Code
    By Paul Cooke in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-13-2013, 10:12 AM
  4. [SOLVED] Import text file from VBA code; import all numbers as text
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-19-2012, 05:19 PM
  5. Adapting this code to have a set range
    By MasonSinclair in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2011, 04:50 AM
  6. Adapting VBA Code
    By smurray444 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2007, 05:30 AM
  7. Adapting some code
    By chrisrowe_cr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-16-2005, 09:05 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