2016-12-30 63 views
0

我用這段代碼做了一個關於vba的日誌,然後我意識到如果數據已經很多了,那麼很難找到「新日誌」。有沒有修改這個代碼的方式,所以我能得到的數據的頂部(記事本/ TXT)登錄VBA

Sub WriteLogFile() 

     Dim iFileNumber As Long 
     Dim strData As String 
     Dim strFileName As String 

     iFileNumber = FreeFile() 
     strData = "Test data"   'the text written in the file 
     strFileName = "C:\test.log"  ' the text file 
     Open strFileName For Append Shared As #iFileNumber 
     Print #iFileNumber, strData 
     Close #iFileNumber 
End Sub 

回答

1

VBA可以使用組件從dotNet的框架太「新記錄」。所以我強烈建議你使用一個日誌框架,如log4n。它使您可以非常精確地控制日誌記錄活動。

缺點 - 修改您的日誌文件名稱行以包含日期。所以你每天得到一個日誌文件。不要嘗試在文件的開頭寫入數據。這將是一個性能災難在等待

+0

感謝您的意見 –

+0

最受歡迎。希望能幫助到你 – stwissel