Boiler Plateing text into another text document.

G

Geoff E

I am trying to import the content of a ".txt" file into a formatted
web page. I seem to have a good grasp on the writeing part but not the
reading (importing) part.

The following is where I am at at the moment:

Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\Tam\importBoPl.htm"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
MyBoilerPlate = "H:\Tam\testText.txt"
Open MyBoilerPlate For Output As #2
i = 0
Do While Not EOF(1)
Line Input #2, streng {I suspect my problem lays on this line}
i = i + 1
Loop
Close #2
Print #1, "</td></tr><tr><td>"
Print #1, "Saturday 12th September 0942hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub


This is what is produced:

<html><body><center><h2>Heading</h2>
Standard generated text - not imported
<table><tr><td>
{Note: The imported text should appear in here.}
</td></tr><tr><td>
Saturday 12th September 0942hrs
</td></tr></table></center></body></html>

Your comments, suggestions and assistance would be greatly
appreciated.
 
K

keiji kounoike

I don't know what "H:\Tam\testText.txt" is and how you like to have your
"importBoPl.htm" look. I guessed this is what you are looking for but
I'm not sure.

Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\Tam\testText.txt"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table>"
MyBoilerPlate = "H:\Tam\testText.txt"
Open MyBoilerPlate For Input As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, "<tr><td>"
Print #1, streng
Print #1, "</td></tr>"
Loop
Close #2
Print #1, "<tr><td>"
Print #1, "Saturday 12th September 0942hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

Keiji
 
G

Geoff E

I don't know what "H:\Tam\testText.txt" is and how you like to have your
"importBoPl.htm" look. I guessed this is what you are looking for but
I'm not sure.

Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\Tam\testText.txt"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table>"
MyBoilerPlate = "H:\Tam\testText.txt"
Open MyBoilerPlate For Input As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, "<tr><td>"
Print #1, streng
Print #1, "</td></tr>"
Loop
Close #2
Print #1, "<tr><td>"
Print #1, "Saturday 12th September 0942hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

Keiji










- Show quoted text -


Keiji... Thank you for your reply

"H:\Tam\testText.txt" is the path to the file that contains the text
to be imported. That text is in the form of a collection of
paragraphs, much like that which you would expect to see in a letter.
It does not contain delimited data which would be subjected to further
processing.

"H:\" is the identity of a flash drive which I am using for testing
purposes.

"importBoPl.htm" Is the name of the destination file which is the
script for a web page.

I actually tried your routine but, unfortunately, it did not
work.However, there was some very useful information on there so I
adjusted my routine to reflect what I learnt from yours. It still does
not work, but it now looks like this:


Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\MsTamsin\importBoPl.htm"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Page Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
MyBoilerPlate = "H:\MsTamsin\testText.txt"
Open MyBoilerPlate For Output As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, "streng"
i = i + 1
Loop
Close #2
Print #1, "</td></tr><tr><td>"
Print #1, "Monday 14th September 1327 hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub

Looking forward to further assistance on this matter.
Geoff E
 
K

keiji kounoike

Keiji... Thank you for your reply

"H:\Tam\testText.txt" is the path to the file that contains the text
to be imported. That text is in the form of a collection of
paragraphs, much like that which you would expect to see in a letter.
It does not contain delimited data which would be subjected to further
processing.

"H:\" is the identity of a flash drive which I am using for testing
purposes.

"importBoPl.htm" Is the name of the destination file which is the
script for a web page.

I actually tried your routine but, unfortunately, it did not
work.However, there was some very useful information on there so I
adjusted my routine to reflect what I learnt from yours. It still does
not work, but it now looks like this:


Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\MsTamsin\importBoPl.htm"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Page Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
MyBoilerPlate = "H:\MsTamsin\testText.txt"
Open MyBoilerPlate For Output As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, "streng"
i = i + 1
Loop
Close #2
Print #1, "</td></tr><tr><td>"
Print #1, "Monday 14th September 1327 hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
End Sub

Looking forward to further assistance on this matter.
Geoff E

If you don't mind, let me know how did my code not work.
Anyway, I think the line Print #1, "streng" in your code above should be

Print #1, streng

("" should not be added)

In your code, Print #1, "streng" will write the string "Strng" itself
into the importBoPl.htm file.

I think you should put Close #1 and Close #2 in error handle. if not, if
error ocuurs, then you can't open your files next time.

P.S I wonder what the variable i means?

Keiji
 
K

keiji kounoike

keiji said:
If you don't mind, let me know how did my code not work.
Anyway, I think the line Print #1, "streng" in your code above should be

Print #1, streng

("" should not be added)

In your code, Print #1, "streng" will write the string "Strng" itself
into the importBoPl.htm file.

I think you should put Close #1 and Close #2 in error handle. if not, if
error ocuurs, then you can't open your files next time.

P.S I wonder what the variable i means?

Keiji
 
K

keiji kounoike

keiji said:
If you don't mind, let me know how did my code not work.
Anyway, I think the line Print #1, "streng" in your code above should be

Print #1, streng

("" should not be added)

In your code, Print #1, "streng" will write the string "Strng" itself
into the importBoPl.htm file.

I think you should put Close #1 and Close #2 in error handle. if not, if
error ocuurs, then you can't open your files next time.

P.S I wonder what the variable i means?

Keiji

I forgot to mention one more thing.
you opened MyBoilerPlate with output mode, but this should be opened as
input mode. so, your

Open MyBoilerPlate For Output As #2

should be

Open MyBoilerPlate For Input As #2

I made a mistake in my code that Myfile and MyBoilerPlate point to the
same file "H:\Tam\testText.txt". As the result, My code would not work
correctly.

Keiji
 
G

Geoff E

I forgot to mention one more thing.
you opened MyBoilerPlate with output mode, but this should be opened as
input mode. so, your

Open MyBoilerPlate For Output As #2

should be

Open MyBoilerPlate For Input As #2

I made a mistake in my code that Myfile and MyBoilerPlate point to the
same file "H:\Tam\testText.txt". As the result, My code would not work
correctly.

Keiji- Hide quoted text -

- Show quoted text -

Again... thank you Keiji.

I now have it working just fine. This is what I ran with.

Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\Tam\importBoPl.htm"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
MyBoilerPlate = "H:\Tam\testText.txt"
Open MyBoilerPlate For Input As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, streng
Loop
Close #2
Print #1, "</td></tr>"
Print #1, "<tr><td>"
Print #1, "Saturday 12th September 1633 hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

I think you nailed all the issues I had with your origional, but there
was one other one:

The "<tr><td>" and "</td></tr>" strings were somehow in the wrong
place and/or duplicated.
While the code may have been working the resulting "HTM" file would
not render.

Regards
Geoff
 
K

keiji kounoike

Geoff said:
On Sep 14, 3:14 pm, keiji kounoike <"kounoike A | T ma.Pikara.ne.jp">
wrote:
I now have it working just fine. This is what I ran with.

Public Sub importBoilerPlate()
Dim i As Long
Dim streng As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
Myfile = "H:\Tam\importBoPl.htm"
Open Myfile For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
MyBoilerPlate = "H:\Tam\testText.txt"
Open MyBoilerPlate For Input As #2
i = 0
Do While Not EOF(2)
Line Input #2, streng
Print #1, streng
Loop
Close #2
Print #1, "</td></tr>"
Print #1, "<tr><td>"
Print #1, "Saturday 12th September 1633 hrs"
Print #1, "</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

I think you nailed all the issues I had with your origional, but there
was one other one:

The "<tr><td>" and "</td></tr>" strings were somehow in the wrong
place and/or duplicated.
While the code may have been working the resulting "HTM" file would
not render.

Regards
Geoff

Hi Geoff

I can't get it what you said -the resulting "HTM" file would not render.
Do you mean the file produced is not considered as "htm" file? Is it
your code above or my original code that the "<tr><td>" and "</td></tr>"
strings are in the wrong place and/or duplicated.
Anyway, How do want your file look like?

Keiji
 
G

Geoff E

Hi Geoff

I can't get it what you said -the resulting "HTM" file would not render.
  Do you mean the file produced is not considered as "htm" file? Is it
your code above or my original code that the "<tr><td>" and "</td></tr>"
strings are in the wrong place and/or duplicated.
Anyway, How do want your file look like?

Keiji- Hide quoted text -

- Show quoted text -

Hi Keiji.

The "htm" file that is scripted by this routine contains a table with
only two rows.
In the original routine the first row was closed after each pass of
the loop but new rows were not opened each time that this was done.

But I now have it working perfectly, thanks to your assistance. I have
also made the code a little more clearer and it now looks like this:

Public Sub importBoilerPlate()
Dim strng, trgt, imprt As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
trgt = ActiveSheet.Range("E4")
imprt = ActiveSheet.Range("F4")
Open trgt For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
Open imprt For Input As #2
Do While Not EOF(2)
Line Input #2, strng
Print #1, strng
Loop
Close #2
Print #1, "<br>0942</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

When I find the time, in the next few days, I will be looking at
getting it to loop through a whole series of pages instead of the
single one that it now does.

Rgds
Geoff
 
K

keiji kounoike

Geoff said:
On Sep 14, 5:44 pm, keiji kounoike <"kounoike A | T ma.Pikara.ne.jp">
wrote:
Hi Keiji.

The "htm" file that is scripted by this routine contains a table with
only two rows.
In the original routine the first row was closed after each pass of
the loop but new rows were not opened each time that this was done.

But I now have it working perfectly, thanks to your assistance. I have
also made the code a little more clearer and it now looks like this:

Public Sub importBoilerPlate()
Dim strng, trgt, imprt As String
On Error GoTo err_handler
ActiveWorkbook.Save
'===================================================================
trgt = ActiveSheet.Range("E4")
imprt = ActiveSheet.Range("F4")
Open trgt For Output As #1
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table><tr><td>"
Open imprt For Input As #2
Do While Not EOF(2)
Line Input #2, strng
Print #1, strng
Loop
Close #2
Print #1, "<br>0942</td></tr></table></center></body></html>"
Close #1
'=====================================================================
Exit Sub
err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

When I find the time, in the next few days, I will be looking at
getting it to loop through a whole series of pages instead of the
single one that it now does.

Rgds
Geoff

Hi Geoff

I can't get what to loop through a whole series of pages, so this could
be a quite diffrernt one from what you are looking for. but give it a try.

Public Sub importBoilerPlate_Test()
Dim strng As String, trgt As String, imprt As String
Dim tmp As String, tmpimprt As String
Dim n As Long
Dim arr

On Error GoTo err_handler

ActiveWorkbook.Save
imprt = ActiveSheet.Range("E4")
trgt = ActiveSheet.Range("F4")

If Dir(trgt) = "" Then
Open trgt For Output As #1
Open imprt For Input As #2
Print #1, "<html><body><center><h2>Heading</h2>"
Print #1, "Standard generated text - not imported "
Print #1, "<table>"
Else
arr = Split(trgt, "\")
arr(UBound(arr)) = "Tmp_" & arr(UBound(arr))
tmpimprt = Join(arr, "\")
Open tmpimprt For Output As #1
Open trgt For Input As #2
Do While Not EOF(2)
Line Input #2, strng
n = InStr(strng, "</table>")
If n = 0 Then
Print #1, strng
ElseIf Left(strng, n - 1) <> "" Then
Print #1, Left(strng, n - 1)
Exit Do
Else
Exit Do
End If
Loop
Close #1
Close #2
Kill trgt
Name tmpimprt As trgt
Open trgt For Append As #1
Open imprt For Input As #2
End If

strng = ""

Do While Not EOF(2)
tmp = Input(1, #2)
If tmp = Chr(10) And strng <> "" Then
Print #1, "<tr><td>"
Print #1, strng
Print #1, "</td></tr>"
strng = ""
ElseIf tmp = Chr(13) And strng <> "" Then
Print #1, "<tr><td>"
Print #1, strng
Print #1, "</td></tr>"
strng = ""
Else
strng = strng & tmp
End If
Loop
Print #1, "</table></center></body></html>"
Close #1
Close #2
Exit Sub

err_handler:
MsgBox Err.Number & vbCrLf & Err.Description
Close #1
Close #2
End Sub

Keiji
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top