Replying to an e-mail with boilerplate text and an attachment

  • Thread starter Thread starter peeweejd
  • Start date Start date
P

peeweejd

Hi,

I'm decent at programming VBA in Excel and Access, but I have no
experience at all with Outlook. I'm using Outlook 2000 right now.

I want to easily be able to reply to the message that is open with
some text ("thank you mr. customer sir we love you, bah, blah, blah" )
and an attachment (a pdf copy of my companies terms and conditions).

I tried it already by creating a template that I copy/paste but it
gets hung up when the message format is HTML or text.

Can someone walk me through this or point to an example? Is VB the
right way to do this?

Thanks in advance!
 
One way:

First set up a signature with the boilerplate text. Then paste this
code in a standard module in the Outlook VBE.

(Note: I did not write this code, it was adapted from outlookcode.com
and rondebruin.nl)

Sub SendBoilerReply()
Call SendStockMsg("Signature name", "Filename/path")
End Sub

Replace "Signature name" with the name of the signature you created,
and replace "Filename/path" with the fully qualified path and filename
of the file you want to attach. For example, if your signature was
named "Boilerplate Client Response" and you were attaching "C:
\ThankYou.PDF", the code would look like this.

Option Explicit
Sub SendBoilerReply()
Call SendStockMsg("Boilerplate Client Response", "C:
\ThankYou.PDF")
End Sub

Create as many signatures as you need, just copy and paste the above
code and change the references accordingly. If you attach the macro to
a toolbar button, it will be even more user friendly (see "How to
assign a macro to a toolbar button" at http://codeforexcelandoutlook.com/ResendMsg.html
if you need placement assistance).

Then paste in this code in the same module:

Sub SendStockMsg(SigName As String, Optional sFile As String)

Dim myItem As Outlook.MailItem
Dim olNewMailItem As Outlook.MailItem
Dim SigString As String
Dim Signature As String

' get valid ref to current item
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set myItem = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set myItem = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0

If myItem Is Nothing Then
MsgBox "Could not use current item. Please select or open a
single email.", vbInformation
GoTo ExitProc
End If

SigString = "C:\Documents and Settings\" & Environ("username") & _
"\Application Data\Microsoft\Signatures\" & SigName &
".htm"

If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If

Set olNewMailItem = myItem.Reply

olNewMailItem.HTMLBody = Signature & olNewMailItem.HTMLBody

If sFile <> "" Then
olNewMailItem.Attachments.Add(sFile)
End If

olNewMailItem.Display

myItem.UnRead = False

ExitProc:

Set olNewMailItem = Nothing

End Sub

Function GetBoiler(ByVal sFile As String) As String
' from Dick Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function


The filename argument is optional, so if you had a stock reply you
wanted to send, but didn't want to include an attachment, this code
would work:

Sub SendBoilerReply()
Call SendStockMsg("Signature name")
End Sub

This code will work if you select one item when viewing your mailbox,
or on the currently open mail item. Note that it looks for the
signature file in this folder:

C:\Documents and Settings\"username"\Application Data\Microsoft
\Signatures\

If your folder is different, change the path accordingly. (search for
"signature name".htm to find the correct folder)


ps- your stock reply sounds a bit like the "bug letter" urban legend

:-)

http://www.snopes.com/business/consumer/bedbug.asp


HTH,
JP
 
One way:

First set up asignaturewith the boilerplate text. Then paste this
code in a standard module in the Outlook VBE.

Thanks for the help. I tried it, but all of the previous text is
wiped out by the signature.
ps- your stockreplysounds a bit like the "bug letter" urban legend

yeah, I didn't think that I should bore you all with my schmoozing ;-)

Thanks for the help. I may just use the "signature" trick. It's
really too bad that you cannot add an attachment to a signature file.
 
What do you mean "previous text"? You mean the text of the original
message? In my testing the code replies to the message then inserts
the boilerplate text at the top, exactly as if you had done so
manually. The original text of the message is preserved at the bottom
of the stock reply.

That is what this line does:

olNewMailItem.HTMLBody = Signature & olNewMailItem.HTMLBody

It sets the HTMLBody property of the new message to the text of the
signature (the boilerplate text) then appends the original text of the
reply (as if you had pressed "Reply").

You don't need to add the attachment to the signature. As I mentioned
in the previous message, you can specify an attachment in the call to
the function. This makes it more flexible in that you can have
different boilerplates and attachments for different situations.


HTH,
JP
 
That statement won't work as you expect it to, because it puts the signature *outside* the <html><body> and </body></html> tags when instead it needs to go inside -- *and* be completely tagged HTML content. In other words, it takes a bit of text parsing with the Instr(), Mid(), and Left() functions.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


What do you mean "previous text"? You mean the text of the original
message? In my testing the code replies to the message then inserts
the boilerplate text at the top, exactly as if you had done so
manually. The original text of the message is preserved at the bottom
of the stock reply.

That is what this line does:

olNewMailItem.HTMLBody = Signature & olNewMailItem.HTMLBody

It sets the HTMLBody property of the new message to the text of the
signature (the boilerplate text) then appends the original text of the
reply (as if you had pressed "Reply").

You don't need to add the attachment to the signature. As I mentioned
in the previous message, you can specify an attachment in the call to
the function. This makes it more flexible in that you can have
different boilerplates and attachments for different situations.


HTH,
JP
 
Back
Top