0

Let's Say i have a HTML file with that structure

<html>
<head>
</head>
<body>
<div class="nav mainnavs"></div>
<div class="nav askquestion"></div>
</body>
</html>

And i want to insert this text

 <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>

between two Layers in that file using vb6 .

The result should be like this:

  <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>
Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51
David Peterson
  • 552
  • 14
  • 31
  • @ramhound In such occasions i will open the file and use something like that: Print #1, " " but my problem is when i want to add multiline i can not print every line before the string i want using print... . – David Peterson Aug 27 '12 at 16:17

3 Answers3

3

The quick and dirty way is to do some string manipulation

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 
ray
  • 8,521
  • 7
  • 44
  • 58
  • one of my intentions of asking such question is to find out how to insert multiline without the vbCrLf & _. Because it may be so many lines of code before and after the UL tag. do you know hoy i can handle it? – David Peterson Aug 27 '12 at 16:13
  • @DavidPeterson I put the vbCrLf _ there only for readability. I may not understand the question. Are you saying your HTML file is large, or the list item html (e.g. li) is large? or something else? – ray Aug 27 '12 at 16:19
  • Unfortunately in that case both of them, my HtML file is large and its lines are large too. – David Peterson Aug 27 '12 at 16:29
  • 1
    A VBA string can hold [2 billion characters](http://msdn.microsoft.com/en-us/library/office/gg251467.aspx). Is your html bigger than 2 billion? If not, you should be ok. – ray Aug 27 '12 at 16:35
  • you are right but suppose if i wanted to copy lines in my code browser screen i should place for example 200 vbCrlf& _ in my code... . – David Peterson Aug 28 '12 at 06:54
  • 2
    Do you mean 200 consecutive vbCrLf strings? Can't see any problem with that, apart from why you would want to. – Mark Bertenshaw Aug 28 '12 at 09:26
  • 2
    @DavidPeterson My example does exactly what you asked in your original question. Rather than belaboring the issue with _what if_ questions, you might be best served by taking my example and working what you're trying to do. If you have a new issue, then ask a new question and include an updated code sample. – ray Aug 28 '12 at 13:13
  • @ray023 I know that man i waited for better solution for you and othe experts like u. Anyway thank you ;) – David Peterson Aug 28 '12 at 16:36
0

I'd give regular expressions a try.

A starting point: How To Use Regular Expressions in Microsoft Visual Basic 6.0

I don't know how familiar you are with regular expressions. But here is my thought about how to do what you want to: You'd have to create an expression that matches the line <div class="nav mainnavs"></div> and then you'd call the method match, get the string index of the match and insert your created <ul>...</ul> string there.

mortb
  • 9,361
  • 3
  • 26
  • 44
  • Sorry to repeat the meme, but it is (mostly) correct: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Mark Bertenshaw Aug 28 '12 at 09:32
  • @MarkBertenshaw Yeah, I know parsing html is "risky business". I made the assumption that the place where the
      is to be inserted is fairly static, just as the accepted answer does. I had no intention of making the program "understand" the html. My intention was just to make a suggestion about how a to find string after which the
        may be inserted may be done and point out that regex are availible to vb6 developers. A fairly clever Regex pattern is at least one bit more flexible than the InStr function
    – mortb Aug 30 '12 at 10:25
  • 1
    Can't disagree with you when you qualify it like that (thus the "mostly" :-) ). – Mark Bertenshaw Aug 30 '12 at 13:26
  • 1
    Cheers :) In my experience, when you maintain a vb6 program nowadays you go for quick fixes. Sure you can do clever and quite modern development in vb6, but most of the time people don't. A vb6 program is just something you "try to keep alive". – mortb Aug 30 '12 at 14:36
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
  • You can also do this same thing. But put your 3 parts of html in 3 text boxes. Then write them. EXAMPLE : Create 3 multiline text boxes and paste your html code in them the first part in the text1.text the second part in the second text2.text box and the third part in the third text box. Then print them to the file by using the following command. Print #iFileNo, text1.text & text2.text & text3.text – Matthew Lenfest Jan 13 '13 at 18:14