1
votes

The VBA code shown below is for replying to an email with a specific template. The user selects the email that they want to reply to through a file dialog box, the code should then open a new email replying to the selected email. I am receiving the following error:

Run-time error '-2147352567 (80020009)' Could not send the message.

When I click the "Help" button it takes me here: https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/olkcategory-backcolor-property-outlook

I can't imagine that link has anything to do with the error though. I have a feeling I'm doing something wrong with adding the reply email. Any help would be appreciated.

Here is my code:

Public Sub btnOK_Click()

Dim myOlApp As Outlook.Application
Dim myItem As Outlook.MailItem
Dim body As String
Set myOlApp = CreateObject("Outlook.Application")

Select Case templateUserForm.templateListBox.Text
 Case "CWO"
    ReplyTo
    Set myItem = myOlApp.CreateItemFromTemplate("C:\example\temp\tempEmail.msg")
    myItem.Reply = myOlApp.CreateItemFromTemplate("L:\example\CWO.oft") <--- Error occurs on this line
    body = myItem.body
    Unload templateUserForm
    Unload inputUserForm
    myItem.Display
End Select
End Sub

Public Sub ReplyTo()

Dim fso As Object
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Dim fd As Office.FileDialog
Set fd = xlApp.Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
Dim selectedItem As Variant

If fd.Show = -1 Then
For Each selectedItem In fd.SelectedItems
    Set fso = VBA.CreateObject("Scripting.FileSystemObject")
    Call fso.CopyFile(fd.SelectedItems.Item(1), "C:\example\temp\tempEmail.msg")
Next
End If
End Sub
1

1 Answers

0
votes

Typically you would create a reply object like this:

Set myReply = myItem.Reply

To start with an oft:

Dim myReply as Outlook.MailItem
Set myReply = myOlApp.CreateItemFromTemplate("L:\example\CWO.oft")
myReply.Display

Now that there is no error you have myReply to manipulate.

It appears you want this.

Set myItem = myOlApp.CreateItemFromTemplate("C:\example\temp\tempEmail.msg")
myReply = myOlApp.CreateItemFromTemplate("L:\example\CWO.oft") 
myReply.body = myItem.body
Unload templateUserForm
Unload inputUserForm
myReply.Display

To make the result look more like a reply:

myReply.body = myReply.body  & vbcr & myItem.reply.body

Depending on your templates you may want HTMLbody rather than body.