Inserting text in HTML/text file

  • This post is a response to a question I posted on an MS Forum. I have asked Dave if I can place the answer here as it requires a workbook and there is no facility to post a workbook on the MS Forum.


    Although the question relates to html files produced by MS Publisher the workbook may of use to others who wish to insert text in a text file.


    If you have arrived here from the MS Forum then enjoy this excellent forum!


    Quote from "a9192Shark summary of discussion on MS Forum"

    Using MS Publisher it is not possible to enter an HTML code fragment into a header. If you need code in teh file then sometimes you can post it in the body, but some code must be in the header. The only way to do this is to open the html file and copy and paste each time. However this becomes very frustrating if you keep rebuilding the files.


    The attached workbook will allow the user to search through html files and insert text after specified text. This has been used with success on my (Alan Forster) web pages. I may improve the workbook over time as I require and will post different versions.


    Note that the workbook was developed for my specific purpose and is very 'quick and dirty'. It may require some modification for your purpose, it is therefore provided without warranty!


    Good luck and I hope that this helps,


    A.

  • Re: Inserting text in HTML/text file


    Hi Alan,


    thanks for the code! What I've been thinking since long:


    I'd like to search in a html file for a specific string, namely <!==begin of table 1==>


    as well as look for <!==end of table 1==>


    and delete all text in between. Is this possible? Because then, I can fill some new text between these two marks, and have replaced the old text in a very fast way.


    Can your magic fingers write some code for that too? Perhaps it could fit in the code you just wrote?


    Thanks in advance


    Wigi

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: Inserting text in HTML/text file


    Wigi,


    Do you follow the VBA in the workbook?


    The code basically:
    1. Opens the html file and a temporary text file
    2. Reads each line of the html file until it finds the searchfor text, writing each line that it reads out to the temporary file.
    3. When it finds the searchfor text it outputs the text that I want to output
    4. Resume reading the file and writing out each line.


    What you will need to do is modify item 3 such that instead of writing out my text it writes out what you want. Then start reading text in until it finds the second search for phrase and only then start writing out.


    I am unlikely to get a chance in the next two weeks to answer any questions on the board as I am going skiing next week (hooray) and have a ton to do before then!


    Hope the description above helps.


    A.

  • Re: Inserting text in HTML/text file


    Well Alan,


    I asked you because I was able to write a bit of code to do what I want, and often it works well. But quite a few times, rectangles are appearing in Notepad when I open the htm file. Copying everything to Word, the rectangles are'nt visible anymore!


    The search for the text strings messes up, and the new text either is added at the end of the file, or is not added at all.


    If you still have the time to reply, please do so, and otherwise no problem. I'll do it manually (a lot of work) and I will certainly try to change your code a bit to see if that helps.


    Thanks Alan and enjoy your skiing!


    Wigi

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: Inserting text in HTML/text file


    Hi Wigi,


    Whilst Alan is off enjoying the slopes maybe you could post a test html file and the new text you want inserted. I'm sure we can put something together to save you a lot of work ;)

    [h4]Cheers
    Andy
    [/h4]

  • Re: Inserting text in HTML/text file


    Hi Andy


    thanks for the offer!


    Here we go:


    the attachment has the original Excel sheet with 2 tables. There's a macro called 'export' to put the table contents and formatting to a filled called 'Boerenbond.htm'. Boerenbond.htm is in a folder called 'website' on my C-drive.


    The original file has 6 tables (and larger), I ripped the inessential things and slimmed it down to 2 small tables.


    The code looks in 'Boerenbond.htm' for a string depending on which table it will export, does the exporting to a temporary file 'Boerenbondtemp.htm' which is afterwards renamed and deleted.


    When all goes well, 'Boerenbond.htm' gets no invisible rectangles, the blank lines remain, etc. After a few times exporting, the file becomes as the other example in the attachment. You'll notice immediately.


    Thanks in advance,


    Wigi

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

  • Re: Inserting text in HTML/text file


    I tested it a few times, and until now, no hated ugly rectangles show up.


    One word Andy:


    GREAT!


    Thanks


    Wigi

    Regards,


    Wigi


    Excel MVP 2011-2014


    For more Excel memes: visit http://www.wimgielis.com ==> English articles ==> Excel memes


    -- Topics without [CODE] tags or a meaningful topic title, will be neglected by me (and probably many others as well) --

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!