Scripting.FileSystemObject - how big can be a file?

dscheste

Board Regular
Joined
Jul 10, 2004
Messages
206
Hello guys,

I decided to export a knowledge database. The FAQ is running on PHPMyFAQ.

I decided to create a html file and with xmlhttp parse the topic and the FAQ, surrond it with some html formatting - and bingo.

Thought I.

Excel does it perferctly, all the faqs land in the worksheet as I request, providing the running numbers for the question IDs.

When I introduce the Scripting.FileSystemObject and create a text file, the file is created all right but is filled with 5 records only. Then the code throws

Run time error 5 and errors out.

Why is that? Is there a cure? I hoped to have the page like 50 pages, but it just does not want to grow 5 records. If I cut the topics or formatting, it accomodates 6. But that is it.

Please help.

Here is the code:


the variable cell is the ID number of the FAQ

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Get_Me_The_FAQ()
<SPAN style="color:#00007F">With</SPAN> Application
.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> fso, tf, xmlhttp, topic, faq, mystart, myend, artikelsize
            <SPAN style="color:#00007F">Set</SPAN> fso = CreateObject("Scripting.FileSystemObject")
            <SPAN style="color:#00007F">Set</SPAN> tf = fso.CreateTextFile("c:\parsed.html", <SPAN style="color:#00007F">True</SPAN>)
            <SPAN style="color:#00007F">Set</SPAN> Rng = Range("B2:B50")
            tf.Writeline ("<html><head><title>Parsed from myPHP</title></head><body>")
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> Rng
<SPAN style="color:#00007F">Set</SPAN> xmlhttp = CreateObject("Microsoft.XMLHTTP")
strURL = "my_working_URL"
xmlhttp.Open "GET", strURL, <SPAN style="color:#00007F">False</SPAN>, "", ""
xmlhttp.Send
rtnpage = xmlhttp.ResponseText
            <SPAN style="color:#007F00">'Get the topic</SPAN>
            Range("A2").Value = cell.Row
            mystart = InStr(rtnpage, "[/i]")
            myend = InStr(rtnpage, "</h2>")
            artikelsize = myend - mystart
            topic = Mid(rtnpage, mystart + 5, artikelsize - 5)
            cell.Offset(0, 1).Value = topic
            tf.Write ("some html format" & topic & "some html format") <SPAN style="color:#007F00">' Writes the name of the topic</SPAN>
            <SPAN style="color:#00007F">Set</SPAN> topic = <SPAN style="color:#00007F">Nothing</SPAN>
                        <SPAN style="color:#007F00">'Get the topic text</SPAN>
                        mystart = InStr(rtnpage, "")
                        myend = InStr(rtnpage, " ")
                        artikelsize = myend - mystart
                        faq = Mid(rtnpage, mystart + 22, artikelsize - 21)
                        cell.Offset(0, 2).Value = faq
                        <SPAN style="color:#007F00">'MsgBox (faq)</SPAN>
                         tf.Write ("some html format " & faq & "some html format") <SPAN style="color:#007F00">'writes the actual answer</SPAN>
                         <SPAN style="color:#00007F">Set</SPAN> faq = <SPAN style="color:#00007F">Nothing</SPAN>
<SPAN style="color:#00007F">Next</SPAN>

.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
   tf.Writeline ("</body></html>")
   tf.Close
   <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
error:
                        MsgBox ("Error occured")
                        tf.Writeline ("</body></html>")
                        tf.Close
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I had to do mail merge through word using the parsed data from excel.
Scripting.FileSystemObject thang just won't write to the file.

The mail merged file was saved as text and then renamed into html. Looks beautiful, but that should have been done with .write...

Any ideas?
 
Upvote 0
I guess all the active gurus enjoy the WE:)

I managed to resolve the issue, however, I would like a piece of advice as why the regular CreateTextFile and then .write does not seem to work in this case.

I have resolved the issue by not only creating the file, but by rather opening it as a text stream for writing. I do not see the difference, Microsoft MSDN keeps silence as well.

The working code is:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Get_Me_The_FAQ()
<SPAN style="color:#00007F">Const</SPAN> ForWriting = 2
<SPAN style="color:#00007F">With</SPAN> Application
.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> fso, tf, xmlhttp, topic, faq, mystart, myend, artikelsize, ID, cell, f1
            <SPAN style="color:#00007F">Set</SPAN> fso = CreateObject("Scripting.FileSystemObject")
<SPAN style="color:#00007F">Set</SPAN> f1 = fso.GetFile("c:\parsed.html")
<SPAN style="color:#00007F">Set</SPAN> tf = f1.OpenAsTextStream(ForWriting, <SPAN style="color:#00007F">True</SPAN>)
            <SPAN style="color:#00007F">Set</SPAN> rng = Range("B2:B155") <SPAN style="color:#007F00">'specify which FAQs to download - in my case just running numbers</SPAN>
            tf.Writeline ("<html><head><title>Parsed from myPHP</title></head><body>") <SPAN style="color:#007F00">'light html formatting</SPAN>
<SPAN style="color:#00007F">Set</SPAN> xmlhttp = CreateObject("Microsoft.XMLHTTP")
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> rng
strURL = "my_working_URL"
xmlhttp.Open "GET", strURL, <SPAN style="color:#00007F">False</SPAN>, "", ""
xmlhttp.Send
rtnpage = xmlhttp.ResponseText
            <SPAN style="color:#007F00">'Get the topic</SPAN>
            Range("A2").Value = cell.Row
            mystart = InStr(rtnpage, "[/i]")
            myend = InStr(rtnpage, "</h2>")
            artikelsize = myend - mystart
            topic = Mid(rtnpage, mystart + 5, artikelsize - 5)
            cell.Offset(0, 1).Value = topic
            tf.Write ("formatting" & topic & "formatting") <SPAN style="color:#007F00">' Writes the name of the topic</SPAN>
                        <SPAN style="color:#007F00">'Get the topic text</SPAN>
                        mystart = InStr(rtnpage, "")
                        myend = InStr(rtnpage, " ")
                        artikelsize = myend - mystart
                        faq = Mid(rtnpage, mystart + 22, artikelsize - 21)
                        cell.Offset(0, 2).Value = faq
                         tf.Write ("formatting" & faq & "formatting") <SPAN style="color:#007F00">'writes the actual answer</SPAN>
<SPAN style="color:#00007F">Next</SPAN>
.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
   tf.Writeline ("</body></html>")
   tf.Close
   <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
error:
                        MsgBox ("Error occured")
                        tf.Writeline ("</body></html>")
                        tf.Close
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

</FONT>
 
Upvote 0

Forum statistics

Threads
1,221,483
Messages
6,160,097
Members
451,617
Latest member
vincenzo1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top