2017-05-26 101 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 

还没有工作却又是位置,所以我需要寻找多一点到这一点,但它的大部分工作现在唯一的事情。