Hello itmanusa,
I made some changes to the macro. It now looks at columns "A:H" and finds the last entry. The file will now be created if it doesn't exist.
'Written: December 24, 2010
'Updated: December 24, 2010 - Extended the range and added auto file creation.
'Author: Leith Ross
'Summary: Save the cells in column "A" staring with "A1" as an HTML file. The file is
' saved in the currrent directory and named using the contents of cell "A1".
Sub SaveRangeAsHTML()
Dim FileSpec As String
Dim FSO As Object
Dim HTMLCode As String
Dim HTMLfile As Object
Dim Rng As Range
Dim RngEnd As Range
Dim Wks As Worksheet
Set Wks = ActiveSheet
Set Rng = Wks.Range("A1:H" & Rows.Count)
Set RngEnd = Rng.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
Set Rng = Wks.Range(Rng.Cells(1, 1), Wks.Cells(RngEnd.Row, "H"))
If Rng Is Nothing Then Exit Sub
If Rng.Cells(1, 1) = "" Then Exit Sub
FileSpec = CurDir & "\" & Rng.Cells(1, 1).Text & ".htm"
'Convert the Range into HTML
With ActiveWorkbook.PublishObjects
.Add( _
SourceType:=xlSourceRange, _
FileName:=FileSpec, _
Sheet:=Wks.Name, _
Source:=Rng.Address, _
HtmlType:=xlHtmlStatic).Publish True
End With
'Read the HTML file back as a string. Create the file if needed.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set HTMLfile = FSO.OpenTextFile(FileSpec, 1, True)
HTMLCode = HTMLfile.ReadAll
'Left align HTML code
HTMLCode = Replace(HTMLCode, "align=center x:publishsource=", _
"align=left x:publishsource=")
HTMLfile.Close
End Sub
Bookmarks