Sub GetDividends(StockSymbols() As String)
Dim DH As MrvInstrument, StockFile As String, fs As FileSystemObject, ft As TextStream
Dim i As Long, LoopsAs Long, ErrorString As String, Symbol As String, dData As Variant, tmpStr As String
Dim Fields(0 To 3) As String
Fields(0) = "DPS_EX_DATE"
Fields(1) = "DPS_PAY_DATE"
Fields(2) = "DPS_GROSS_UNADJ"
Fields(3) = "DPS_CCY_CODE"
Condition = "DPS_ANNOUNCE_DATE:T-1Y<T"
Const myFolder = "myFolder"
Const MaxLoops = 1000000
Set DH = New MrvInstrument
Set fs = New FileSystemObject
StockFile = myFolder & "Dividends" & Format(Date, "mmddyy") & ".txt"
Set ft = fs.OpenTextFile(StockFile, ForWriting, True)
For i = 1 To UBound(StockSymbols)
Symbol = StockSymbols(i)
With DH
.Mode = "SOURCE:EQUITY MATRIX:YES"
.InstrumentID = Symbol
.RequestTimeSeries "DIVIDEND_HISTORY", Fields, Condition
Do While DH.RunStatus = DE_RS_BUSY
DoEvents
Loops = Loops + 1
If Loops > MaxLoops Then Exit Do
Loop
If Loops > MaxLoops Then Debug.Print Symbol
If IsError(.ErrorString) Then
ErrorString = "#Error"
Else
ErrorString = .ErrorString
End If
If Len(ErrorString) = 0 Then
For j = 1 To UBound(.Data, 2)
For k = 1 To 4
dData(k) = .Data(k, j)
If IsError(dData(k)) Then dData(k) = "#N/A N/A"
Next k
tmpStr = StockSymbols(i) & ";" & dData(1) & ";" & dData(2) & ";" & dData(3) & ";" & dData(4)
ft.WriteLine tmpStr
Next j
End If
End With
Next i
ft.Close
Set ft = Nothing
Set fs = Nothing
Set DH = Nothing
End Sub