假设我有一个具有该结构的HTML文件
<html>
<head>
</head>
<body>
<div class="nav mainnavs"></div>
<div class="nav askquestion"></div>
</body>
</html>
我想插入此文
<ul>
<li><a id="nav-questions" href="/questions">Questions</a></li>
<li><a id="nav-tags" href="/tags">Tags</a></li>
<li><a id="nav-users" href="/users">Users</a></li>
<li><a id="nav-badges" href="/badges">Badges</a></li>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
</ul>
在该文件中的两个图层使用vb6 。
结果应该是这样的:
<html>
<head>
</head>
<body>
<div class="nav mainnavs"></div>
<ul>
<li><a id="nav-questions" href="/questions">Questions</a></li>
<li><a id="nav-tags" href="/tags">Tags</a></li>
<li><a id="nav-users" href="/users">Users</a></li>
<li><a id="nav-badges" href="/badges">Badges</a></li>
<li><a id="nav-unanswered" href="/unanswered">Unanswered</a></li>
</ul>
<div class="nav askquestion"></div>
</body>
</html>
答案 0 :(得分:3)
快速而肮脏的方法是进行一些字符串操作
Dim myHtmlFile As String
Dim topPart As String
Dim topSearch As String
Dim bottomPart As String
Dim bottomSearch As String
Dim newPart As String
'Load contents of file into string
'You would do some I/O stuff here to get the file in a variable
'Examples are pretty easy to find. Google "vba read file into variable"
myHtmlFile = "<html> " & vbCrLf & _
"<head>" & vbCrLf & _
"</head>" & vbCrLf & _
"<body>" & vbCrLf & _
"<div class=""nav mainnavs""></div>" & vbCrLf & _
"<div class=""nav askquestion""></div>" & vbCrLf & _
"</body>" & vbCrLf & _
"</html>"
topSearch = "<div class=""nav mainnavs""></div>" & vbCrLf
bottomSearch = "<div class=""nav askquestion""></div>" & vbCrLf
topPart = Left$(myHtmlFile, InStr(1, myHtmlFile, topSearch) + Len(topSearch) - 1)
bottomPart = Mid$(myHtmlFile, InStrRev(myHtmlFile, bottomSearch))
newPart = "<ul> " & vbCrLf & _
"<li><a id=""nav-questions"" href=""/questions"">Questions</a></li> " & vbCrLf & _
"<li><a id=""nav-tags"" href=""/tags"">Tags</a></li> " & vbCrLf & _
"<li><a id=""nav-users"" href=""/users"">Users</a></li> " & vbCrLf & _
"<li><a id=""nav-badges"" href=""/badges"">Badges</a></li> " & vbCrLf & _
"<li><a id=""nav-unanswered"" href=""/unanswered"">Unanswered</a></li> " & vbCrLf & _
"</ul> "
myHtmlFile = topPart & newPart & bottomPart
'Now write the file back out
答案 1 :(得分:0)
我会尝试使用正则表达式。
一个起点: How To Use Regular Expressions in Microsoft Visual Basic 6.0
我不知道你对正则表达式有多熟悉。但是我想到了如何做你想做的事情:你必须创建一个与行<div class="nav mainnavs"></div>
匹配的表达式,然后你调用方法匹配,得到匹配的字符串索引并插入你在那里创建了<ul>...</ul>
字符串。
答案 2 :(得分:0)
Dim sFileText As String
Dim iFileNo As Integer
iFileNo = FreeFile
'open the file for writing
Open App.Path & "\" & "test.html" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!
'write some example html to the file
Print #iFileNo, "<html>"
Print #iFileNo, "<head>"
Print #iFileNo, "</head>"
Print #iFileNo, "<body>"
Print #iFileNo, " "
Print #iFileNo, "<div class="&"nav mainnavs"&"></div>"
Print #iFileNo, " <ul>"
Print #iFileNo, " <li><a id="&"nav-questions" href="&"/questions"&">Questions</a></li>"
Print #iFileNo, " <li><a id="&"nav-tags"&" href="&"/tags"&">Tags</a></li>"
Print #iFileNo, " <li><a id="&"nav-users"&" href="&"/users"&">Users</a></li>"
Print #iFileNo, " "
Print #iFileNo, " <li><a id="&"nav-badges"&" href="&"/badges"&">Badges</a></li>"
Print #iFileNo, " <li><a id="&"nav-unanswered"&" href="&"/unanswered"&">Unanswered</a></li>"
Print #iFileNo, " </ul>"
Print #iFileNo, "<div class="&"nav askquestion"&"></div>"
Print #iFileNo, "</body>"
Print #iFileNo, "</html>"
MsgBox "Done, html file has been created."
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
End Sub