Macro to insert multipul HTML files in Word

richiwatts
11-26-2003, 08:27 AM
Hi,

I am trying to insert a number of HTML files into an MS Word document

With Word 2000 open I can press
Insert > File > filename.html

and the file will be placed into the Word document. However, I can only do one file at a time. The problem is that I need to import 168 html files from the same folder into one word document so that I can proofread all the text. I did the first 15 or so files and realised that it will take me ages and hoped that someone on the forum has a better idea as to how I could do this. Maybe a Macro that will import all the files or something.

It would also be really good if I could have the name of the file before each new import so that we know what HTML file we have to go back and change. However beggers can't be choosers

I would be really grateful if someone could help.

Thanks

Richi

Wamphyri
11-26-2003, 10:22 AM
If you have all the files in one folder it would be fairly easy using FSO
You'll need to add a reference to the Scripting Runtime library in the Tools -> References -> Microsoft Scripting Runtime

Dim FSO As FileSystemObject
Dim fsoFldr As Folder
Dim fsoFile As File
Dim i As Long
Set FSO = New FileSystemObject
Set fsoFldr = FSO.GetFolder("C:\HTML_FILES")
For Each fsoFile In fsoFldr.Files
With ActiveDocument.Range
.InsertAfter fsoFile.Path
.Collapse 0
.InsertFile FileName:=fsoFile.Path, Range:="", ConfirmConversions _
:=False, Link:=False, Attachment:=False
.Collapse 0
End With
DoEvents
Next
Set fsoFile = Nothing
Set fsoFldr = Nothing
Set FSO = Nothing

richiwatts
11-26-2003, 12:02 PM
Hi Wamphyri

Thank you very much that worked really well. As a complete beginner it took me a bit of a while to understand what to do with what you sent me but I got there in the end.

Thanks again
Richi

Wamphyri
11-26-2003, 01:02 PM
No problem. Is there any part of the code that you don't understand? Or do you feel you understand how the code works?

richiwatts
11-27-2003, 04:53 AM
Hi,

No I think I have understood it.

A couple of things that would make it very useful for future projects though.

Is it possible to select only certain file type. e.g *.html
Is it possible to change the format of the file name: maybe hav 5 carrige returns before the file name and make the file name red, bold ,size 14.

I also notice that it always puts the last file first and then the rest are in order.

None of the above is neccessary as it doesn't take two minute to do search and replaces but it is always handy to know.

Thanks again for your help, you saved me hours of manual labour :-)

Richi

Wamphyri
12-01-2003, 02:04 PM
Is it possible to select only certain file type. e.g *.html

Yes. (actually, it is a yes to both questions but I'll answer the second one tomorrow. Got to leave for kickboxing soon)
You could check the fsoFile.Path, (I think fsoFile.Name will also work, since I believe it returns the file extension), for the file extension to you are looking for. You could use InStr() to do this.

For Each fsoFile In fsoFldr.Files
'returns a 0 (false) if not found
If InStr(fsoFile.Name,".html") Then
With ActiveDocument.Range
.InsertAfter fsoFile.Path
.Collapse 0
.InsertFile FileName:=fsoFile.Path, Range:="", ConfirmConversions _
:=False, Link:=False, Attachment:=False
.Collapse 0
End With
DoEvents
End If
Next

richiwatts
05-18-2004, 01:54 PM
It has been a while so I will ask again

Is it possible to change the format of the file name: maybe have 2 carrige returns before the file name and make the file name red

Richi

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum