Connecting Tech Pros Worldwide Forums | Help | Site Map

Can I save an HTML file with VBA?

needin4mation@gmail.com
Guest
 
Posts: n/a
#1: Mar 21 '06
Hi, I have access 97. I can upgrade if I have to (but that will cost
more money :)). If I want to read a row, parse that row, and then save
the values to an HTML file, will VBA do that?

Say I want to write:

<html>

<body>

<table>
<td>
row data from table
</td>
</table>

</body>

</html>

save results as .htm for that row, go to the next, save another .htm
file....

I'm not asking anyone to write this for me. I am only asking to be
pointed in the right direction. Thank you.


Larry Linson
Guest
 
Posts: n/a
#2: Mar 21 '06

re: Can I save an HTML file with VBA?


Yes. HTML is simply particular characters saved in a text file, and there
are File I/O statements in VBA that read and write text files. Look in Help
for Open, Input#, Print#, and Close, as a start. These File I/O statements
have been around since DOS Basic.

Larry Linson
Microsoft Access MVP

<needin4mation@gmail.com> wrote in message
news:1142917024.767270.57120@i39g2000cwa.googlegro ups.com...[color=blue]
> Hi, I have access 97. I can upgrade if I have to (but that will cost
> more money :)). If I want to read a row, parse that row, and then save
> the values to an HTML file, will VBA do that?
>
> Say I want to write:
>
> <html>
>
> <body>
>
> <table>
> <td>
> row data from table
> </td>
> </table>
>
> </body>
>
> </html>
>
> save results as .htm for that row, go to the next, save another .htm
> file....
>
> I'm not asking anyone to write this for me. I am only asking to be
> pointed in the right direction. Thank you.
>[/color]


Anthony England
Guest
 
Posts: n/a
#3: Mar 21 '06

re: Can I save an HTML file with VBA?



<needin4mation@gmail.com> wrote in message
news:1142917024.767270.57120@i39g2000cwa.googlegro ups.com...[color=blue]
> Hi, I have access 97. I can upgrade if I have to (but that will cost
> more money :)). If I want to read a row, parse that row, and then save
> the values to an HTML file, will VBA do that?
>
> Say I want to write:
>
> <html>
>
> <body>
>
> <table>
> <td>
> row data from table
> </td>
> </table>
>
> </body>
>
> </html>
>
> save results as .htm for that row, go to the next, save another .htm
> file....
>
> I'm not asking anyone to write this for me. I am only asking to be
> pointed in the right direction. Thank you.[/color]



I see you're not asking anyone to write it for you, but if you need to ask
whether it can be done, I'm guessing you might need a little help in getting
it done. Here is an example function which generates an html file for a
table of products. If you cut and paste the code below into a new module,
you will be able to call it like this:

If CreateProductHtml("C:\Products.htm") Then
MsgBox "File Created",vbInformation
End If


' *** Code Starts ***
Option Compare Database
Option Explicit

Public Function CreateProductHtml(strPath As String) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim lngMaxField As Long
Dim lngCount As Long
Dim intFile As Integer
Dim strLine As String
Dim blnFileOpen As Boolean

intFile = FreeFile

If Len(Dir(strPath)) > 0 Then
If MsgBox("Overwrite existing file?" & vbCrLf & _
strPath, vbExclamation Or vbYesNoCancel) = vbYes Then
Kill strPath
Else
Exit Function
End If
End If

Open strPath For Output As #intFile

blnFileOpen = True

strSQL = "SELECT * FROM tblProduct ORDER BY ProductID"

Set dbs = CurrentDb()

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then

lngMaxField = rst.Fields.Count - 1

Print #intFile, "<html>"
Print #intFile, "<head>"
Print #intFile, "<title>Product List</title>"
Print #intFile, "</head>"
Print #intFile, "<body>"
Print #intFile, "<table border=2>"
While Not rst.EOF
Print #intFile, "<tr>"
For lngCount = 0 To lngMaxField
strLine = Trim(Nz(rst.Fields(lngCount).Value, ""))
If Len(strLine) = 0 Then
strLine = "&nbsp;"
End If
strLine = "<td>" & strLine & "</td>"
Print #intFile, strLine
Next lngCount
Print #intFile, "</tr>"
rst.MoveNext
Wend
Print #intFile, "</table>"
Print #intFile, "</body>"
Print #intFile, "</html>"

CreateProductHtml = True

End If

Exit_Handler:

If blnFileOpen Then
Close #intFile
End If

If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
' *** Code Ends ***


Newbie
 
Join Date: May 2006
Posts: 1
#4: May 4 '06

re: Can I save an HTML file with VBA?


thanks this is really useful

Quote:

Originally Posted by Anthony England

<needin4mation@gmail.com> wrote in message
news:1142917024.767270.57120@i39g2000cwa.googlegro ups.com...[color=blue]
> Hi, I have access 97. I can upgrade if I have to (but that will cost
> more money :)). If I want to read a row, parse that row, and then save
> the values to an HTML file, will VBA do that?
>
> Say I want to write:
>
> <html>
>
> <body>
>
> <table>
> <td>
> row data from table
> </td>
> </table>
>
> </body>
>
> </html>
>
> save results as .htm for that row, go to the next, save another .htm
> file....
>
> I'm not asking anyone to write this for me. I am only asking to be
> pointed in the right direction. Thank you.[/color]



I see you're not asking anyone to write it for you, but if you need to ask
whether it can be done, I'm guessing you might need a little help in getting
it done. Here is an example function which generates an html file for a
table of products. If you cut and paste the code below into a new module,
you will be able to call it like this:

If CreateProductHtml("C:\Products.htm") Then
MsgBox "File Created",vbInformation
End If


' *** Code Starts ***
Option Compare Database
Option Explicit

Public Function CreateProductHtml(strPath As String) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim lngMaxField As Long
Dim lngCount As Long
Dim intFile As Integer
Dim strLine As String
Dim blnFileOpen As Boolean

intFile = FreeFile

If Len(Dir(strPath)) > 0 Then
If MsgBox("Overwrite existing file?" & vbCrLf & _
strPath, vbExclamation Or vbYesNoCancel) = vbYes Then
Kill strPath
Else
Exit Function
End If
End If

Open strPath For Output As #intFile

blnFileOpen = True

strSQL = "SELECT * FROM tblProduct ORDER BY ProductID"

Set dbs = CurrentDb()

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then

lngMaxField = rst.Fields.Count - 1

Print #intFile, "<html>"
Print #intFile, "<head>"
Print #intFile, "<title>Product List</title>"
Print #intFile, "</head>"
Print #intFile, "<body>"
Print #intFile, "<table border=2>"
While Not rst.EOF
Print #intFile, "<tr>"
For lngCount = 0 To lngMaxField
strLine = Trim(Nz(rst.Fields(lngCount).Value, ""))
If Len(strLine) = 0 Then
strLine = "&nbsp;"
End If
strLine = "<td>" & strLine & "</td>"
Print #intFile, strLine
Next lngCount
Print #intFile, "</tr>"
rst.MoveNext
Wend
Print #intFile, "</table>"
Print #intFile, "</body>"
Print #intFile, "</html>"

CreateProductHtml = True

End If

Exit_Handler:

If blnFileOpen Then
Close #intFile
End If

If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
' *** Code Ends ***

Closed Thread