将HTML转换为字符串,然后查找和替换

时间:2017-01-30 14:15:26

标签: html string vba replace notepad++

我在这个问题上进行了广泛的搜索,但却未能找到我一直在寻找的东西...所以我走了!

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

我有一个完整的自动化系统,我目前如何实现这一点是使用VBA打开Notepad ++(用作我的HTML编辑器),然后手动进行这些更改。我在Notepad ++中录制了一个宏来使用“CTRL F1”作为命令自动进行更改,但是当我使用shell命令打开Notepad ++时,VBA与Sendkeys函数不兼容,因此它不是一个可行的解决方案用于自动化。

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

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

2 个答案:

答案 0 :(得分:0)

替换功能无法正常工作。以下代码在&gt;之前获取dest文件代码“! - 插入vba ....&gt;”在一个文本字符串中,然后它在“! - 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

答案 1 :(得分:0)

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