我正在从模板(oft文件)创建Outlook 2010消息,并替换Subject,Body和To属性中的部分文本。
当我读取Body时,出现运行时错误287。
Sub test()
Dim olApp As Outlook.Application
Dim olItem As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olItem = olApp.CreateItemFromTemplate("C:\Users\Public\JobsTracker.V01\Templates\Template-1.oft")
olItem.Subject = Replace(olItem.Subject, "sometext", "othertext") 'This works
olItem.Body = Replace(olItem.Body, "sometext", "othertext") 'This raises Runtime Error 287
olItem.Display
End Sub
答案 0 :(得分:0)
尝试移动olItem.Display
Sub test()
Dim olApp As Outlook.Application
Dim olItem As Outlook.MailItem
Set olApp = CreateObject("Outlook.Application")
Set olItem = olApp.CreateItemFromTemplate("C:\Users\Public\JobsTracker.V01\Templates\Template-1.oft")
olItem.Display
olItem.Subject = Replace(olItem.Subject, "sometext", "othertext") 'This works
olItem.Body = Replace(olItem.Body, "sometext", "othertext")
End Sub
如果这不起作用。看起来代码不在Outlook中,因为您正在使用
Set olApp = CreateObject("Outlook.Application")
尝试Shell" Outlook.exe"在运行代码之前
Set olApp = GetObject(, "Outlook.Application")
您可能需要设置延迟,直到Outlook打开。
答案 1 :(得分:0)
在某些情况下,Outlook可能无法完全正常运行
Set olApp = CreateObject("Outlook.Application")
此处详细信息NameSpace.Logon Method (Outlook)
" ...首先,实例化Outlook Application对象,然后引用默认文件夹,例如收件箱。这具有初始化MAPI以使用默认配置文件和使副对象模型完全正常运行的副作用。"
Sub InitializeMAPI ()
' Start Outlook.
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
' Get a session object.
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
' Create an instance of the Inbox folder.
' If Outlook is not already running, this has the side
' effect of initializing MAPI.
Dim mailFolder As Outlook.Folder
Set mailFolder = olNs.GetDefaultFolder(olFolderInbox)
' Continue to use the object model to automate Outlook.
End Sub