2012年1月5日木曜日

スクリプトによるNotesDBからCSV書き出し

「ファイル」-「書き出し」からでもCSV書き出しが出来ますが、スクリプトによるCSV書き出しのサンプルコードです。

Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim doc As NotesDocument
    Dim Code, Section, Name As Variant
    Dim FileName As String   
    Dim FileNum As Long
    Dim i As Integer

    Set db = s.CurrentDatabase
    Set view = db.GetView("EmployeeView")
    Set doc = view.GetFirstDocument

    FileName = "C:\Employee.CSV"
    FileNum = Freefile '空いている番号を取得する
    Open FileName For Output As #FileNum '書き込みモードで開く

    Do Until doc Is Nothing
        Code = doc.GetItemValue("Code")
        Section = doc.GetItemValue("Section")
        Name = doc.GetItemValue("Name")
        Print #FileNum , & _
        Code(0) & "," & _
        Section(0) & "," & _
        Name(0)
        Set doc = view.GetNextDocument(doc)
    Loop

    Close #FileNum 'ファイルを閉じる

End Sub