2013-04-08 67 views
0

我试图做一个函数,以便它发送一封电子邮件到我的查询(UpcomingBirthday)中的一个电子邮件地址。我有一个函数中的代码,然后在宏自动执行,所以它运行时,我加载我的数据库。'Object Required'to send Email

Public Function EmailSend() 
Dim imsg As Object 
Dim iconf As Object 
Dim flds As Object 
Dim schema As String 

Set imsg = CreateObject("CDO.Message") 
Set iconf = CreateObject("CDO.Configuration") 
Set flds = iconf.Fields 


schema = "http://schemas.microsoft.com/cdo/configuration/" 
flds.Item(schema & "sendusing") = cdoSendUsingPort 
flds.Item(schema & "smtpserver") = "smtp.live.com" 
flds.Item(schema & "smtpserverport") = 25 
flds.Item(schema & "smtpauthenticate") = cdoBasic 
flds.Item(schema & "sendusername") = "[email protected]" 
flds.Item(schema & "sendpassword") = "MyPassword" 
flds.Item(schema & "smtpusessl") = False 
flds.Update 

With imsg 
    Call EmailSend(UpcomingBirthday.[Email], "[email protected]", "Birthday Promotion!", "<html>Happy Birthday! <p> Our records indicate that you're eligible for a birthday promotion.</p></html.") 
    Set .Configuration = iconf 
    .Send 
End With 

Set iconf = Nothing 
Set imsg = Nothing 
Set flds = Nothing 
End Function 

现在,当我尝试运行这段代码,它告诉我“运行时错误424 - 必选对象”,并强调这一行,当我去调试:呼叫EmailSend(UpcomingBirthday [邮件] ,“[email protected]”,“生日促销!”,等等。所以我需要做的是从我的查询'UpcomingBirthday'中'Email'列中的值,然后发送一封电子邮件给他们。

如果有人能告诉我我需要做什么来解决这个错误,那会很棒!还有,如果你只是扫描代码,看看它是否正常(如应该它工作)? !:)

+0

是你在你的代码中使用递归计划?你将函数定义为'Public Function EmailSend()',该函数中的一个语句是'Call EmailSend'。当一个过程('Sub'或'Function')在没有任何退出条件的情况下自行调用时,事情往往会“变得糟糕”。 – 2013-04-08 22:36:08

+0

如果我将'Call EmailSend'更改为'Call SendEmail',它表示* Sub或Function not defined *。所以是的,我认为递归是故意的?我认为问题出现在'即将到来的生日'内[电子邮件]' – 2013-04-08 22:41:13

回答

0

Public Function EmailSend中的Call EmailSend声明显然是有问题的。如果你想将值分配给imsg(一CDO.Message对象)的属性,那么就做类似

With imsg 
    .To = "[email protected]" 
    .From = "[email protected]" 
    .Subject = "Birthday Promotion!" 
    '' and so on 
    .Send 
End With 

此外,我们不知道什么UpcomingBirthday真的是因为它在别处定义。

你可能想保存自己的一些麻烦,只是使用SendEmail功能可在这里下载:

http://www.cpearson.com/excel/Email.aspx

+0

嗯,好吧,我用更简单的方法去改变它到你说的,但是当我说'.Body =“生日快乐等'',它给我一个错误*对象不支持这个属性或方法*。 因此,我拿出.Body部分来查看会发生什么,然后突出显示'.Send'并说:*“SendUsing”配置值无效。* – 2013-04-09 02:53:57

+0

或者如果我下载了您提供的SendEmail功能,将会我仍然能够自动将'.To'字段更新为来自UpcomingBirthday查询的电子邮件地址?谢谢。 – 2013-04-09 03:01:14

+0

@ NightSpy2当然,只要看看代码:'ToAddress'是参数列表中的第三项。 – 2013-04-09 05:49:20