|
-
Apr 26th, 2010, 08:31 PM
#1
Thread Starter
Lively Member
[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>
-
Apr 27th, 2010, 12:16 AM
#2
Re: Inserting Text Contents Into An HTML Document
vb Code:
Private Sub Form_Load()
Dim strData As String
Open "c:\index.html" For Binary As #1
strData = Space(LOF(1)) 'this determines how much data is read on the next line, we're making a buffer
Get 1, 1, strData
Close #1
'now you have the html file in strdata
End Sub
So now there's InStr, Mid$, Left$, and Right$.
So:
vb Code:
x = Instr(strData, "<head>")
y = instr(x+1, strdata, "<\head>"
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:
y = instr(strdata, "<\head>"
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.
-
Apr 27th, 2010, 09:58 AM
#3
Thread Starter
Lively Member
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)
-
Apr 27th, 2010, 06:24 PM
#4
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.
-
Apr 27th, 2010, 06:57 PM
#5
Thread Starter
Lively Member
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..
-
Apr 27th, 2010, 07:20 PM
#6
Re: Inserting Text Contents Into An HTML Document
Paste your code here, between code tags, or highlight="vb" tags.
-
Apr 27th, 2010, 07:50 PM
#7
Thread Starter
Lively Member
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.
-
Apr 27th, 2010, 07:52 PM
#8
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.
-
Apr 27th, 2010, 07:59 PM
#9
Thread Starter
Lively Member
Re: Inserting Text Contents Into An HTML Document
It still did not work, but no errors.
-
Apr 27th, 2010, 09:32 PM
#10
Re: Inserting Text Contents Into An HTML Document
Well, you have to actually write the data back to the file....
vb Code:
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 'read data
X = InStr(strData, "<head>")
Y = InStr(x+6, strData, "</head>") 'x+len("<head>") = x+6
If X > 0 And Y > 0 Then
strData = Left$(strData, X + Len("<head>")-1) & Text1.Text & Mid$(strData, Y) 'fixed: len -1
If LOF(1) <= Len(strData) Then 'new data length is equal or greater
Put 1, 1, strData 'write data
Else 'strData length is less than file length
Close #1 'should close it to kill it
Kill cd.FileName 'delete the file, we shrunk it
Open (cd.FileName) For Binary As #1 'open it again
Put 1, 1, strData 'write data
End If 'forgot this
Else
MsgBox "Failed to find head tags, document unchanged."
End If
Close #1
End If
End If
Last edited by FireXtol; Apr 28th, 2010 at 03:35 PM.
Reason: fixed the code
-
Apr 28th, 2010, 10:44 AM
#11
Thread Starter
Lively Member
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.
-
Apr 28th, 2010, 03:36 PM
#12
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).
-
Apr 28th, 2010, 10:21 PM
#13
Thread Starter
Lively Member
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?
-
Apr 28th, 2010, 11:26 PM
#14
Re: Inserting Text Contents Into An HTML Document
 Originally Posted by EndResult
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.
-
Apr 29th, 2010, 12:07 PM
#15
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|