Discussion Locked

Converting HTML file tree into Word

0 helpful
14 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
I need to convert a directory tree of HTML files to one large MS Word document, keeping chapters, links, and graphics intact. Is there an obscure feature of MS Word that will allow this to be done?
If not, is there a program available that can do this without a whole lot of "cut and paste" ?

If you need more info to answer this question, please contact me.

Thanks for your help,

Daniel Ahrens
danielahrens@fortna.com

14 replies

by ann777

What you need is a scrolling screen shot program. I use SnagIt... and it works very well (even though my purchased version is outdated).

You can download this shareware from www.download.com -- it'll work for 45 days -- to purchase is $40.00.

by ann777

You will need to capture the pages from your web browser and then take the capture and insert them into MS Word. It works great for putting together instructions with examples!

by shanghai sam

Anna777 - I appreciate your input, however, as I stated in my question, I need to preserve links and I don't want to do all cut and paste. I trialed SnagIt, and while it was a great program in itself, it saves the HTML pages as a graphic, which doesnot preserve the hyperlinks. Also, using the method you suggested would essentially be the same as "cut and paste".

Thanks for the suggestion, but unfortunatley it's not what I'm looking for. :(

Regards,
Daniel

by raffi_

Hello Dan. I'm afraid your options here are a little limited.

If this is something you need to accomplish on a regular basis with this "tree" you should examine the Word on-line help regarding Master Documents & Subdocuments.

For the first 'go-round' you will probably have to cut'n'paste or drag'n'drop

Sorry the news wasn't better . . .

Regards
Raffi_

by diamond dan

Raffi - Thanks for looking into the problem, your time is appreciated, however I am still at square one with the ol' "cut n' paste". I will do this if necessary, but I am holding out for a better mousetrap. I'm sure that it's out there.

-Daniel

by peyison

Dan,

This actually shouldn't be that hard to do using VBA. Instead of cutting and pasting, you can do an Insert -> File... to bring in each HTML file. This will keep the links and graphics intact. I'm not sure what you mean by chapters, but you can insert a page break or section break for each HTML file, or for each sub-directory in your tree.

The VBA code would loop through each subdirectory in the tree and "Insert -> File" each HTML file it finds.

If this is a route you're interestedin, let me know and I can post the code to do this.

by diamond dan

Peyison - First off - don't believe the rating of unacceptable. I found your input quite thought provoking, however to post a reply and to keep the question open I had to make it unacceptable. Sorry. :(

I would be interested in seeing the code. One question though, how can I ensure that the script pulls the files into the document in the correct order, as these are not organized alphabetically or chronologically? Also, wouldn't the links have to be converted into bookmarks for them to work? At any rate, I think that you are on the right track. Please post the code. Thanks for your input.

Regards,
Daniel

by peyison

Daniel,

I didn't consider that the links would link to other pages being imported. This makes things a little more complicated, but it is still doable. You're right - you'd probably want to create a bookmark for each page. You'd then have to convert the links (which become Field Codes in Word) so that they point to the bookmark instead of the HTML page.

As far as file order, maybe the best thing to do then is to create a text file that has the files and directories to import in the properorder. The VBA code could then use this file when importing.

Two other things come to mind:

1) It would be best if none of the files had the same name (e.g. different directory, same name). You'd probably want to use the filename as the bookmark name, so the filenames should be unique.

2) It would make things a lot easier if none of the links were to HTML bookmarks in the same page, or other pages. That is, all the HTML links are just links to pages. Each page that is imported would only have one bookmark.

Let me know what you think, and then I'll put some code together.

by diamond dan

.

Peyison,

Sorry that TechRepublic makes it hard to successfully carry on a dialogue. I tried to post a comment to the discussion without rating your answer, but it was placed at the top of the page, directly under my original post.

-Dan

by diamond dan

Peyison,

The conversion of links to bookmarks can be done without too much trouble, and a text file that contains the file order already exists, albiet in a crude fashion, but it can be reworked rather easily.

To my knowledge, no 2 files have the same name, so unique naming is accomplished.

Some of the HTML pages have links to full screen graphics at the bottom of the same page, but the pages that have this are few. Not a problem here.

It looks like all of the issues thus far are minor. I think that I can definitely work with the constraints of the code. The biggest issue that I had was the raw importation of all of the files in the correct order without a lot of leg work, and it looks like you have that issue in hand.

Thanks, look forward to seeing the code.

by peyison

Daniel,

Hopefully the changes they are making this weekend will take care of some of these communication issus.

Here's the basic code to do this. There are three routines, ImportHTML is the main one. It will read filenames from the specified text file. The format of the text file should be like:

-----------
c:\htmlfiles\file1.htm
c:\htmlfiles\file2.htm
...
------------

It will set a bookmark for each html file, import the file and then go through and convert all of the links to be links to bookmarks.

Issues:

1) All hyperlinks are converted - if you have any external links, or links to bookmarks within the page, some additional code will be needed to skip them.

2) It would be best if images have absolute path/filenames. If not, the image files will have to be moved to the relative location to the Word doc. e.g. if you have an image with src=mypic.gif, it will have to be placed in the same folder as the Word doc.

3) This code has no error checking.

Let me know if this is what you were thinking of, and if you have any questions.

Sub ImportHTML()
Dim sFileName As String
Dim iFile As Integer
Dim sText As String
Dim sHTMLFile As String
Dim sBookmark As String

sFileName = "c:\htmlfiles\filelist.txt"
iFile = FreeFile

Open sFileName For Input As iFile

Do Until EOF(iFile)
Line Input #iFile, sText
If Len(sText) > 0 Then
sHTMLFile = Trim$(sText)
sBookmark = GetBookmark(sHTMLFile)
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=sBookmark
Selection.TypeParagraph
Selection.InsertFile FileName:=sHTMLFile
Selection.InsertBreak Type:=wdPageBreak
End If
Loop

Close #iFile

ConvertLinksEnd Sub

more to follow....

by peyison

(hopefully the TechRepublic update will allow us to post without losing white space as well....)

Sub ConvertLinks()
Dim oHyperlink As Hyperlink
Dim sAddress As String
Dim sBookmark As String

'loop through hyperlinks and convert external links to bookmark links
For Each oHyperlink In ActiveDocument.Hyperlinks
sAddress = Trim$(oHyperlink.Address)
If Len(sAddress) > 0 Then
sBookmark = GetBookmark(sAddress)

oHyperlink.SubAddress = sBookmark
oHyperlink.Address = ""
End If
Next
End Sub

Function GetBookmark(asFileName As String)
Dim iPos As Integer
Dim sTemp As String
iPos = InStrRev(asFileName, "\")
sTemp = Mid$(asFileName, iPos + 1)
iPos = InStr(sTemp, ".")
GetBookmark = Left$(sTemp, iPos - 1)
End Function

by shanghai sam

.

Peyison -
Many thanks, your script 'saved the day'. Importation of HTML files was flawless.

Just one problem, I had problems with the conversion of the hyperlinks. Some converted to bookmarks ok, while others didn't, causing the macro to report an error. All of the links are relative to the document in which they appear, detailing the relative path if the file in question is not in the same sub-directory.

Is this a problem for the script to handle? Is there a way that the hyperlinks need to be re-written so that the script can convert them correctly? I am very 'green' on Visual Basic scripts, so I wasn't able to find the problem on my own. I have posted the error message, as well as the line highlighted in the debugger, and the values shown on mouse-over of the variables.

Error Message:
>>Run-time error '5':
>>Invalid procedure call or argument

In debugger:

Highlighted error is in this function:
>>Function GetBookmark(asFileName As String)

Highlighted Line:
>>GetBookmark = Left$(sTemp, iPos - 1)

Mouse-over values:
>>GetBookmark = Empty
>>sTemp=""
>>iPos=0

I am closing out this question (your answer is the best and the one that I will use. Kudos!)

I very much appreciate your help with my problem, and would like to be able to continue our dialog, if you have the time to help me with the hyperlink problem. My email is below.

Many thanks,
Daniel Ahrens

danielahrens@fortna.com

This discussion is closed to new replies.