PDA

View Full Version : Is it possible to export cells as seperate HTML files?



mikewg
08-19-2006, 08:02 AM
I'm working with VBA to create HTML code in cells M2:M1000. I'd like to export each cell as a seperate HTML file named (ColumnB).html, so that each will have a different name. Ideally I'd like to include some header information on each HTML file before I add the data from Cell M.

Is there a guide or something that could give me some tips on how to do something like this?

mikewg
08-19-2006, 11:39 AM
Found it:


Public Sub CreateFiles()
Let MyPath$ = "C:\Temp\" 'This is the path where the files will be written to
[B2].Select 'This is the starting cell that contains the first filename
While ActiveCell.Value <> ""
Open MyPath$ & ActiveCell.Value & ".html" For Output As #1
ActiveCell.Offset(0, 1).Select
Print #1, ActiveCell.Value & " ";
Close #1
ActiveCell.Offset(1, -1).Select
Wend
End Sub

mdmackillop
08-19-2006, 12:16 PM
Hi Mike
Welcome to VBAX
I can't see that your code is going to work unless you have created all your files first. Try this variation. It will create the files using the information listed in column B as the file name, and also write this as the file content (as per your code). I've added a commented-out line. Using this will write the contents of the corresponding cell in C to the file created using the column B data.
Regards
MD

Option Explicit
Sub CreateAfile()
Dim MyPath As String, fil As String
Dim fs, a, cel As Range

Let MyPath = "C:\Temp\" 'This is the path where the files will be written to
Set fs = CreateObject("Scripting.FileSystemObject")
For Each cel In Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp))
fil = MyPath & cel & ".html"
Set a = fs.CreateTextFile(fil, True)
a.Close
Open fil For Output As #1
Print #1, cel.Value & " ";
'Print #1, cel.Offset(, 1).Value & " ";
Close #1
Next
End Sub