#1
|
|||
|
|||
.RTFBody property for a mail item is returning error
Hi,
I currently have a macro that creates personalised Emails i.e. Dear John, and then sends them out to a recipient list which it reads from a CSV file (it does other stuff as well but this is the lead into my problem). I want the Email body to come up in a specific format with a company logo, I have created the Email body with word and saved it as both HTML and RTF format, the HTML format does not display correctly as all of the logo's etc are saved as separate files on my PC and are not sent with the mail so I thought I would try it with RTF as all of the data is saved in a single file. I have created the below macro to try and test just sending the RTF but it comes back with "Run-time error Method 'RTFBody of object _MailItem' failed". I have tried converting it but the array it is stored in is already a string so it should be the right type. Any ideas? Code:
Sub RTF_email() Dim EmailHTMLFile As Integer Dim EmailHTMLFilePath As String Dim newFileContents As String Dim OutApp As Object Dim OutMail As Object EmailHTMLFilePath = "D:\Stuff\Email template.rtf" EmailHTMLFile = FreeFile() Open EmailHTMLFilePath For Input As #EmailHTMLFile 'Reading the Email contents into an array which e are going to use and manipulate from here newFileContents = Input$(LOF(EmailHTMLFile), #EmailHTMLFile) Close #EmailHTMLFile Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) ' Change the mail address and subject in the macro before you run this procedure. 'On Error Resume Next With OutMail .to = "mlw@sierrawireless.com" .CC = "" .BCC = "" .Subject = "Test Email" .BodyFormat = 3 .RTFBody = newFileContents .Display End With End Sub Matt |
#2
|
||||
|
||||
Stick with HTML format as that is widely supported. There are issues with creating Outlook objects from other Office applications and kindly fellow Microsoft MVP Ben Clothier has produced an excellent function that determines if Outlook is available and opens it properly. If you don't use that function, then be prepared to manually start Outlook and Get that running application instead of creating a new one. I have assumed that Outlook is present, so have not included error trapping against that omission.
HTML format and Word formats are different from one another, but you should be able to get images from the document body into the message body using the following code to create the message. Here the source is the activedocument open in Word. If you are using some other document then open it and set it as the oSource document. Code:
Option Explicit 'Ben Clothier - http://www.rondebruin.nl/win/s1/outlook/openclose.htm #Const LateBind = True Const olMinimized As Long = 1 Const olMaximized As Long = 2 Const olFolderCalendar As Long = 9 Const olFolderContacts As Long = 10 Const olFolderDrafts As Long = 16 Const olFolderInbox As Long = 6 Const olFolderOutbox = 4 Const olFolderSentMail = 5 Const olFolderTasks = 13 #If LateBind Then Public Function OutlookApp( _ Optional WindowState As Long = olMinimized, _ Optional Folder As Long = olFolderInbox, _ Optional ReleaseIt As Boolean = False _ ) As Object Static o As Object #Else Public Function OutlookApp( _ Optional WindowState As Outlook.OlWindowState = olMinimized, _ Optional Folder As Long = olFolderInbox, _ Optional ReleaseIt As Boolean _ ) As Outlook.Application Static o As Outlook.Application #End If On Error GoTo ErrHandler Select Case True Case o Is Nothing, Len(o.Name) = 0 Set o = GetObject(, "Outlook.Application") If o.Explorers.Count = 0 Then InitOutlook: 'Open inbox to prevent errors with security prompts o.Session.GetDefaultFolder(Folder).Display o.ActiveExplorer.WindowState = WindowState End If Case ReleaseIt Set o = Nothing End Select Set OutlookApp = o ExitProc: Exit Function ErrHandler: Select Case Err.Number Case -2147352567 'User cancelled setup, silently exit Set o = Nothing Case 429, 462 Set o = GetOutlookApp() If o Is Nothing Then Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed." Else Resume InitOutlook End If Case Else MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error" End Select Resume ExitProc Resume End Function #If LateBind Then Private Function GetOutlookApp() As Object #Else Private Function GetOutlookApp() As Outlook.Application #End If On Error GoTo ErrHandler Set GetOutlookApp = CreateObject("Outlook.Application") ExitProc: Exit Function ErrHandler: Select Case Err.Number Case Else 'Do not raise any errors Set GetOutlookApp = Nothing End Select Resume ExitProc Resume End Function Sub CreateMessage() Dim OutApp As Object Dim olInsp As Object Dim outMail As Object Dim wdDoc As Document Dim oSource As Document Dim oRng As Range Set oSource = ActiveDocument 'the document with the message format oSource.Range.Copy Set OutApp = OutlookApp() Set outMail = OutApp.createitem(0) With outMail .to = "mlw@sierrawireless.com" .Subject = "Test Email" .BodyFormat = 2 Set olInsp = .GetInspector Set wdDoc = olInsp.WordEditor Set oRng = wdDoc.Range oRng.PasteAndFormat (wdFormatOriginalFormatting) .Display End With lbl_Exit: Set OutApp = Nothing Set olInsp = Nothing Set oSource = Nothing Set wdDoc = Nothing Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Gmayor,
Thanks for the response. When I do this I do manually have Outlook open so this is not a problem, so to date I have been using the above macro but with HTMLBody rather than RTFBody and specifying the basic htm file (rather than the RTF one). Given this in my code how would I point to the graphic so that it is included, I cannot figure it out from your code. FYI I am running the overall macro with a different word document as the current item as this is being watermarked, saved as a PDF and then attached to the Email which the HTML document is then being used as the body of the mail (and I change the addressee within this HTML doc to personalise it). Regards Matt |
#4
|
||||
|
||||
Outlook may be open but your code creates another Outlook application
Code:
Set OutApp = CreateObject("Outlook.Application") You can't use an existing watermark in your message body as watermarks are set in the header footer story range and e-mails do not support header/footers. Create you message as a Word document. The macro will copy the complete body of the active document, including any graphics anchored to the body story range to the message. If your graphics are anchored to the header/footer range, bring them out to the document body and then the macro can copy them.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Gmayor,
Sorry maybe I wasn't clear, the watermarked document is the attachment which is saved as a PDF and from which I run the macro. The Email body (separate file(s) on the drive) just needs to have a company logo and the addressee changed, so the key bit I need is how to include the company logo in the Email body. Regards Matt |
#6
|
||||
|
||||
Read the last sentence of my last messaage again.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
Sorry for the delay, I have been distracted, thanks for the posting I almost have it working now.
Regards Matt |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copying data related to one item to worksheet with many instances of the same item | nmp13 | Excel | 3 | 02-06-2016 02:13 AM |
COUNTIFS returning Value Error | ubns | Excel | 1 | 04-16-2015 02:00 PM |
Returning a specific value when item is selected from a drop-down list | J Press | Excel | 4 | 09-10-2012 06:12 AM |
oulook 2003 error - this item cannot be displayed in the regading pane. | GoneFusion | Outlook | 1 | 09-22-2010 09:45 AM |
MYOB and "Item.Send error" Outlook message | peter_lambros | Outlook | 1 | 12-06-2008 08:24 AM |