Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将HTML转换为字符串,然后查找并替换_Html_String_Vba_Replace_Notepad++ - Fatal编程技术网

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

将HTML转换为字符串,然后查找并替换,html,string,vba,replace,notepad++,Html,String,Vba,Replace,Notepad++,我已经在这个主题上进行了广泛的搜索,但没有找到我一直在寻找的东西。。。我来了 基本上,我有两个HTML文件。我每天从excel导出到HTML。第二个文件具有用于表格标题和格式的附加代码/CSS以及滚动条和搜索功能。我要做的是,将导出文件中的必要位复制到第二个文件中,以便它使用最新数据进行更新。然后,第二个文件链接到公司内部网上一个较大的网页,供员工查看结果 我有一个完整的自动化系统,目前我是如何做到这一点的,就是使用VBA打开Notepad++(用作我的HTML编辑器),然后手动进行这些更改。我

我已经在这个主题上进行了广泛的搜索,但没有找到我一直在寻找的东西。。。我来了

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

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

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

PS:下面的代码是这样写的,因为我删除了通配符,所以它只是在一行中找到几个单词,并用源文件中的整个代码替换它。我需要能够用源文件中的指定节替换整个节

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
Sub Find_Replace2()
Dim sTempSource作为字符串,sTempDest作为字符串
"暗弦",
作为字符串的Dim sBuf
作为整数的Dim iFileNum
将sFileName设置为字符串
'html文件的位置,sourcefile进入destfile
Dim htmlSourcefile作为字符串:htmlSourcefile=“I:\The Hub\Pages\Statistics\gentive\STB Incentive\STB League2.html”
Dim htmlDestfile As String:htmlDestfile=“I:\The Hub\Pages\Statistics\gentive\STB League-Copy.html”
sFileName=htmlDestfile
'打开上述文件,并将其转换为长字符串
iFileNum=FreeFile
打开HtmlTestFile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempDest=sTempDest&sBuf&vbCrLf
环
近缘
iFileNum=FreeFile
打开htmlSourcefile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempSource=sTempSource&sBuf&vbCrLf
环
近缘
'在字符串上查找并替换
sTempDest=Replace(sTempDest、“”、&sTempSource&“”)
'将字符串保存回原始文件
iFileNum=FreeFile
打开sFileName以作为iFileNum输出
打印#iFileNum,最干
近缘
端接头

替换功能无法正常工作。下面的代码在一个文本字符串中获取“!--插入vba…”之前的dest file code,然后在另一个文本字符串中获取“!--结束插入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
Sub Find_Replace2()
Dim sTempSource作为字符串、sTempDest作为字符串、sTempDest1作为字符串、sTempDest2作为字符串
将sSource1变长,sSource2变长,将sTempSource2变为字符串
变暗点1为长,点2为长,点3为长,点4为长
作为字符串的Dim sBuf
作为整数的Dim iFileNum
'html文件的位置,sourcefile进入destfile
Dim htmlSourcefile作为字符串:htmlSourcefile=“I:\The Hub\Pages\Statistics\gentive\STB Incentive\STB League2.html”
Dim htmlDestfile As String:htmlDestfile=“I:\The Hub\Pages\Statistics\gentive\STB League-Copy.html”
'打开上述文件,并将其转换为长字符串
iFileNum=FreeFile
打开HtmlTestFile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempDest=sTempDest&sBuf&vbCrLf
环
近缘
iFileNum=FreeFile
打开htmlSourcefile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempSource=sTempSource&sBuf&vbCrLf
环
近缘
点3=仪表(1,sTempSource,“”)
sTempDest1=Mid(sTempDest,1,point1)
sTempDest1=sTempDest1和sTempSource2
sTempDest2=sTempDest1和Mid(sTempDest,点2,Len(sTempDest)-点2)
'将字符串保存回测试器文件
iFileNum=FreeFile
打开“I:\thehub\Pages\Statistics\gentive\tester.html”作为iFileNum输出
打印#iFileNum,sTempDest2
近缘
端接头
子查找_Replace2()
Dim sTempSource作为字符串、sTempDest作为字符串、sTempDest1作为字符串、sTempDest2作为字符串、sTempDest3作为字符串
Dim sTempSource2作为字符串,sTempSource3作为字符串
调暗点1为长,点2为长,点3为长,点4为长,点5为长,点6为长,点7为长,点8为长
作为字符串的Dim sBuf
作为整数的Dim iFileNum
'html文件的位置,sourcefile进入destfile
Dim htmlSourcefile作为字符串:htmlSourcefile=“I:\The Hub\Pages\Statistics\gentive\STB Incentive\STB League2.html”
Dim htmlDestfile As String:htmlDestfile=“I:\The Hub\Pages\Statistics\gentive\STB League-Copy.html”
'打开上述文件,并将其转换为长字符串
iFileNum=FreeFile
打开HtmlTestFile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempDest=sTempDest&sBuf&vbCrLf
环
近缘
iFileNum=FreeFile
打开htmlSourcefile作为iFileNum输入
直到EOF为止(iFileNum)
行输入#iFileNum,sBuf
sTempSource=sTempSource&sBuf&vbCrLf
环
近缘
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