Private Sub CommandButton26_Click()
'Dim objDumpStuff As clsPenData, br
'Dim objAllPensInfo As Object
Dim lNumPts As Long
Dim vVal As Variant
Dim vDate As Variant
Dim vQual As Variant
Dim DatafromPens As Variant
Dim ColCnt As Integer
Dim cnt As Integer
Dim TagNames As Collection
Dim TagDescription As Collection
Dim Pen As Object
Dim Results() As Variant
' Export file name
Dim filetemp As String
Dim FileName As String
filetemp = CStr(Now)
' 21/07/2017 21:00:23 = filename
filetemp = Replace(filetemp, "/", "_", 1, -1, vbTextCompare)
filetemp = Replace(filetemp, ":", "_", 1, -1, vbTextCompare)
FileName = System.FixPath(App_Path) + "\" + filetemp + ".csv"
' can't do a new in VBA, so this property is a proxy to a new object
'Set objDumpStuff = objPenDataClass
' get all pens info
'Set objAllPensInfo = objDumpStuff.GetPensInfo(Chart1)
' export the information to the Dynamics\App directory "\ChartOutput.CSV"
'objDumpStuff.ExportPensInfoToCSV objAllPensInfo, filename, True
ColCount = 1 ' reset the count
For Each Pen In Chart1.Pens ' for each pen
DatafromPens = Pen.GetPenDataArray(lNumPts, vVal, vDate, vQual)
' Only need to redimension the matrix once.
If ColCount = 1 Then
ReDim Results(lNumPts + 2, Chart1.Pens.Count)
End If
For cnt = 0 To lNumPts - 1
Results(cnt + 2, ColCount) = vVal(cnt)
Next cnt
Results(0, ColCount) = Pen.Source
Results(1, ColCount) = Pen.PenDescription
ColCount = ColCount + 1 ' increment counter.
Next Pen
' set the date and time
For cnt = 0 To lNumPts - 1
Results(cnt + 2, 0) = vDate(cnt)
Next cnt
' set the header
Results(0, 0) = "Date"
Results(1, 0) = "Time"
' At this stage we have all we need in a Result Matrix. Now we need to put it in a file.
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(FileName, True)
For cnt = 0 To UBound(Results, 1) ' lines
For ColCount = 0 To UBound(Results, 2) ' columns
a.Write (Results(cnt, ColCount))
a.Write (";")
Next ColCount
a.WriteLine
Next cnt
a.Close
MsgBox "Files Exported to " + FileName, vbInformation, "Pen Export"
End Sub