【发布时间】:2022-10-06 01:05:54
【问题描述】:
以下代码将自动将约会的 BODY(无论是新创建的还是刚刚修改的)发送到 MySQL(到名为 report 的表中,在名为 BODY 的列下)。但是,它仅在约会在默认日历中时才有效。
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items
Private WithEvents objItems2 As Outlook.Items
Private Sub Application_Startup()
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace(\"MAPI\")
\'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderCalendar)
Set objItems = objWatchFolder.Items
Set objItems2 = objWatchFolder.Items
Set objWatchFolder = Nothing
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
\' Your code goes here
\' MsgBox \"Message subject: \" & Item.Subject & vbCrLf & \"Message sender: \" & Item.SenderName & \" (\" & Item.SenderEmailAddress & \")\"
\' https://www.slipstick.com/developer/itemadd-macro
MsgBox \"*** PROPERTIES of olFolderCalendar ***\" & vbNewLine & _
\"Subject: \" & Item.Subject & vbNewLine & _
\"Start: \" & Item.Start & vbNewLine & _
\"End: \" & Item.End & vbNewLine & _
\"Duration: \" & Item.Duration & vbNewLine & _
\"Location: \" & Item.Location & vbNewLine & _
\"Body: \" & Item.Body & vbNewLine & _
\"Global Appointment ID: \" & Item.GlobalAppointmentID
send2mysql Item
Set Item = Nothing
End Sub
Private Sub objItems2_ItemChange(ByVal Item As Object)
MsgBox \"*** PROPERTIES of olFolderCalendar ***\" & vbNewLine & _
\"Subject: \" & Item.Subject & vbNewLine & _
\"Start: \" & Item.Start & vbNewLine & _
\"End: \" & Item.End & vbNewLine & _
\"Duration: \" & Item.Duration & vbNewLine & _
\"Location: \" & Item.Location & vbNewLine & _
\"Body: \" & Item.Body & vbNewLine & _
\"Global Appointment ID: \" & Item.GlobalAppointmentID
send2mysql Item
Set Item = Nothing
End Sub
Sub send2mysql(ByVal Item As Object)
Dim updSQL As String
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String
strConn = \"Driver={MySQL ODBC 8.0 ANSI Driver};Server=localhost; Database=thairis; UID=root; PWD=root\"
cn.Open strConn
updSQL = \"INSERT INTO report (BODY) VALUES (\" & Item.Body & \"\')\"
cn.Execute updSQL
MsgBox updSQL
MsgBox \"Done\"
End Sub
如果我在自定义日历(例如“我的测试日历”)中创建或修改约会,则不会触发任何内容。
问题:除了默认日历之外,我如何让上述代码响应任何自定义日历的 objItems_ItemAdd 或 objItems_ItemModify?
提前致谢。
我在 Windows 10(64 位)上使用离线桌面版 Outlook 2016。
-
这回答了你的问题了吗? Adding Listeners to different folders in Outlook