2013-07-31 85 views
0

我在看這個代碼片段從這裏另一個問題(MS訪問VBA):https://stackoverflow.com/a/17975507/1085885通過MS訪問VBA/Outlook中發送電子郵件,選擇發送模式

眼下這個代碼只能當我運行它,而展望開了。有沒有辦法讓這個代碼「打開Outlook」,然後運行所有的發送代碼?

其次,我該如何選擇從哪個Outlook配置文件發送?我可以訪問兩個不同的配置文件,它從我的主要頂級收件箱發送,但我希望它來自我的第二個收件箱。

+1

的可能重複。[MS訪問VBA:發送電子郵件通過Outlook(http://stackoverflow.com/questions/ 17973549/ms-access-vba-sending-an-email-through-outlook) – Elias

+1

這不是重複的;它是一個followu使用不同範圍的問題 –

回答

0

您需要登錄到指定的配置文件。創建Outlook應用程序

設置oApp =的CreateObject( 「Outlook.application」)的一個實例後

添加類似以下內容:

set oNS = oApp.GetNamespace.Logon 
oNS.Logon("MyProfileName") 

注意如果Outlook已經在運行,登錄會做沒有。您需要使用Extended MAPI(C++或Delphi或MAPI包裝器,如RedemptionRDOSession.Logon))才能登錄到指定的配置文件。

爲什麼不使用單個配置文件並創建多個accpunts?然後可以設置MailItem.SendUsaingAccount屬性來指定一個特定的帳戶

+0

如何使用SendUsingAccount屬性?我確實創建了MailItem(請參閱代碼) –

0

嘗試這種方式

Private Sub Command1_Click() 

Dim bStarted As Boolean 
Dim oOutlookApp As Outlook.Application 
Dim oItem As Outlook.MailItem 

On Error Resume Next 


'Get Outlook if it's running 
Set oOutlookApp = GetObject(, "Outlook.Application") 
If Err <> 0 Then 
    'Outlook wasn't running, start it from code 
    Set oOutlookApp = CreateObject("Outlook.Application") 
    bStarted = True 
End If 

'Create a new mailitem 
Set oItem = oOutlookApp.CreateItem(olMailItem) 

With oItem 
    'Set the recipient for the new email 
    .To = "[email protected]" 

    .Send 
End With 

If bStarted Then 
' 'If we started Outlook from code, then close it 
    oOutlookApp.Quit 
End If 

'Clean up 
Set oItem = Nothing 
Set oOutlookApp = Nothing 



End Sub