0
0

【VBA】ExcelからOutlookのスケジュールを自動作成する

Posted at

下記のマクロを作成する。(参照可能なライブラリでOutlookを追加する)

Sub Outlookの予定表へ登録する()

'Outlook用の定義
Dim olApp As Outlook.Application
Dim olItem  As AppointmentItem

'Excel用の定義
Dim wbBook As Workbook
Dim wsSheet As Worksheet

Dim lnContactCount As Long
Dim i As Long

'スクリーンの更新は行われません。
Application.ScreenUpdating = False

'Excelのブックとワークシートのオブジェクトを設定します。
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)


wsSheet.Activate

'Outlookオブジェクトを設定し、MAPI名前空間を介してOutlookの予定表を取得します。
Set olApp = New Outlook.Application


'取得結果を記述する行番号を指定します。2行目のセルから開始されることになります。
lnContactCount = 2

Dim rc As Integer
rc = MsgBox("予定表へ登録しますか?", vbYesNo + vbQuestion, "確認")

If rc = vbYes Then

    '予定表一覧の件数分繰り返す。
    For i = lnContactCount To Cells(1, 1).End(xlDown).Row
        Set olItem = olApp.CreateItem(olAppointmentItem)
        
        With olItem
            .Subject = Cells(i, 1)
            .Location = Cells(i, 2)
            .Start = Format(Cells(i, 3), "yyyy/mm/dd hh:mm:ss")
            .End = Format(Cells(i, 4), "yyyy/mm/dd hh:mm:ss")
            .Body = Cells(i, 5)
            .RequiredAttendees = Cells(i, 6)
            .OptionalAttendees = Cells(i, 7)
            
        End With
        
        'ここで保存
        olItem.Save

    Next

Else
    MsgBox "処理を中断します"
    Exit Sub
End If

'Null out the variables.
Set olItem = Nothing
Set olApp = Nothing

        
'Turn screen updating back on.
Application.ScreenUpdating = True

MsgBox "Outlook予定表の登録が完了しました!", vbInformation

End Sub

※Sheet名を変更した場合は、コードの変数も変更する

下記のようなExcelの表が必要
スクリーンショット 2024-07-17 003702.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0