Attribute VB_Name = "HTML_Programmability"
Option Explicit
Type HTMLConvertReplace
oRangeOrChart As Object 'RangeAndChartToConvert
oUseExisting As Integer 'UseExistingFile
oFilePath As String 'HTMLFilePath
oHeader As String 'HeaderFullPage
oIsExistingFPFile As Integer 'UseFrontPageForExistingFule
oAddToFPWeb As Integer 'AddToFrontPageWeb
oTemplatePath As String 'ExistingFilePath
End Type
Public Const HTMLCONVERT_SUCCESS = 0
Public Const HTMLCONVERT_ENTIRECOLUMNSELECTED = 1
Public Const HTMLCONVERT_ENTIREROWSELECTED = 2
Public Const HTMLCONVERT_PATHEXISTINGFILEMISSING = 3
Public Const HTMLCONVERT_CANNOTSTARTFRONTPAGE = 4
Public Const HTMLCONVERT_FILENOTFOUND = 5
Public Const HTMLCONVERT_CANNOTUSEEXISTINGFILE = 6
Public Const HTMLCONVERT_XLHTMLNOTINSTALLED = 7
Public Const HTMLCONVERT_CODEPAGEDOESNOTEXIST = 8
Public Const HTMLCONVERT_CANTOPENFILE = 9
Public Const HTMLCONVERT_OBJECTNOTDEFINED = 10
Public Const HTMLCONVERT_WEBSERVERNOTOPEN = 11
Public Const HTMLCONVERT_CANNOTFINDXLHTML = 13
Function HTMLConvert(RangeAndChartToConvert As Variant, UseExistingFile As Boolean, UseFrontPageForExistingFile As Boolean, AddToFrontPageWeb As Boolean, CodePage As Long, HTMLFilePath As String, Optional ExistingFilePath As String, Optional TitleFullPage As String, Optional HeaderFullPage As String, Optional DescriptionFullPage As String, Optional LineBeforeTableFullPage As Boolean, Optional LineAfterTableFullPage As Boolean, Optional LastUpdate As Variant, Optional NameFullPage As String, Optional EmailFullPage As String) As Integer
Dim i As Integer
Dim szParentSheet As String
Dim szParentBook As String
Dim szDivID As String
Dim szFilePath As String
Dim PubObj As Object
Dim CopiedTemplate As Boolean
Dim p_PublishInfo() As HTMLConvertReplace
Dim FoundWorkbook As Workbook
Dim IsChartPly As Boolean
'Need to kill the function if UseFrontPageForExistingFile or AddToFrontPageWeb are used
If UseFrontPageForExistingFile Or AddToFrontPageWeb Then
HTMLConvert = HTMLCONVERT_CANNOTSTARTFRONTPAGE
GoTo EarlyExit
End If
'************* Verify RangeToConvert **************
On Error GoTo EntryNotRef
If IsObject(RangeAndChartToConvert) Then
ReDim p_PublishInfo(0 To 0)
Set p_PublishInfo(0).oRangeOrChart = RangeAndChartToConvert
Else
ReDim p_PublishInfo(LBound(RangeAndChartToConvert) To UBound(RangeAndChartToConvert))
For i = LBound(RangeAndChartToConvert) To UBound(RangeAndChartToConvert)
Set p_PublishInfo(i).oRangeOrChart = RangeAndChartToConvert(i)
Next i
End If
On Error Resume Next
Set FoundWorkbook = p_PublishInfo(LBound(p_PublishInfo)).oRangeOrChart.Parent.Parent
If Err > 0 Then 'This is (most likely) a chart ply (or invalid)
Err = 0
On Error GoTo EntryNotRef
Set FoundWorkbook = p_PublishInfo(LBound(p_PublishInfo)).oRangeOrChart.Parent
End If
szParentBook = CStr(FoundWorkbook.Name)
On Error GoTo 0
'******************** Insert the Data into vars ********************
For i = LBound(p_PublishInfo) To UBound(p_PublishInfo)
p_PublishInfo(i).oFilePath = HTMLFilePath
p_PublishInfo(i).oUseExisting = UseExistingFile
p_PublishInfo(i).oIsExistingFPFile = UseFrontPageForExistingFile
p_PublishInfo(i).oAddToFPWeb = AddToFrontPageWeb
If IsMissing(ExistingFilePath) Then
p_PublishInfo(i).oTemplatePath = ""
Else
p_PublishInfo(i).oTemplatePath = ExistingFilePath
End If
If IsMissing(HeaderFullPage) Or i <> LBound(p_PublishInfo) Then 'should only be used the first time through the loop
p_PublishInfo(i).oHeader = ""
Else
p_PublishInfo(i).oHeader = CStr(HeaderFullPage)
End If
szFilePath = p_PublishInfo(i).oFilePath
If szFilePath = "" Then
HTMLConvert = HTMLCONVERT_FILENOTFOUND
Exit Function
End If
Next i
If p_PublishInfo(LBound(p_PublishInfo)).oUseExisting Then
CopiedTemplate = CopyFileTemplate(p_PublishInfo(LBound(p_PublishInfo)))
If Not CopiedTemplate Then
HTMLConvert = HTMLCONVERT_PATHEXISTINGFILEMISSING
Exit Function
End If
End If
For i = LBound(p_PublishInfo) To UBound(p_PublishInfo)
szParentSheet = CStr(p_PublishInfo(i).oRangeOrChart.Parent.Name)
If szParentSheet = szParentBook Then
szParentSheet = CStr(p_PublishInfo(i).oRangeOrChart.Name)
IsChartPly = True
End If
szDivID = GetDivID(p_PublishInfo(i))
On Error GoTo CantCreatePublishObject
'Need to determine whether it's a chart or a range
If VarType(p_PublishInfo(i).oRangeOrChart) = vbObject Then
'Chart
If IsChartPly Then 'Need to publish differently if it's a chart ply instead of a chart object
Set PubObj = Workbooks(szParentBook).PublishObjects.Add(xlSourceChart, szFilePath, szParentSheet, "", xlHtmlStatic, szDivID, p_PublishInfo(i).oHeader)
Else
Set PubObj = Workbooks(szParentBook).PublishObjects.Add(xlSourceChart, szFilePath, szParentSheet, CStr(p_PublishInfo(i).oRangeOrChart.Name), xlHtmlStatic, szDivID, p_PublishInfo(i).oHeader)
End If
Else
'Sheet
Set PubObj = Workbooks(szParentBook).PublishObjects.Add(xlSourceRange, szFilePath, szParentSheet, CStr(p_PublishInfo(i).oRangeOrChart.Address), xlHtmlStatic, szDivID, p_PublishInfo(i).oHeader)
End If
'If the file is to be replaced, it should only replace the file the first time through
'the loop. HTMLConvert used to publish one solid chunk, so this wasn't an issue there.
On Error GoTo CantPublishObject
If i = i = LBound(p_PublishInfo) Then
PubObj.Publish (Not p_PublishInfo(i).oUseExisting)
Else
PubObj.Publish (False)
End If
On Error GoTo 0
Next
HTMLConvert = HTMLCONVERT_SUCCESS
EarlyExit:
Exit Function
EntryNotRef:
HTMLConvert = HTMLCONVERT_OBJECTNOTDEFINED
Exit Function
CantCreatePublishObject:
HTMLConvert = HTMLCONVERT_CANTOPENFILE
Exit Function
CantPublishObject:
HTMLConvert = HTMLCONVERT_CANNOTUSEEXISTINGFILE
Exit Function
End Function
Function GetDivID(UsePubInfo As HTMLConvertReplace) As String
Dim HeaderString As String
Dim ParentName As String
Dim SheetName As String
Dim RangeName As String
Dim CurPos As Integer
'Note: this was broken out so that it could be more easily maintained.
'Yes, it causes duplication of code, but not at expense of performance.
'Need to strip all colons (":") (for compatibility with Publish)
ParentName = UsePubInfo.oRangeOrChart.Parent.Parent.Name
While InStr(1, ParentName, ":") > 0
CurPos = InStr(1, ParentName, ":")
ParentName = Left$(ParentName, CurPos - 1) & "_" & Right$(ParentName, Len(ParentName) - CurPos)
Wend
SheetName = UsePubInfo.oRangeOrChart.Parent.Parent.Name
While InStr(1, SheetName, ":") > 0
CurPos = InStr(1, SheetName, ":")
SheetName = Left$(SheetName, CurPos - 1) & "_" & Right$(SheetName, Len(SheetName) - CurPos)
Wend
If VarType(UsePubInfo.oRangeOrChart) = vbObject Then
'Chart
RangeName = CStr(UsePubInfo.oRangeOrChart.Name)
Else
'Sheet
RangeName = CStr(UsePubInfo.oRangeOrChart.Address)
End If
While InStr(1, RangeName, ":") > 0
CurPos = InStr(1, RangeName, ":")
RangeName = Left$(RangeName, CurPos - 1) & "_" & Right$(RangeName, Len(RangeName) - CurPos)
Wend
GetDivID = "[" & ParentName & "]" & SheetName & "!" & RangeName
End Function
Function CopyFileTemplate(UsePubInfo As HTMLConvertReplace) As Boolean
If Not IsMissing(UsePubInfo.oTemplatePath) Then
If UsePubInfo.oTemplatePath = "" Then
CopyFileTemplate = False
Else
Err = 0
On Error Resume Next
FileCopy UsePubInfo.oTemplatePath, UsePubInfo.oFilePath
CopyFileTemplate = (Err = 0) 'pass a true if there were no errors
End If
Else 'No path specified
CopyFileTemplate = False
End If
End Function