' Thread: http://www.mrexcel.com/forum/showthread.php?t=603712
' Poster: Trebor76
' Written: January 07, 2012
' Author: Leith Ross
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32.dll" () As Long
Private Declare Function OpenClipboard Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function StrLen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef pDst As Any, ByRef pSrc As Long, ByVal ByteLen As Long)
Function GetTextFromClipboard() As String
Const CF_TEXT = 1
Dim cch As Long
Dim hStrPtr As Long
Dim Text As String
OpenClipboard (0)
hStrPtr = GetClipboardData(CF_TEXT)
If hStrPtr <> 0 Then
cch = StrLen(hStrPtr)
If cch > 0 Then
Text = String(cch, 0)
CopyMemory ByVal Text, ByVal hStrPtr, cch
GetTextFromClipboard = Text
End If
End If
CloseClipboard
End Function
Sub CopyWebText()
Dim Filename As String
Dim fn As Integer
Dim I As Long
Dim ieApp As Object
Dim ieDoc As Object
Dim ieBtn As Object
Dim ieBtns As Object
Dim oShell As Object
Dim Text As String
Dim URL As String
Filename = "Copy of AusStats.txt"
URL = "http://www.abs.gov.au/AUSSTATS/abs@.nsf/webpages/ABS%20Release%20Calendar%20Export?opendocument"
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Navigate URL
ieApp.Visible = True
While ieApp.Busy Or ieApp.ReadyState <> 4: DoEvents: Wend
Set oShell = CreateObject("Shell.Application")
For Each oShWindow In oShell.Windows
If oShWindow.LocationName = "ABS Release Calendar Export" Then
Set ieDoc = oShWindow.Document
End If
Next oShWindow
Set ieBtns = ieDoc.getElementsByTagName("input")
For I = 0 To ieBtns.Length - 1
If ieBtns(I).Value = "Highlight All" Then
Set ieBtn = ieBtns(I)
Exit For
End If
Next I
If ieBtn Is Nothing Then MsgBox """Highlight All"" button not found.": Exit Sub
ieBtn.Click
ieApp.ExecWB 12, 0
Text = GetTextFromClipboard
fn = FreeFile
Open Filename For Output Access Write Lock Write As #fn
Print #fn, Text
Close fn
ieApp.Quit
End Sub