我正在excel中预览html代码,这个参考文件在中运行良好
回复:HTML文本,带有Excel单元格中格式化文本的标签
然而,我现在正试图为每一行构建一个按钮(有点像邮件合并(,所以当我根据哪一行单击按钮时,预览会显示该行的html代码。
例如,如果我点击第5行(A5(,那么我将看到第5行的html代码(A5(。
我使用的VBA代码是:
Sub HTMLPreview()
Dim Ie As Object, RowNumber As Integer
Set Ie = CreateObject("InternetExplorer.Application")
With Ie
.Visible = True
.Navigate "about:blank"
.document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
'.document.body.createtextrange.execCommand "Copy"
'ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1")
'.Quit
End With
End Sub
有人能帮忙吗?
屏幕截图供参考。
在此处输入图像描述
提前感谢
试试这个-它不会为每次点击按钮打开一个新窗口,而是尝试重复使用以前打开的IE。
将所有按钮分配给ShowHTML
Sub ShowHTML()
Dim Ie As Object, html
Set Ie = GetIE("about:blank") 'try to get already-open window
If Ie Is Nothing Then
Set Ie = CreateObject("InternetExplorer.Application") 'open a new IE
Ie.Visible = True
Ie.Navigate "about:blank"
End If
'Application.Caller is the name os the clicked-on button
html = ActiveSheet.Shapes(Application.Caller).TopLeftCell.EntireRow.Cells(1).Value
Ie.document.body.InnerHTML = html
End Sub
'Find an open IE window which has a matching URL
Function GetIE(sLocation As String) As Object
Dim o As Object, sURL As String, retVal As Object
For Each o In CreateObject("Shell.Application").Windows
sURL = ""
sURL = o.LocationURL
If sURL Like sLocation & "*" Then
Set retVal = o
'Exit For
End If
Next o
Set GetIE = retVal
End Function