此版本為基本的建立新的紀事本並寫入資料,如果要運行較複雜的做法,例如:判斷路徑的資料夾存不存在,如果不存在是否要建立新的或報錯,如果有同樣的名字,是否要開啟記事本把資料寫入到最後一行或覆寫等等,等下次有時間在來PO文。
Sub Initialize
On Error Goto ErrorHandle
Dim session As NotesSession
Dim stream As NotesStream
Dim pathname As String
Set session = New NotesSession
Set stream = session.CreateStream
'設定路徑
pathname = "D:\" & "Test.txt"
'建立一個文件流並關聯上所指定路徑之檔案名稱,檔案不存在,系統會新建一個檔案。
If Not stream.Open(pathname, "ASCII") Then
Messagebox pathname,, "Open failed"
Exit Sub
End If
'當Bytes為0時,代表檔案為新檔案,反之,不等於0就是代表檔案原本已存在而且有內容。
If stream.Bytes <> 0 Then
Messagebox pathname,, "File already exists and has content"
Exit Sub
End If
'寫入資料
Call stream.WriteText("Test_Line_1", EOL_CRLF)
Call stream.WriteText("Test_Line_2", EOL_CRLF)
Call stream.WriteText("Test_Line_3", EOL_CRLF)
Call stream.WriteText("Test_Line_4", EOL_CRLF)
Call stream.WriteText("Test_Line_5", EOL_CRLF)
Call stream.Close
Exit Sub
ErrorHandle:
Msgbox ("ErrorPlace : 【Agent/(writeToText)/Initialize】, Error is : " & Str(Err) & " , ErrCode is : " & Error$ & " , " & "Error line is : " & Cstr(Erl))
Exit Sub
End Sub
沒有留言:
張貼留言