1

In the following code, how can I save the text to a text file (text.txt for example) instead of the current MsgBox?

myURL = "http://URL.com"

Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set ohtmlFile = CreateObject("htmlfile")

oXMLHttp.Open "GET", myURL, False
oXMLHttp.send

If oXMLHttp.Status = 200 Then

ohtmlFile.Write oXMLHttp.responseText
ohtmlFile.Close

Set oTable = ohtmlFile.getElementsByTagName("table")
For Each oTab In oTable
    MsgBox oTab.Innertext
Next
End If

WScript.Quit

Please, help me!

Thanks!

1 Answer 1

2

You can use the FileSystemObject's OpenTextFile method.

You can create the FileSystemObject at the top of your code with your other objects:

Set objFSO = CreateObject("Scripting.FileSystemObject")

And add these constants:

Const ForReading = 1, ForWriting = 2, ForAppending = 8

If you want to append everything into the same file, you can create and open the file outside of your loop:

sFileName = "c:\text.txt"
Set objFile = objFSO.OpenTextFile(sFileName, ForAppending, True)
For Each oTab In oTable
    objFile.WriteLine oTab.Innertext
Next
objFile.Close

Otherwise you can create multiple files within your loop:

Dim iTableCounter
iTableCounter = 0

For Each oTab In oTable

    iTableCounter = iTableCounter + 1
    sFileName = "c:\table_" & iTableCounter & ".txt" ' create a dynamic file name using table name perhaps

    Set objFile = objFSO.OpenTextFile(sFileName, ForWriting, True)
    objFile.Write oTab.Innertext
    objFile.Close

Next
Sign up to request clarification or add additional context in comments.

3 Comments

It worked! Is there any way to save on different lines in the same file? Whether it's possible, Can you send me the code with this change? Thank you very much!
Yes, just need to use WriteLine instead of Write. I have updated the answer. Please mark the question as answered using gray checkmark to the left of the answer when you get a chance!
It really solved my problem. I already mark the question as answered. I am very grateful for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.