Results 1 to 15 of 15

Thread: [RESOLVED] Inserting Text Contents Into An HTML Document

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Resolved [RESOLVED] Inserting Text Contents Into An HTML Document

    How would I insert the contents of a textbox into any html file I open up without deleting all the html contents? I am also trying to figure out how to get the textbox contents at the beginning of the html document.

    Here is my code for it

    cd.DialogTitle = "Save HTML file"
    cd.Filter = "HTML Files|*.htm;*.html;*.shtml;*.shtm;*.stm;*.asp;*.htt;*.css;*.tem|All Files (*.*)|*.*"
    cd.FLAGS = 4
    cd.ShowSave

    If Len(cd.FileName) > 0 Then

    strFileName = cd.FileName

    On Error Resume Next
    If Dir$(cd.FileName) <> "" Then Open (cd.FileName) For Output As #1
    Write #1, cd.FileName & Text1.Text
    Close #1

    End If

    ..But I keep deleting the HTML files contents and replacing it.

    I am trying to get the end result to look like this:

    <head>

    TEXTBOX CONTENTS RIGHT HERE

    </head>

    <title></title>

  2. #2
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    vb Code:
    1. Private Sub Form_Load()
    2. Dim strData As String
    3. Open "c:\index.html" For Binary As #1
    4.   strData = Space(LOF(1)) 'this determines how much data is read on the next line, we're making a buffer
    5.   Get 1, 1, strData
    6. Close #1
    7. 'now you have the html file in strdata
    8. End Sub

    So now there's InStr, Mid$, Left$, and Right$.

    So:
    vb Code:
    1. x = Instr(strData, "<head>")
    2. y = instr(x+1, strdata, "<\head>"
    3. strData = left$(strdata, x+len("<head>")) & Text1.Text & mid$(strdata, y)

    Now that'll insert the text between the <head> and </head>, but it'll remove anything that was previously there. To preserve what's already in the head tags, you'd want to either add to the end or the start, for instance:

    vb Code:
    1. y = instr(strdata, "<\head>"
    2. strData = left$(strdata, y-1) & vbcrlf & Text1.Text & vbcrlf & mid$(strdata, y) 'add it on it's own line, after other stuff in the head tags

    Cheers.

    You're likely increasing the file size, so you can just write it back to the file, however, if you reduce the size, you'll want to close the file, kill it(deletes it), and then write the smaller file. That's the only way to reduce the file size. If you don't, the file will not become any smaller, and the end of the file will contain the original HTML.
    Last edited by FireXtol; Apr 27th, 2010 at 06:29 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Thanks for your response. This makes better sense than what I was trying to do, however I am getting a runtime error 5 "invalid call procedure or argument" on this line of code: strData = Left$(strData, x + Len("<head>")) & Text1.Text & Mid$(strData, y)

  4. #4
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    Technically, you should check that both x and y are greater than zero before using them in a Mid$() call.

    If they are zero, that's mean not found, and that'll surely cause errors passing zeros to Mid$().

    With Mid$ the first character is 1, and the length must be at least 1, but if left out(it's optional) it performs similar to Right$, only it uses the start and just goes to the end of the string, whereas Right$ would require a little math to do the same(len(string)-start).

    So put that in an if:

    Code:
    If x > 0 and y > 0 then
      'the code
    End If
    Last edited by FireXtol; Apr 27th, 2010 at 06:28 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Alright, I changed that part of the code and while it stopped it from giving me a runtime error now its not doing anything..

  6. #6
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    Paste your code here, between code tags, or highlight="vb" tags.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Alright here it is, it's the same code but I have worked it into how you showed me...

    I could not get the vbcode tags for this forum to work (its my browser)..

    cd.DialogTitle = "HTML file..."
    cd.Filter = "HTML Files|*.htm;*.html;*.shtml;*.shtm;*.stm;*.asp;*.htt;*.css;*.tem|All Files (*.*)|*.*"
    cd.FLAGS = 4
    cd.ShowSave

    If Len(cd.FileName) > 0 Then
    strFileName = cd.FileName

    If Dir$(cd.FileName) <> "" Then

    Dim strData As String
    Open (cd.FileName) For Binary As #1
    strData = Space(LOF(1))
    Get 1, 1, strData
    Close #1

    If x > 0 And y > 0 Then

    x = InStr(strData, "<head>")
    y = InStr(strData, "</head>")
    strData = Left$(strData, x + Len("<head>")) & Text1.Text & Mid$(strData, y)

    End If
    End If
    End If

    I tried to do it solo also without loading from the dialog, but still I could not get it to work.

  8. #8
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    Alright, you put the IF above the assignments for x and y.

    Code:
    If x > 0 And y > 0 Then
    x = InStr(strData, "<head>")
    y = InStr(strData, "</head>")
    strData = Left$(strData, x + Len("<head>")) & Text1.Text & Mid$(strData, y)
    End If
    End If
    End If
    Should be:

    Code:
        x = InStr(strData, "<head>")
        y = InStr(strData, "</head>")
    
        If x > 0 And y > 0 Then
          strData = Left$(strData, x + Len("<head>")) & Text1.Text & Mid$(strData, y)
        End If
    
      End If
    End If
    X and Y must be assigned(from the InStr function) before the check, otherwise they'll definitely be zero.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    It still did not work, but no errors.

  10. #10
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    Well, you have to actually write the data back to the file....

    vb Code:
    1. cd.DialogTitle = "HTML file..."
    2. cd.Filter = "HTML Files|*.htm;*.html;*.shtml;*.shtm;*.stm;*.asp;*.htt;*.css;*.tem|All Files (*.*)|*.*"
    3. cd.FLAGS = 4
    4. cd.ShowSave
    5.  
    6. If Len(cd.FileName) > 0 Then
    7.   strFileName = cd.FileName
    8.  
    9.   If Dir$(cd.FileName) <> "" Then
    10.  
    11.     Dim strData As String
    12.    
    13.     Open (cd.FileName) For Binary As #1
    14.       strData = Space(LOF(1))
    15.       Get 1, 1, strData 'read data
    16.  
    17.       X = InStr(strData, "<head>")
    18.       Y = InStr(x+6, strData, "</head>") 'x+len("<head>") = x+6
    19.       If X > 0 And Y > 0 Then
    20.         strData = Left$(strData, X + Len("<head>")-1) & Text1.Text & Mid$(strData, Y) 'fixed: len -1
    21.         If LOF(1) <= Len(strData) Then 'new data length is equal or greater
    22.           Put 1, 1, strData 'write data
    23.         Else 'strData length is less than file length
    24.           Close #1 'should close it to kill it
    25.           Kill cd.FileName 'delete the file, we shrunk it
    26.           Open (cd.FileName) For Binary As #1 'open it again
    27.           Put 1, 1, strData 'write data
    28.         End If 'forgot this
    29.       Else
    30.         MsgBox "Failed to find head tags, document unchanged."
    31.       End If
    32.      
    33.     Close #1
    34.   End If
    35. End If
    Last edited by FireXtol; Apr 28th, 2010 at 03:35 PM. Reason: fixed the code

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Thank you for your help, I cant get it still, I don't know why this is so tough for me, I guess I have been away from coding for too long, I wish I could get this code to work.
    Last edited by EndResult; Apr 28th, 2010 at 11:02 AM.

  12. #12
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    I fixed a couple simple bugs in the code, and verified the code is working(at least for me).

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Alright that worked, thank you! and thanks for your patience, and if I may ask what were the bugs?

  14. #14
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Inserting Text Contents Into An HTML Document

    Quote Originally Posted by EndResult View Post
    Alright that worked, thank you! and thanks for your patience, and if I may ask what were the bugs?
    You're welcome.

    Commented lines: 'fixed this and 'forgot this.

    You can go to your original post in this thread, there's a 'Thread Tools' link, and that'll make a menu drop down, click 'Mark Thread Resolved'.

    Good luck.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Inserting Text Contents Into An HTML Document

    Alright, I see. Appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width