【问题标题】:HTML Text with tags to formatted text in an Excel cell with buttons for multiple rows带有标签的 HTML 文本,用于 Excel 单元格中的格式化文本,带有多行按钮
【发布时间】:2021-09-15 13:36:25
【问题描述】:

我正在努力在 excel 中预览 html 代码,这个参考很好

回复:HTML Text with tags to formatted text in an Excel cell

但是,我现在正在尝试为每一行构建一个按钮(有点像邮件合并),所以目的是当我根据哪一行单击按钮时,预览会显示该行的 html 代码.

例如,如果我点击第 5 行 (A5),我将看到第 5 行 (A5) 的 html 代码。

我使用的 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

有人可以帮忙吗?

截图供参考。

enter image description here

提前谢谢大家

【问题讨论】:

  • 我不明白输入/输出是什么。单元格中的输入 html 和 html 显示为格式化文本的输出?例如。 text 会在按下该行上的按钮后给出 text,然后保持那样?
  • Correct.. A1:A22 包含所有 HTML 代码,当您按下 HTML 按钮时,A1 中的段落 HTML 代码显示在 IE(浏览器)中,就像阅读故事一样。所以计划是,每一行都有不同的故事,但是当我点击 HTML 按钮时,它会根据行显示故事。希望这是有道理的。

标签: excel vba html-parsing


【解决方案1】:

试试这个 - 它不会为每次按钮单击打开一个新窗口,而是尝试重新使用以前打开的 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

【讨论】:

  • 谢谢蒂姆..不断收到“运行时错误'13':类型不匹配”有什么解决办法吗?
  • 在哪一行?
  • (对不起,我对 VBA 很陌生,不确定我在做什么),但是当我按下绿色播放按钮时它会显示这个? ibb.co/D96c1Wg
  • 在 VB 编辑器中,转到工具 > 选项 > 常规选项卡 >> 错误捕获并将其设置为“中断类模块”。现在,当您遇到错误时,您应该会得到一个“调试”选项。不过,该代码不需要放入工作表代码模块中 - 您可以将其放入常规模块中。
  • 嗯,我不确定您所说的常规模块是什么意思。我所做的是,用您的代码替换我的 OP 代码。现在我收到此错误:ibb.co/D96c1Wg。你知道我做错了什么吗?感谢您的患者。
猜你喜欢
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 2012-12-17
相关资源
最近更新 更多