PDA

View Full Version : Outlook button



largeinnit
07-31-2007, 06:19 AM
Hi,

My dept receives a lot of emails about the same thing. We give standard responses for the same query by several users - what I'd like to do is to setup a button in outlook so that when it's clicked it brings up an email reply box with the subject line filled out and the text set, so all I need to do is fill in the recipients name.

any ideas on how to do this much appreciated.

thanks

Dan

mvidas
07-31-2007, 07:31 AM
Hi Dan,

Welcome to VBAX :)

The easiest way would be to use a template. Create a new mail message and type out the subject and body as you want it to be. Once it is all set, go to File / Save As, change the Save As Type to Outlook Template, and save it to a hard drive (be sure to note where it is saved -- the default location is C:\Documents and Settings\yourusername\Application Data\Microsoft\Templates).

Then in outlook, right-click on the toolbar, and go to Customize. Select Categories: File, and click/drag Commands: Mail Message to where you want your button to be. Right-click on the new button, change the name/image/etc as you desire, then go to Assign Hyperlink -> Open.... Where it says "Type the file or Web page name:", enter the location of your template (or browse for it with the File button), click OK, and you're all set!

Let me know if you have any questions

TrippyTom
08-29-2007, 09:35 AM
Hey Matt,
I have come upon the same need and was wondering.... This brings up a new email window. Is there any way to get it to work with just REPLYING to an original message?

If no, is there a way to make simple "Boiler Plate" responses and attach them to macros that just insert a certain text string in any message you have open?

mvidas
08-29-2007, 10:17 AM
Hi Tom,

(just about) Anything is possible with VBA, just depends on how hard you want to work to get it. By the way--putting your specs in your signature is a great idea. Unfortunately I've only got OL2000 where I am here, I think that OL2003 would make this a little more straightforward. Still works though.

First, I'll show you how to do your second question, as I can answer it quicker, I'll reply after this with how you can catch a "reply" event -- I'll have to re-figure out how though :)
Sub InsertTextIntoCurrentOpenMessage()
If ActiveInspector Is Nothing Then Exit Sub
If TypeName(ActiveInspector.CurrentItem) <> "MailItem" Then Exit Sub
With ActiveInspector.CurrentItem
If Len(.HTMLBody) > 0 Then
.HTMLBody = "Hello world" & "<br>" & .HTMLBody
Else
.Body = "Hello world" & vbCrLf & .Body
End If
End With
End SubNote that the above code will put your text at the top of the message and not at the cursor. If you want that, the easiest way I can think of at this moment is to use macros to put a specific message into the clipboard, and you can just paste from there. There may be another way, let me know if you're interested and I can try a couple things out.

The above sub can be changed to be a function for greater flexibility. For example:Sub ReplyWithSoldOut()
InsertTextIntoCurrentOpenMessage "Sorry, we're sold out."
End Sub
Sub ReplyWithSure()
InsertTextIntoCurrentOpenMessage "Sure! I'll get right to it."
End Sub
Sub ReplyWithOKAndErrorChecking()
If InsertTextIntoCurrentOpenMessage("Ok.") = False Then
MsgBox "An error occurred, please verify you have started a reply"
End If
End Sub
Function InsertTextIntoCurrentOpenMessage(ByVal MessageText As String) As Boolean
If ActiveInspector Is Nothing Then Exit Function
If TypeName(ActiveInspector.CurrentItem) <> "MailItem" Then Exit Function
With ActiveInspector.CurrentItem
If Len(.HTMLBody) > 0 Then
.HTMLBody = MessageText & "<br>" & .HTMLBody
Else
.Body = MessageText & vbCrLf & .Body
End If
End With
InsertTextIntoCurrentOpenMessage = True
End FunctionThen you can either call the ReplyWithSoldOut sub or ReplyWithSure to reply with two canned responses, both using the same function. I added a third, ReplyWithOKAndErrorChecking to show how you can check to make sure the text was added to the message.

I'll play around with the events again and see what I can figure out about hooking into the normal Reply (and ReplyAll, they're two separate events IIRC)

mvidas
08-29-2007, 10:54 AM
Ok, got it. It does involve a bit more code, but pretty cool nonetheless.

First off, go to your outlook vba, and then go to Insert, then Class Module. In the properties of the class module, change the (Name) to "vMailEvents", no quotes.

In the code pane for the class module, insert the following:Option Explicit
Dim WithEvents vInspectors As Outlook.Inspectors
Dim WithEvents vExplorers As Outlook.Explorers
Dim WithEvents vExplorer As Outlook.Explorer
Dim WithEvents vMailItem As Outlook.MailItem

Private Sub Class_Initialize()
Set vInspectors = Application.Inspectors
Set vExplorers = Application.Explorers
If Not ActiveExplorer Is Nothing Then
Set vExplorer = ActiveExplorer
If TypeName(vExplorer.Selection.Item(1)) = "MailItem" Then
Set vMailItem = vExplorer.Selection.Item(1)
End If
End If
End Sub
Private Sub Class_Terminate()
Set vInspectors = Nothing
Set vExplorer = Nothing
Set vExplorers = Nothing
Set vMailItem = Nothing
End Sub

Private Sub vExplorer_SelectionChange()
If vExplorer.Selection.Count = 1 And TypeName(vExplorer.Selection.Item(1)) = _
"MailItem" Then Set vMailItem = vExplorer.Selection.Item(1)
End Sub
Private Sub vExplorers_NewExplorer(ByVal Explorer As Explorer)
Set vExplorer = Explorer
If TypeName(vExplorer.Selection.Item(1)) = "MailItem" Then
Set vMailItem = vExplorer.Selection.Item(1)
End If
End Sub
Private Sub vInspectors_NewInspector(ByVal Inspector As Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
Set vMailItem = Inspector.CurrentItem
End If
End Sub

Private Sub vMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
With Application.CreateItemFromTemplate("C:\template.oft")
.To = Response.To
.CC = Response.CC
.BCC = Response.BCC
.Subject = Response.Subject
Cancel = True
.Display
End With
End Sub

Private Sub vMailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
With Application.CreateItemFromTemplate("C:\template.oft")
.To = Response.To
.CC = Response.CC
.BCC = Response.BCC
.Subject = Response.Subject
Cancel = True
.Display
End With
End Sub


Next, double click on the ThisOutlookSession object, and paste in the following:Option Explicit
Dim vMailEventsVar As vMailEvents

Private Sub Application_Startup()
Set vMailEventsVar = New vMailEvents
End Sub

Private Sub Application_Quit()
Set vMailEventsVar = Nothing
End SubIf you already use a _Startup or _Quit event, simply add the above lines into your existing code.

Now, when you click Reply or Reply All, the template "C:\template.oft" is opened instead. The "C:\template.oft" is specified in both the vMailItem_Reply and _ReplyAll events within the class module, change as needed.

:)

TrippyTom
08-29-2007, 11:19 AM
Holy speedy gonzalez batman! :bow: That's a lot of info to process. I think just using the copy/paste method will work for me, but thanks for providing options.

I will have to look at this further and try to understand it as I'm not familiar with the Outlook vba model at all. I only just recently figured out how to make the auto-load macros to work every time I open outlook. (for some reason, I had to create my own signature file for my outlook macros to work each session)

mvidas
08-29-2007, 11:37 AM
Hehehe.. as I said, nearly anything is possible using vba, just depends how much work you want to put into it. Though there is no time like the present to dive into the outlook model (my new best friend for the past few months). My first post shows some basic information, the second is a bit more advanced as it creates a second class, but gives a good example on how you can control the events of an explorer object (what many people consider the application--its what you'd see on your task bar), an inspector object (what you see if you are replying to a message or open it up in it's own window), as well as a mailitem's events. You may want to follow the instructions from my second post anyways, and just comment out the code for ThisOutlookSession, so you can use it as a future reference on how you could accomplish something like this in the future.

And of course as always, you could just ask if you need something in the future too :)

mvidas
08-29-2007, 11:38 AM
I only just recently figured out how to make the auto-load macros to work every time I open outlook. (for some reason, I had to create my own signature file for my outlook macros to work each session)

You must have your macro security set to high -- if it had been on medium then you wouldn't need a digital signature, though you'd have to click Enable Macros everytime you opened outlook. Having a selfcert is a good idea anyways, makes things easier IMO

TrippyTom
08-29-2007, 01:21 PM
Yes, the company I work for is paranoid of email viruses, so they don't want me to reduce the security settings. I inserted the code from your 2nd post.

I have another question. Would it be possible to use a Select/Case statement or something to allow for a list of boiler text replies?

For instance, here are some simple responses I want to use:


Chart Edits:
Your chart request is attached.

Logo Search:
Your requested logos are attached.

Excel:
Your Excel file is attached.

PowerPoint:
Your PowerPoint file is attached.

TrippyTom
08-29-2007, 01:31 PM
Also, for some reason it's still erasing the original message and only showing what's in the .oft file I specify. Is there a way to fix that? I would like to preserve the original message.

TrippyTom
08-29-2007, 05:10 PM
Actually, your first example works great for me. I setup all my responses and everything, then put the separate macros on my own toolbar. But I can't seem to figure out how to put that toolbar INSIDE the reply window.

It's currently in the main Email window, and the macro works if I go to the main window and click the button, but it's kind of awkward. Is there an easy way to copy a custom toolbar to the reply window?

mvidas
08-30-2007, 08:01 AM
I dont know of an easy way to copy a custom toolbar, you may have to just re-create it for an inspector window. Most any customized toolbars/commandbars I use are created programmatically, so I can just change an explorer object to an inspector object if I want to do it that way.

Depending on how much you want to get into it, you could always create a custom reply form with a dropdown containing your canned responses :) I think just opening a new reply and adding a custom toolbar would be the easiest way though

TrippyTom
08-30-2007, 09:02 AM
Ok, thanks Matt. Because I'm new to the object library I didn't even know it was called an inspector window. :) I remade the toolbar and it fits my needs now.

Just a couple random questions:
1) How can I remove the HTML formatting so the text assumes my default font? I normally have messages in RTF.

2) for learning purposes, how would I setup a toolbar via code so I can make it an addin that automatically sets up the toolbar for me? (Perhaps that could be a separate post)

mvidas
08-30-2007, 09:29 AM
1) That is a good question.. I tested it on a couple RTF messages yesterday and saw that it messed up the formatting. Not sure how to get around that... maybe the only way is to use the MSForms library's DataObject to put text in the clipboard?Sub CopyFileAttachedToClipboard()
Dim DatObj As DataObject
Set DatObj = New DataObject
DatObj.SetText "Excel file attached."
DatObj.PutInClipboard
Set DatObj = Nothing
End Sub


2) AddIns for outlook are not like they are for something like Excel, they would have to be created in VB or another programming environment, usually as a COM add-in. However you can always just call a sub like this in Application_Startup:Sub CreateToolbarButtons()
Dim cbBar As CommandBar, btNew As CommandBarButton
Dim MenuName As String, MenuCaption As String

MenuName = "My new menu"
MenuCaption = "&My new menu"

Set cbBar = ActiveExplorer.CommandBars("Menu Bar") 'could also use ActiveInspector

'add new menu before "Help" menu
With cbBar.Controls.Add(msoControlPopup, , , cbBar.Controls("Help").Index, True)
.Caption = MenuCaption
End With

With cbBar.Controls(MenuName).Controls.Add(msoControlButton, , , , True)
.OnAction = "ToggleAuto"
.BeginGroup = True
.TooltipText = "Automatic Processing"
.Caption = "Automatically Process Requests"
End With

With cbBar.Controls(MenuName).Controls.Add(msoControlButton, , , , True)
.OnAction = "ManualRun"
.TooltipText = "Manually Process New Requests"
.Caption = "Manually Process Requests"
End With

With cbBar.Controls(MenuName).Controls.Add(msoControlButton, , , , True)
.OnAction = "ChangeExportFilename"
.TooltipText = "Change file to export requests"
.Caption = "Change exported requests file"
End With

With cbBar.Controls(MenuName).Controls.Add(msoControlButton, , , , True)
.OnAction = "ChangeRequestsPath"
.TooltipText = "Change folder to look for requests in"
.Caption = "Change requests folder"
End With
End SubThats just a stripped version of one I use in an add-in, but should give you the base code you need (The "True" in there is for the "Temporary" argument -- something that helps in case you happen to delete/rename code :))