2017-05-26 138 views
1

我目前有一個項目即將完成,但我陷入了最後的障礙。使用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。

+0

你可以在代碼中添加聲明和賦值EmailAddy和ASMail對象的代碼部分嗎? (並且,我想,QualifiedEmail,STdate,StTime,Edate和Location對象。) – YowE3K

+0

請閱讀此:[調試VBA代碼](http://www.cpearson.com /excel/DebuggingVBA.aspx) - 遍歷代碼,檢查變量。 – Andre

+0

我強烈建議在每個模塊的頂部放置['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

回答

0

經過更多的試驗和錯誤之後,通過代碼,我發現它比Excel更適合在Access中聲明。如果有人想要更多的信息在這裏是下面的代碼:

Sub Outlook() 
Dim obj0App As Object 
Dim objAppt As Object 
Dim EmailAddy As Object 
Dim ASMail As Object 
Dim QualificationEmail As Object 
Dim STdate As Object 
Dim StTime As Object 
Dim Edate As Object 
Dim Location As Object 



Set obj0App = CreateObject("outlook.Application") 
Set objAppt = obj0App.CreateItem(1) 'olAppointmentItem 


With objAppt 

.requiredattendees = Forms("EngTraining").EmailAddy.Value 
.optionalattendees = Forms("EngTraining").ASMail.Value 
.subject = "Training booked for " & " " &  Forms("EngTraining").QualificationEmail.Value 
.Importance = 2 'high 
.Start = Forms("EngTraining").STdate.Value & " " &  Forms("EngTraining").StTime.Value 
.End = Forms("Engtraining").EngTrainsubform.EndDate.Value 
'.Location = Location.Value 
.ReminderMinutesBeforeStart = 20160 'reminder set for two weeks before  the event 
.Body = "Training for" & " " &  Forms("EngTraining").QualificationEmail.Value  & "." & 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 

還沒有工作卻又是位置,所以我需要尋找多一點到這一點,但它的大部分工作現在唯一的事情。