2013-03-12 126 views
0

我必須創建電子郵件並將它們保存在Outlook(2007)草稿文件夾中。我使用Perl(ActivePerl 5.12.3)和Win32 :: OLE模塊。如果Outlook已經打開,它可以正常工作。 Elsif我實例化Outlook,第一封電子郵件保存在收件箱中,剩下的放在草稿文件夾中。以下說明了這個問題。保存在收件箱而不是草稿箱中的新Outlook MailItem

use strict; 
use Win32::OLE; 
use Win32::OLE::Const 'Microsoft Outlook'; 

my $oMailer; 
# Connect to a running version of Outlook 
eval { $oMailer = 
    Win32::OLE->GetActiveObject('Outlook.Application') 
}; 
die "Outlook not installed" if [email protected]; 

# Start up Outlook if not running 
unless(defined $oMailer) { 
    $oMailer = new Win32::OLE('Outlook.Application', sub {$_[0]->Quit;}) 
    or die "Unable to start an Outlook instance: $!\n"; 
} 

for (my $i=1; $i <5; $i++) { 
    my $oEmail = $oMailer->CreateItem(0) or 
    die "Unable to create mail item: $!\n"; 

    $oEmail->{'To'} = '[email protected]'; 
    $oEmail->{'Subject'} = "This is test #$i"; 
    $oEmail->{BodyFormat} = olFormatHTML; 
    $oEmail->{HTMLBody} = '<html></html>'; 

    $oEmail->save(); 
} 

的M $ dox on MailItem.Save說:

保存在Microsoft Outlook項目到當前文件夾或者,如果這是一個新項目,爲項目類型Outlook默認文件夾。

我一直無法找到任何其他在我的搜索努力的報告。任何想法如何讓它按照記錄工作?

回答

0

這表示您從未登錄過MAPI會話。

立即創建Outlook.Application對象的實例後,檢索Namespace對象(Application.GetNamespace(「MAPI」),然後調用Namespace.Logon

+0

這工作!代碼的Perl的版本(未來搜索者)是:my $ namespace = oMailer-> GetNamespace('MAPI'); $ namespace-> Logon(); – EricM 2013-03-12 19:09:40

相關問題