'Thisworkbook Module
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call CancelAction
End Sub
Private Sub Workbook_Open()
UpdateNews 'on opening update the query with the news from the site www.msnbc.msn.com
Call ShowNews
End Sub
'Module1
Public i As Integer 'integer to count news
Public scadenza 'register time
Public counter As Integer 'counter to set updating frequency
Sub ShowNews()
On Error Goto fine 'if something doesn't work
Dim MyNews As String 'string to store current news
Dim AreaNews As Range 'range of news
'news stored in the queryof sheet News
Set AreaNews = ThisWorkbook.Worksheets("News").Range(ThisWorkbook.Worksheets("News").Names("www.msnbc.msn"))
'first time macro is run or all news were shown so restart
If i = 0 Or i = AreaNews.Cells.Count Then i = 1
If AreaNews.Cells(i).Value = vbNullString Then i = i + 1 'in case of blank cells
MyNews = AreaNews.Cells(i).Value 'current news to be shown
scadenza = Now + TimeSerial(0, 0, 5) 'calculate 5 seconds
Application.OnTime scadenza, "GoNextNews" 'in 5 seconds run again
Application.StatusBar = MyNews & " (CTRL+K to stop)" 'show in the statusbar
Exit Sub
fine:
Call CancelAction
End Sub
Sub GoNextNews()
i = i + 1 'skip to next news
counter = counter + 1 'increment for frequency
If counter = 100 Then Call UpdateNews 'change counter limit to change updating frequency
Call ShowNews
End Sub
Sub UpdateNews()
ThisWorkbook.Worksheets("News").QueryTables(1).Refresh 'update webquery
End Sub
'Module2
Sub CancelAction()
' delete the scheduled launch of next news
' Quick launch: CTRL+k
On Error Resume Next
Application.OnTime earliesttime:=scadenza, Procedure:="GoNextNews", Schedule:=False
'clear
Application.StatusBar = False
scadenza = 0
i = 0
counter = 0
End Sub
|