用vba excel打开outlook .msg文件

嗨,大家好,我有以下代码

Sub Kenneth_Li() Dim objOL As Outlook.Application Dim Msg As Outlook.MailItem Set objOL = CreateObject("Outlook.Application") inPath = "C:\Users\SiliconPlus\Desktop\Si+ Contact Lists\Contact_Si+" thisFile = Dir(inPath & "\*.msg") Do While thisFile <> "" 'Set Msg = objOL.CreateItemFromTemplate(thisFile) 'Or Set Msg = objOL.OpenSharedItem(thisFile) Msg.display MsgBox Msg.Subject thisFile = Dir Loop Set objOL = Nothing Set Msg = Nothing End Sub 

每当我使用OpenSharedItem它会给出一个运行时错误438对象不支持此属性或方法。

而且每当我使用CreateItemFromTemplate我得到以下错误:

 Cannot open file: AUTO Andy Low Yong Cheng is out of the office (returning 22 09 2014).msg. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click properties to check your permissions for the folder. 

我不是100%的代码,但试试这个:

 Sub LiminalMsgbx() Dim outappp, outmaill As Object Dim pthh As String pthh = "C:\DeskTop\MyTemplate.oft" Set outappp = CreateObject ("Outlook.Application") Set outmaill = outapp.CreateItemFromTemplate(pthh) With outmaill .display End With Set outappp = Nothing Set outmaill = Nothing End Sub 

您也可以使用.send而不是.display

OpenSharedItem方法由Namespace对象公开,而不是Application。

 Set objOL = CreateObject("Outlook.Application") set objNs = objOL.GetNamespace("MAPI") objNs.Logon ... Set Msg = objNs .OpenSharedItem(thisFile) 

至于第二个错误,这是相当明确的 – 该文件无法find。 您必须提供文件夹path的标准文件名。 您只提供文件名称。