3

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
omegastripes
  • 12,351
  • 4
  • 45
  • 96
Zulake
  • 149
  • 3
  • 14
  • [This](http://stackoverflow.com/questions/11780366/vb-script-or-vba-code-to-copy-the-contents-of-a-web-webpage-to-a-word-excel-shee) might be of some help. – Pankaj Jaju Jan 29 '14 at 17:10
  • Problem is that webpage I want to collect the data uses JavaScript to render the data, so downloading source code of the page will not work since it does not contain that data. That's why I was going for a Select-All copy and paste – Zulake Jan 29 '14 at 19:47
  • 1
    VBScript is not the right tool for this kind of automation. Use something like [AutoIt](http://www.autoitscript.com/site/autoit/) instead. – Ansgar Wiechers Jan 29 '14 at 20:22

1 Answers1

3

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
omegastripes
  • 12,351
  • 4
  • 45
  • 96