Navigate to a Website, SelectAll, Copy and Paste i

2019-01-18 16:58发布

问题:

I'm trying to create a VB script to Navigate to a Website, SelectAll, Copy and then save the copied data from the clipboard to a text file, but I'm stuck! :(

Here is what I got so far:

With CreateObject("InternetExplorer.Application")
    .Navigate "https://www.microsoft.com"
    Do Until .ReadyState = 4: Wscript.Sleep 100: Loop
    .Visible = true
    With .Document
        .execCommand "SelectAll"
        .execCommand "Copy"
    End With ' Document

回答1:

You can try to get text data directly from DOM

With CreateObject("InternetExplorer.Application")
    .Visible = True
    .Navigate "https://www.microsoft.com"
    Do Until .ReadyState = 4
        Wscript.Sleep 100
    Loop
    For Each Tag In .Document.GetElementsByTagName("script")
        Tag.OuterHtml = ""
    Next
    For Each Tag In .Document.GetElementsByTagName("noscript")
        Tag.OuterHtml = ""
    Next
    Content = .Document.GetElementsByTagName("body")(0).InnerText
    Do While InStr(Content, vbCrLf & vbCrLf)
        Content = Replace(Content, vbCrLf & vbCrLf, vbCrLf)
    Loop
    ShowInNotepad Content
    .Quit
End With

Sub ShowInNotepad(Content)
    With CreateObject("Scripting.FileSystemObject")
        TempPath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "\" & .GetTempName
        With .CreateTextFile(TempPath, True, True)
            .WriteLine (Content)
            .Close
        End With
        CreateObject("WScript.Shell").Run "notepad.exe " & TempPath, 1, True
        .DeleteFile (TempPath)
    End With
End Sub