locked
How to read the code behind a web page and write it into a word doc RRS feed

  • Question

  • I am trying to read the html text from behind a web page, edit it and write it back to a word doc using VBA. I have the edit code, I just need code in my macro to read the html of the web page and then write the edited html back to a word doc.

    Any suggestions as to how I may be able to do this? So far I have tried this code to read the text, but am getting an error on the 5th line "Set ts = fileobj.openastextstream(forreading, tristateusedefault)"

    Sub write()
    Set fso = CreateObject("scripting.filesystemobject")
    FileName = "C:\Users\Meghan.Sposato\Desktop\tester.html"
    Set fileobj = fso.getfile(FileName)
    Set ts = fileobj.openastextstream(forreading, tristateusedefault)
    textline = ""
    Do While ts.atendofstream <> True
    textline = textline & ts.readline & "<BR>"
    Loop

    thanks,

    Meghan

    • Moved by Damon Zheng Friday, May 24, 2013 12:04 PM Not Word specific
    Wednesday, May 22, 2013 4:18 PM

Answers

  • If you are trying to read the HTML c/w tags into the document then

    Sub Macro1()
    Dim i As Long
    Const strPath As String = "C:\Users\Meghan.Sposato\Desktop\tester.html"
    Dim strHTM() As String
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        On Error Resume Next
        FileToArray strPath, strHTM
        For i = 0 To UBound(strHTM)
            oRng.Text = oRng.Text & strHTM(i)
        Next i
        Set oRng = Nothing
    End Sub

    Public Function FileToArray(ByVal Filename As String, _
                           ByRef TheArray As Variant)

    Dim oFSO As New FileSystemObject
    Dim oFSTR As Scripting.TextStream
    Dim ret As Long
    Dim lCtr As Long
    If Dir(Filename) = "" Then Exit Function
    If VarType(TheArray) <> vbArray + vbString Then Exit Function
    On Error GoTo ErrorHandler
    Set oFSTR = oFSO.OpenTextFile(Filename)
    Do While Not oFSTR.AtEndOfStream
        ReDim Preserve TheArray(lCtr) As String
        TheArray(lCtr) = oFSTR.ReadLine
        lCtr = lCtr + 1
        DoEvents
    Loop
    oFSTR.Close
    ErrorHandler:
    Set oFSTR = Nothing
    End Function

    If you are trying to translate the web page into a formatted Word document then you need simply open it

    Sub Macro2()
    Const strPath As String = "C:\Users\Meghan.Sposato\Desktop\tester.html"
    Documents.Open Filename:=strPath, ConfirmConversions:=False, ReadOnly _
            :=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _
            :="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _
            , Format:=wdOpenFormatAuto, XMLTransform:=""
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Damon Zheng Wednesday, May 29, 2013 12:10 PM
    Friday, May 24, 2013 1:57 PM

All replies

  • Hello,

    According to your description, this issue is not specific to Word application and Word Object Modals. We will move this thread to VBA forum for better discussion.

    Thanks for your understanding.

    Regards,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 24, 2013 12:28 PM
  • If you are trying to read the HTML c/w tags into the document then

    Sub Macro1()
    Dim i As Long
    Const strPath As String = "C:\Users\Meghan.Sposato\Desktop\tester.html"
    Dim strHTM() As String
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        On Error Resume Next
        FileToArray strPath, strHTM
        For i = 0 To UBound(strHTM)
            oRng.Text = oRng.Text & strHTM(i)
        Next i
        Set oRng = Nothing
    End Sub

    Public Function FileToArray(ByVal Filename As String, _
                           ByRef TheArray As Variant)

    Dim oFSO As New FileSystemObject
    Dim oFSTR As Scripting.TextStream
    Dim ret As Long
    Dim lCtr As Long
    If Dir(Filename) = "" Then Exit Function
    If VarType(TheArray) <> vbArray + vbString Then Exit Function
    On Error GoTo ErrorHandler
    Set oFSTR = oFSO.OpenTextFile(Filename)
    Do While Not oFSTR.AtEndOfStream
        ReDim Preserve TheArray(lCtr) As String
        TheArray(lCtr) = oFSTR.ReadLine
        lCtr = lCtr + 1
        DoEvents
    Loop
    oFSTR.Close
    ErrorHandler:
    Set oFSTR = Nothing
    End Function

    If you are trying to translate the web page into a formatted Word document then you need simply open it

    Sub Macro2()
    Const strPath As String = "C:\Users\Meghan.Sposato\Desktop\tester.html"
    Documents.Open Filename:=strPath, ConfirmConversions:=False, ReadOnly _
            :=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _
            :="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _
            , Format:=wdOpenFormatAuto, XMLTransform:=""
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Damon Zheng Wednesday, May 29, 2013 12:10 PM
    Friday, May 24, 2013 1:57 PM