【问题标题】:Convert HTML to string, then Find and Replace将 HTML 转换为字符串,然后查找和替换
【发布时间】:2017-06-15 17:41:54
【问题描述】:

我在这个主题上进行了广泛的搜索,但无法找到我一直在寻找的东西......所以我开始了!

基本上,我有 2 个 HTML 文件。 1、我每天都从excel导出到HTML。第二个文件,添加了表格标题和格式的代码/CSS,以及滚动条和搜索功能。我所做的是将导出文件中的必要位复制到第二个文件中,以便使用最新数据进行更新。然后,第二个文件链接到公司内部网上的一个更大的网页,供员工查看他们的结果。

我有一个完整的自动化系统,我目前的实现方式是使用 VBA 打开 Notepad++(用作我的 HTML 编辑器),然后手动进行这些更改。我在 Notepad++ 中记录了一个宏,以使用“CTRL F1”作为命令自动进行更改,但是当我使用 shell 命令打开 Notepad++ 时,VBA 不能很好地与 Sendkeys 功能配合使用,所以它不是一个可行的解决方案用于自动化。

然后我进行了更多研究,发现了以下代码,我已经修改了这些代码以满足我的需要,完全绕过 Notepad++ 并将 HTML 转换为字符串。问题是,我需要查找和替换的不仅仅是 1 个单词,它是 2 个完整且独立的代码部分。我以为我可以使用通配符,但它似乎不想工作。任何可以让我用另一个 HTML 代码替换整个 HTML 代码块的帮助,使用 excel VBA 将是绝对的救星。提前致谢!

PS:下面的代码可以正常工作,因为我删除了通配符,所以它只是在 1 行上找到几个单词并将其替换为源文件中的整个代码。我需要能够用源文件中的指定部分替换整个部分

Sub Find_Replace2()
Dim sTempSource As String, sTempDest As String
'Dim sTemp As String
Dim sBuf As String
Dim iFileNum As Integer
Dim sFileName As String

'locations of html files, sourcefile goes into destfile
Dim htmlSourcefile As String: htmlSourcefile = "I:\The Hub\Pages\Statistics\Incentive\STB Incentive\STB League2.html"
Dim htmlDestfile As String: htmlDestfile = "I:\The Hub\Pages\Statistics\Incentive\STB League - Copy.html"

sFileName = htmlDestfile

'Opens the above files, and converts them to big long strings
iFileNum = FreeFile
Open htmlDestfile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempDest = sTempDest & sBuf & vbCrLf
Loop
Close iFileNum

iFileNum = FreeFile
Open htmlSourcefile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempSource = sTempSource & sBuf & vbCrLf
Loop
Close iFileNum

'find and replace on string
sTempDest = Replace(sTempDest, "<!--Start of VBA insert -->", "<!--Start of VBA insert -->" & sTempSource & "<!--End of VBA insert -->")

'saves string back off as original file
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTempDest
Close iFileNum

End Sub

【问题讨论】:

  • excel 输出的结构如何,如表?
  • 是的。这就像一个各种统计数据的排行榜,其中每个员工及其各种统计数据都在一行上。我需要将大约 100 行 HTML 格式化代码复制并替换到目标文件中(其中 CSS 代码设置为滚动等),然后为所有个人复制另外 10000 行实际的新数据和更新数据跨度>
  • 也许你的方法不对,最好不要调整 CSS 文件来从工作簿本身获取数据?我建议查看document.getlementsbytagname("TABLE"),然后插入类似document.getlementsbytagname("TABLE")(0).innerHtml
  • 你好。我想你不能详细说明一下吗?在 HTML 编码方面,我非常初级。这是我长期以来一直在做的事情,终于在这里寻求帮助。这是让我的每日更新完全定时和自动化的最后一块拼图

标签: html string vba replace notepad++


【解决方案1】:

替换功能将无法正常工作。以下代码在一个文本字符串中获取 > "!--insert vba....>" 之前的 dest 文件代码,然后在另一个文本字符串中获取 "!--end insert vba..." 之后的所有内容

我们只从源文件中获取表格。 (假设表格结束标记是

</table>.

我将表格保存为 html,这就是我的 Excel 结束表格的方式。)

所以我们将 Dest1 + 源表 + dest2 加在一起作为最后一页。

我让它将文件保存到 tester.html 文件中,这样它就不会破坏您的原始文件,直到您有机会对其进行测试。

Sub Find_Replace2()
Dim sTempSource As String, sTempDest As String, sTempDest1 As String, sTempDest2 As String
Dim sSource1 As Long, sSource2 As Long, sTempSource2 As String
Dim point1 As Long, point2 As Long, point3 As Long, point4 As Long

Dim sBuf As String
Dim iFileNum As Integer

'locations of html files, sourcefile goes into destfile
Dim htmlSourcefile As String: htmlSourcefile = "I:\The Hub\Pages\Statistics\Incentive\STB Incentive\STB League2.html"
Dim htmlDestfile As String: htmlDestfile = "I:\The Hub\Pages\Statistics\Incentive\STB League - Copy.html"

'Opens the above files, and converts them to big long strings
iFileNum = FreeFile
Open htmlDestfile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempDest = sTempDest & sBuf & vbCrLf
Loop
Close iFileNum

iFileNum = FreeFile
Open htmlSourcefile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempSource = sTempSource & sBuf & vbCrLf
Loop
Close iFileNum

point3 = InStr(1, sTempSource, "<table") - 1
point4 = InStr(point3, sTempSource, "</table>") + 8
sTempSource2 = Mid(sTempSource, point3, point4 - point3)


point1 = InStr(1, sTempDest, "<!--Start of VBA insert -->") + 27
point2 = InStr(point1, sTempDest, "<!--End of VBA insert -->")
sTempDest1 = Mid(sTempDest, 1, point1)
sTempDest1 = sTempDest1 & sTempSource2
sTempDest2 = sTempDest1 & Mid(sTempDest, point2, Len(sTempDest) - point2)


'saves string back to a tester file
iFileNum = FreeFile
Open "I:\The Hub\Pages\Statistics\Incentive\tester.html" For Output As iFileNum
Print #iFileNum, sTempDest2
Close iFileNum

End Sub

【讨论】:

  • 嘿,约翰,非常感谢,你是个天才,这确实更有意义。我自己从来没有遇到过这个!但是,当我运行此代码时,最后一节的第二行...“sTempDest2 = sTempDest1 & Mid(sTempDest, point2, Len(sTempDest) - point2)”有错误“运行时间 2,无效的过程调用或参数”。我试过玩它,但老实说我不确定从哪里开始。我确信这可能非常简单,但我想在开始向此代码添加第二个替换选项之前确保。再次感谢,非常感谢
  • 我一直试图通过这个来找出问题所在。我还没有答案,但我有更多信息。例如,当我进入代码并将鼠标悬停在“point3”和“point4”上时,结果是 673 和 11592,这是有道理的,因为这是字符串中的字符数。当我对“point1”和“point2”做同样的事情时,结果分别是27(因为那行代码末尾的+27)和0。
  • 由于某种原因,它没有以与另一个相同的方式拾取这个字符串,我认为这是有连锁反应,因为当我将鼠标悬停在它上面时,“sTempDest”显示 =“”,但是其他的都显示 HTML 字符串。希望这会有所帮助!再次感谢,真的很感激!
  • 等一下,我正在做额外的测试,它似乎正在工作。不知道上述 cmets 和现在之间发生了什么,但我会接受的!很快就会回来报告!
  • 知道了!!再次感谢,非常感谢!
【解决方案2】:
Sub Find_Replace2()
Dim sTempSource As String, sTempDest As String, sTempDest1 As String, sTempDest2 As String, sTempDest3 As String

Dim sTempSource2 As String, sTempSource3 As String
Dim point1 As Long, point2 As Long, point3 As Long, point4 As Long, point5 As Long, point6 As Long, point7 As Long, point8 As Long

Dim sBuf As String
Dim iFileNum As Integer

'locations of html files, sourcefile goes into destfile
Dim htmlSourcefile As String: htmlSourcefile = "I:\The Hub\Pages\Statistics\Incentive\STB Incentive\STB League2.html"
Dim htmlDestfile As String: htmlDestfile = "I:\The Hub\Pages\Statistics\Incentive\STB League - Copy.html"

'Opens the above files, and converts them to big long strings
iFileNum = FreeFile
Open htmlDestfile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempDest = sTempDest & sBuf & vbCrLf
Loop
Close iFileNum

iFileNum = FreeFile
Open htmlSourcefile For Input As iFileNum
Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTempSource = sTempSource & sBuf & vbCrLf
Loop
Close iFileNum

point3 = InStr(1, sTempSource, "<!--table")
point4 = InStr(point3, sTempSource, "-->") + 3
    sTempSource2 = Mid(sTempSource, point3, point4 - point3)

point7 = InStr(1, sTempSource, "</tr>")
point8 = InStr(point7, sTempSource, "<!--END OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD-->") + 58
    sTempSource3 = Mid(sTempSource, point7, point8 - point7)

point1 = InStr(1, sTempDest, "<!--Start of VBA insert -->") + 27
point2 = InStr(point1, sTempDest, "<!--End of VBA insert -->") - 1
point5 = InStr(1, sTempDest, "<!--Start of VBA insert2 -->") + 28
point6 = InStr(point5, sTempDest, "<!--End of VBA insert2 -->") - 1
sTempDest1 = Mid(sTempDest, 1, point1)
sTempDest1 = sTempDest1 & sTempSource2
sTempDest2 = sTempDest1 & Mid(sTempDest, point2, point5 - point2)
sTempDest2 = sTempDest2 & sTempSource3
        sTempDest3 = sTempDest2 & Mid(sTempDest, point6, Len(sTempDest) - point6)

'saves string back to a tester file
iFileNum = FreeFile
Open "I:\The Hub\Pages\Statistics\Incentive\STB League - Copy.html" For Output As iFileNum
Print #iFileNum, sTempDest3
Close iFileNum

End Sub

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 2016-12-11
    • 2019-10-16
    • 2015-09-22
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    相关资源
    最近更新 更多