Click to See Complete Forum and Search --> : How to generate a HTML document
Derek
February 22nd, 2000, 06:53 AM
Hi,
Suppose i have a form in Visual Basic wherein i ask the user to enter values in the text boxes and with the help of those values in the text boxes i should be able to generate a html page.
For Eg: I have 2 textboxes wherein the user enters a heading and the name of an image that should appear on the html page, how would i generate that html page that should look somewhat like this....
<html>
<h1>heading entered by the user</h1>
<img src="test.jpg">image that should appear</img>
</html>
So i would like to see the output in any browser.
I would appreciate any help,
Thanks in advance,
Derek
Lothar Haensler
February 22nd, 2000, 07:21 AM
an HTML file is a simple ASCII text file.
you can create one using regular VB statements:
open yourfile four output as #1
print #1, "<html>"
print #1, "<h1>" & Text1.Text & "</h1>
...
close #1
Better yet, use the DHTML Editing component for IE5.
It's an ActiveX control that you can use in VB.
Kyle Burns
February 22nd, 2000, 07:37 AM
If you're using the Scripting library for your file I/O, you could do something like this:
'GENERAL HTML GEN FUNCTIONS
public Function MakeHeader(Expression as string, optional HeaderLevel as Integer = 1) as string
'ADD ADDITIONAL optional ARGUMENTS to MAKE
'THIS FUNCTION MORE REUSABLE (e.g. CLASS=,...)
MakeHeader = "<H" & HeaderLevel & ">" & Expression & "</H" & HeaderLevel & ">"
End Function
public Function MakeImage(SRC as string) as string
'USE ADDITIONAL optional ARGUMENTS to SPECIFY
'THINGS LIKE SIZE AND BORDER
MakeImage = "<IMG SRC='" & SRC & "'>"
'YOU DON'T WANT A </IMG> TAG. IT'S FORBIDDEN
'BY THE HTML STANDARD
End Function
Sub WritePage(FilePath as string)
Dim oFS as Scripting.FileSystemObject
Dim Response as Scripting.TextStream
'The variable name "Response" is used to
'Make your code somewhat "cut-and-paste" ready
'for Active Server Pages
set oFS = new Scripting.FileSystemObject
set Response = oFS.CreateTextFile(FilePath)
With Response
.Write "<HTML>"
.Write MakeHeader( txtHeader.Text )
.Write MakeImage( txtImage.Text )
.Write "</HTML"
.Close
End With
set Response = nothing
set oFS = nothing
End Sub
Derek
February 22nd, 2000, 08:03 AM
Hi Kyle,
This is the first time i am doing something like this so what is "Scripting library for your file I/O" (I am new to Visual Basic) and after i write this code how do i view it in a browser?I would appreciate it if you could guide me in getting this right justas you did with the Treeview. Thanks for the start :)
Derek
Derek
February 22nd, 2000, 08:06 AM
Hi Lothar,
I am brand new to Visual Basic and haven't really understood the code that you sent me in your reply. What is the "#" for and do i use this code with the DHTML Editing component for IE5 or in a normal form with the usual componants.
Please help with this,
thanks alot,
Derek
Chris Eastwood
February 22nd, 2000, 08:24 AM
This should help you get started - I've included one WinAPI call that opens the 'html' file in the default browser, the rest of the code will need slight modifications for your text box names.
Paste the following into your form (starting at the declarations section) :
'
private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (byval hwnd as Long, _
byval lpOperation as string, byval lpFile as string, _
byval lpParameters as string, byval lpDirectory as string, _
byval nShowCmd as Long) as Long
'
private Sub WriteHTMLFile()
'
Dim iFileNumber as Integer
'
' get the next available FileNumber from windows
'
iFileNumber = FreeFile
'
' Open our html file for writing
'
Open "c:\testout.htm" for Output as iFileNumber
'
' Write out the HTML
'
print #iFileNumber, "<HTML>"
print #iFileNumber, " <HEAD>"
print #iFileNumber, " <TITLE>" & txtTitle.Text & "</TITLE>"
print #iFileNumber, " </HEAD>"
print #iFileNumber, " <BODY>"
print #iFileNumber, " <H1>" & txtHeading.Text & "</H1>"
print #iFileNumber, " <IMG src = " & Chr(34) & txtImage.Text & _
Chr(34) & ">"
print #iFileNumber, " </BODY>"
print #iFileNumber, "</HTML>"
'
' Close the HTML file
'
Close #iFileNumber
'
' Now open it in the browser for viewing
'
ShellExecute me.hwnd, "open", "c:\testout.htm", "", vbNullString, 1
End Sub
I've presumed that your text boxes are called 'txtImage' and 'txtHeading' - I've also included another called 'txtTitle' for the title of the HTML page. Notice how the line that writes out the '<IMG>' tag is placing the txtImage.Text value inside quotes (chr(34)).
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
Derek
February 22nd, 2000, 08:33 AM
Hi Chris,
Thanks alot for your help. I will give it a try and let you know how i did. :)
Derek
Kyle Burns
February 22nd, 2000, 09:17 AM
The Scripting Library is a library that gives you somewhat object oriented access to the File System and I/O. One method of reading/writing to files is to use the open statement (Open "myfile.ext" For Input As #<<filenumber>>)
and then writing to the file with the Print or Write statements and reading from the file with the Input statement.
I prefer to use the Scripting Runtime for this because it makes many methods (FileExists(), etc.) available and gives you the added benefit of Intellisense. To use the Scripting library, you need to set a reference to it in Project...References.
You can find a reference for this library at http://msdn.microsoft.com/scripting. Click on the VBScript menu item and from there click on the Scripting Runtime item. Hope this helps
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.