我正在使用outlook 2010。
当我收到电子邮件时,我需要执行以下操作:
根据我的ODBC数据库检查发件人的电子邮件地址:
一个。如果找到,则将其保存为特定文件夹中的.htm并写入" 1"在表格的数据库中
湾如果找到两次,则将电子邮件转发至admin@mydomain.org,主题为:"复制以手动排序"
C。如果没有找到,请将其转发到admin@mydomain.org,主题为:"要添加到数据库的电子邮件"
我现在就在这里:
Public Sub ShowMessage(Item As Outlook.MailItem)
'connect to database
On Error Resume Next
Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As ADODB.Command
Dim sSQL As String
Dim strConn As String
Set cnn = New ADODB.Connection
strConn = "DSN=mydsn;DATABASE=mydb;Trusted_Connection=yes;"
cnn.Open strConn
' Set cmd = New ADODB.Command
' cmd.ActiveConnection = cnn
sSQL = "SELECT id FROM dbo.mailing_list WHERE email_add = '" & Item.SenderEmailAddress & "'"
' cmd.CommandText = sSQL
rst.Open sSQL, cnn, adOpenStatic
email_add = rst!email_add
If rst.BOF And rst.EOF Then
' nothing found
Stop
ElseIf rst.RecordCount > 1 Then
' more than one record
Stop
Else
' one record
'
Dim Path As String
Path = "C:\"
End If
Dim strFromEmail As String
'name and format I want the email to be saved
Item.SaveAs Path, olMHTML
'if email is saved send an alert msg and if not send another one
If Item.Saved = True Then
Item.Delete
Else
MsgBox ("This email was not saved.")
End If
ex:
Set cnn = Nothing
Set cmd = Nothing
Set rst = Nothing
Set rst1 = Nothing
End Sub
答案 0 :(得分:0)
考虑实施以下方案:
VBA sub应如下所示:
Public Sub HandleNewMail(mail As MailItem)
' do something
End Sub
Outlook对象模型为项目提供Forward方法。它为项执行Forward操作,并将生成的副本作为MailItem对象返回。 SaveAs方法将Microsoft Outlook项目保存到指定的路径,并采用指定文件类型的格式。如果未指定文件类型,则使用MSG格式(.msg)。传递第二个参数的 olHTML 值。
由您决定选择哪种方式。但我建议从MSDN中的Getting Started with VBA in Outlook 2010文章开始。
答案 1 :(得分:0)
对于SQL部分我会使用一个单独的模块;我认为以下应该有效。使用像
这样的东西Sub whatsoever()
Dim countsql As Integer
countsql = countemailadress("rechnung.wien@bechtle.com")
Debug.Print "COUNT = " & countsql
End Sub
调用它,然后你在“countql”中得到了答案,并且可以使用例如在一个“案件”。
这里的功能:
Function countemailadress(searchforemail As String)
Dim mysql As String
Dim con As ADODB.connection
Dim rec As ADODB.recordset
Set con = New ADODB.connection
con.Open "DSN=mydsn;DATABASE=mydb;Trusted_Connection=yes;"
mysql = "SELECT Count(mailing_list.[email_add]) AS countemail_add FROM mailing_list where (mailing_list.[email_add]=" & searchforemail & ");"
Set rec = con.Execute(mysql)
rec.movefirst
countemailadress = rec.Fields(0)
End Function
我希望这有帮助, 最大