' Excel macros for periodically writing a spreadsheet to a file.

' Used for Excel 2010 in Windows 7 Ultimate.

' D. R. Williamson  November 2011.

' Reference: Walkenbach, J., "Excel 2010 Power Programming with VBA," 
'            Wiley, 2010, p. 670.

' Warning: This file has been edited to improve comments, but has not
' been rerun in Excel.

' The constant called step is the duty cycle in days. 
' Writing every 60 seconds requires step=6.94e-04 days:
Public Const step As Double = (60 / 86400)
Public earliest As Double

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' November 20, 2011.  Exporting text to a file was copied from 
'    http://www.cpearson.com/excel/ImpText.aspx

' ExportToTextFile
' This exports a sheet or range to a text file, using a
' user-defined separator character.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ExportToTextFile(FName As String, _
    Sep As String, SelectionOnly As Boolean, _
    AppendData As Boolean)

Dim WholeLine As String
Dim FNum As Integer
Dim RowNdx As Long
Dim ColNdx As Integer
Dim StartRow As Long
Dim EndRow As Long
Dim StartCol As Integer
Dim EndCol As Integer
Dim CellValue As String

Application.ScreenUpdating = False
On Error GoTo EndMacro:
FNum = FreeFile

If SelectionOnly = True Then
    With Selection
        StartRow = .Cells(1).Row
        StartCol = .Cells(1).Column
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
Else
    With ActiveSheet.UsedRange
        StartRow = .Cells(1).Row
        StartCol = .Cells(1).Column
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
End If

If AppendData = True Then
    Open FName For Append Access Write As #FNum
Else
    Open FName For Output Access Write As #FNum
End If

For RowNdx = StartRow To EndRow
    WholeLine = ""
    For ColNdx = StartCol To EndCol
        If Cells(RowNdx, ColNdx).Value = "" Then
            CellValue = Chr(34) & Chr(34)
        Else
           CellValue = Cells(RowNdx, ColNdx).Value
        End If
        WholeLine = WholeLine & CellValue & Sep
    Next ColNdx
    WholeLine = Left(WholeLine, Len(WholeLine) - Len(Sep))
    Print #FNum, WholeLine
Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #FNum

End Sub

Sub DoTheExport()
' Export the file to NFS drives F and G.
 
'   Use these for testing:
'   MsgBox ("Top of DoTheExport")
'   ExportToTextFile FName:="C:\Users\dale\xltest.txt", Sep:=",", _
      SelectionOnly:=False, AppendData:=False
      
'   Export to drive F:
    ExportToTextFile FName:="F:\xlequotes.txt", Sep:=",", _
      SelectionOnly:=False, AppendData:=False

'   Export to drive G:
    ExportToTextFile FName:="G:\xlequotes.txt", Sep:=",", _
      SelectionOnly:=False, AppendData:=False
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ExportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' D. R. Williamson
' November 26, 2011

' The following macros control writing the spreadsheet:
'    Function tasker() periodically writes the spreadsheet.
'    Function tasker_stop() halts tasker().
'    Function tasker1() replaces DoTheExport() when testing.

' A macro is started by selecting Developer->Macros in Excel.

Sub tasker()
' Run every step hours and export the spreadsheet to NFS files.
    earliest = Now + step 'needed in tasker_stop() or it will not work
    Application.OnTime earliest, "tasker"
    DoTheExport
'   tasker1 ' remove DoTheExport and run this when testing
End Sub

Sub tasker_stop()
' Stop tasker().
    On Error Resume Next
    Application.OnTime earliest, "tasker", , False
    MsgBox ("tasker() has been stopped")
End Sub

Sub tasker1()
' Replaces DoTheExport when testing.
    MsgBox ("Saving spreadsheet")
End Sub


