我目前有一個項目即將完成,但我陷入了最後的障礙。使用VBA從Access發送Outlook預約
我想要實現的是讓Access向工程師發送電子郵件並在他們的Outlook日曆中設置提醒。現在電子郵件已經排序,它只是日曆提醒,它已經困擾了我。
我從https://access-programmers.co.uk/forums/showthread.php?t=209552找到的東西看起來是正確的,所以我剪掉了我不需要的代碼,這是剩下的。
Option Compare Database
Sub Outlook()
Dim obj0App As Object
Dim objAppt As Object
Set obj0App = CreateObject("outlook.Application")
Set objAppt = obj0App.CreateItem(olAppointmentItem)
With objAppt
.requiredattendees = EmailAddy.Value
.optionalattendees = ASMail.Value
.subject = "Training booked for " & " " & QualificationEmail.Value
.Importance = 2 ' high
.Start = STdate.Value & "Starting at" & " " & StTime.Value
.End = Edate.Value
.Location = Location.Value
.Reminderminutesbeforestart = 20160 'reminder set for two weeks before the event
.Body = "Training for" & " " & [QualificationEmail] & "." & vbNewLine & "Any changes to this arrangement will be emailed to you. You will recieve any confirmation for bookings nearer the time."
.Meetingstatus = 1
.responserequested = True
.Save
.display
.send
MsgBox "Appointment sent"
End With
End Sub
當我測試代碼時,我遇到的問題是.requiredattendee導致運行時錯誤424對象必需。
如果有人可以讓我知道爲什麼VBA不認可必需和可選參加者?
備註: 聲明值的部分是;電子郵件Addy,ASMail,QualificationsEmail,STdate,StTime,Edate &位置。所有鏈接到Access數據庫表單,在文本框中使用Dlookup,如下面的示例。
=DLookUp("[Engineer Email]","[EngTrainForm]","'[Windows ID]=" & [Windoze] & "'")
=[Forms]![Training_Admin]![Windows ID]
=DLookUp("[Area Of Work]","[EngTrainForm]","'[Windows ID]=" & [Windoze] & "'")
=DLookUp("[ASM Email]","[EngTrainForm]","'[Area]=" & [Area] & "'")
=DLookUp("[OutlookMSG]![Qualification]","[OutlookMSG]","' [EngTrain]! [Training Date Booked] =" & [EngDate] & "'")
添加儘可能多的信息,我可以當我嘗試逐步執行olAppointmentItem =空,&整個代碼暫停在.requiredattendees = EmailAddy.Value造就了運行時錯誤424,所需的對象。
但是,如果我接下來添加一個錯誤恢復,並且它已經通過代碼運行,我會收到一封電子郵件,其中重要性用作正文詳細信息(接受質量電子郵件)。
監視列表上的.requiredattendees = EmailAddy.Value表示表達式未在上下文中定義,而上下文爲OutlookCalander,Outlook。
你可以在代碼中添加聲明和賦值EmailAddy和ASMail對象的代碼部分嗎? (並且,我想,QualifiedEmail,STdate,StTime,Edate和Location對象。) – YowE3K
請閱讀此:[調試VBA代碼](http://www.cpearson.com /excel/DebuggingVBA.aspx) - 遍歷代碼,檢查變量。 – Andre
我強烈建議在每個模塊的頂部放置['Option Explicit'](https://msdn.microsoft.com/en-us/library/bw9t3484%28v=vs.84%29.aspx)。 它在編譯時執行變量聲明並報告未聲明或拼寫錯誤的變量/常量。 要在新模塊中自動執行此操作,請在VBA編輯器中設置[需要變量聲明](http://www.fmsinc.com/microsoftaccess/modules/options/index.html)選項。 這對VBA開發來說確實是必須的。 – Andre