2012年1月19日木曜日

VB.NETからNotesDBの新規文書を作成する

自分の環境では「参照の追加」は不要ですが、環境によっては必要かもしれませんので、ご注意を。

        Dim session As Object
        Dim db As Object
        Dim doc As Object

        Dim strDominoServer As String
        Dim strDBName As String

        'NotesDB接続初期化
        ' ***NotesDBからの読み込み
        ' *** セッションの確立
        session = CreateObject("Notes.NotesSession")

        ' *** ノーツDBの取得
        db = session.GetDatabase(strDominoServer, strDBName)

        If (Not db.IsOpen()) Then
            ' *** エラーメッセージの表示
            MsgBox("データベースが見つかりませんでした", vbOKOnly + vbCritical)
            GoTo load_exit
        End If

        doc = db.CreateDocument

        doc.ReplaceItemValue("Form", "MainDoc")
        doc.ReplaceItemValue("Subject", “テスト”)

        Call doc.Save(True, True)

load_exit:
        ' *** オブジェクトの破棄
        db = Nothing
        session = Nothing

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

NotesDBのデータを直接Excelに書き出す

ビューをCSVに書き出す機能は標準で用意されていますが、以下のスクリプトを参考にすれば、直接Excelに書き出すことが出来ます。(当然のことですが、Excelがインストールされていないと使えません。)
以下のスクリプトはビューのボタンに配置したときのサンプルです。

Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim view As NotesView
    Dim XLApp As Variant
    Dim XLBook As Variant
    Dim Code, Section, Name As Variant
    Dim x As Integer
    Set db = s.CurrentDatabase
    Set view = db.GetView("EmployeeView")
    Set doc = view.GetFirstDocument
    Set XLApp = createobject("Excel.Application")
    If ( XLApp Is Nothing ) Then
        Messagebox "Excelが見つかりません。
                             Excelが正しくインストールされていることを
                             確認してください。", MB_OK, "エラー"
        Exit Sub
    End If
    XLApp.Workbooks.add
    Set XLBook = XLApp.ActiveWorkbook
    XLApp.Visible=True
'見出し
    XLBook.Worksheets( 1 ).Cells( 1, 1 ).Value="社員コード"
    XLBook.Worksheets( 1 ).Cells( 1, 2 ).Value="部署"
    XLBook.Worksheets( 1 ).Cells( 1, 3 ).Value="氏名"
    x = 2
    Do Until doc Is Nothing
        Code = doc.GetItemValue("Code")
        Section = doc.GetItemValue("Section")
        Name = doc.GetItemValue("Name")
        XLBook.Worksheets( 1 ).Cells( x, 1 ).Value = Code(0)
        XLBook.Worksheets( 1 ).Cells( x, 2 ).Value = Section(0)
        XLBook.Worksheets( 1 ).Cells( x, 3 ).Value = Name(0)
        x = x + 1
        Set doc = view.GetNextDocument(doc)
    Loop
End Sub