Solved

Add Button in Outlook Message Window

Posted on 2006-06-24
13
562 Views
Last Modified: 2010-04-07
Hello all,

I want to write a VB application which will "Add a New Button in Outlook Message Window". Every time I open up any message this button should aapear. My outlook messages can have more than one attachment. This button, when clicked, will ask me for a path and will save all the attachement in that specified path in one go, so that I don't need to save each message seperately.

Can anybody help?

Thanks
0
Comment
Question by:Rajesh Dalmia
  • 7
  • 6
13 Comments
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 16975020
I am aware that Outlook already has Save Attachments option in the File Menu which will do the same thing. But actually this button will not only save the attachments but will also forward the email to few pre definied email addresses. Everytime I don't want to click Forward and then key in the email address.
So you if can guide me how to creat the button in message window, other things I can add in the code.

Thanks
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16975453
Hi rdonline1,

You don't need a VB application, just a bit of VBA scripting.  This is easily done, but there is a catch.  Forwarding messages is going to trigger Outlook's built-in security causing a dialog-box to pop up warning that an application is attempting to send on your behalf and asking for your permission to allow it to continue.  You can avoid this by having the code simply display the message and allowing you to click Send.  I'll post the rest of the VBA code for adding the button shortly.

Cheers!
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 16975936
rdonline1,

Here's the code for doing this.  Follow these instructions to set up and use it.

1.  Start Outlook
2.  Click Tools->Macro->Visual Basic Editor
3.  If not already expanded, expand Microsoft Outlook Objects in the Project pane, then click on ThisOutlookSession
4.  Copy the script below
5.  Paste the script into the right-hand pane of the VB Editor
6.  Edit the code.  I've included comments where changes are needed or can be made
7.  Click the diskette icon on the toolbar to save the changes
8.  Close the VB Editor
9.  Click Tools->Macro->Security
10.  Set Security Level to Medium
11.  Close Outlook
12.  Start Outlook
13.  A dialog-box will appear telling you the ThisOutlookSession contains macros and asking if you want to enable them.  Say yes.
14.  Test the macro by opening a message with an attachment and clicking the toolbar button labeled "SaveAndForward".  When you a dialog-box will pop up asking for the path to save the items to.  If you enter a valid path, then the attachments will be saved there.  If you enter an invalid path, then the attachments will not be saved.  The code will then open a new window with a forward copy of the original message that's already addressed to whomever.  All you need to do is click Send.

I wrote and tested this on Outlook 2003 and it worked as described above.


Public WithEvents myOlInspectors As Outlook.Inspectors, _
    WithEvents olkControl As Object

Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    Dim olkBar As Object
    If Inspector.CurrentItem.Class = olMail And Inspector.CurrentItem.SenderName <> "" Then
        Set olkBar = Inspector.CommandBars.Item("Standard")
        'Test to see if the toolbar button already exists
        Set olkControl = olkBar.FindControl(, , "SaveAndForward")
        'If not found, then create button
        If olkControl Is Nothing Then
            Set olkControl = olkBar.Controls.Add(, , , 4, True)
            With olkControl
                .Caption = "SaveAndForward"
                .FaceId = 1676
                .Style = msoButtonIconAndCaption
                .Tag = "SaveAndForward"
                .Visible = True
            End With
        End If
        Set olkBar = Nothing
   End If
End Sub

Private Sub olkControl_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    Dim olkMessage As Outlook.MailItem, _
        olkForward As Outlook.MailItem, _
        olkAttachment As Outlook.Attachment, _
        objFSO As Object, _
        strPath As String
    Set olkMessage = Application.ActiveInspector.CurrentItem
    If olkMessage.Attachments.Count > 0 Then
        'Change the default path on the following line as needed
        strPath = InputBox("Enter the path to save this items's attachments to.", "Save Attachments", "C:\eeTesting")
        If strPath <> "" Then
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            If objFSO.FolderExists(strPath) Then
                For Each olkAttachment In olkMessage.Attachments
                    olkAttachment.SaveAsFile strPath & "\" & FixFileName(olkAttachment.FileName)
                Next
            End If
            Set objFSO = Nothing
        End If
    End If
    Set olkForward = olkMessage.Forward
    'Change the recipient on the following line.  Add more recipients by duplicating the next line.
    olkForward.Recipients.Add "jane.doe@company.com"
    olkForward.Display
    Set objFSO = Nothing
    Set olkAttachment = Nothing
    Set olkMessage = Nothing
End Sub

Private Sub Application_Quit()
    Set myOlInspectors = Nothing
    Set olkControl = Nothing
End Sub

Private Sub Application_Startup()
    Set myOlInspectors = Application.Inspectors
End Sub

Function FixFileName(strFileName As String) As String
    FixFileName = Replace(strFileName, ":", "")
    FixFileName = Replace(FixFileName, "\", "")
End Function

0
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 16981954
The very 1st line of code

Public WithEvents myOlInspectors As Outlook.Inspectors, _
    WithEvents olkControl As Object

is giving me error.
"Compiler error:
Expected: identifier"
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16982544
My fault.  Change

    WithEvents olkControl As Object

to

    WithEvents olkControl As CommandBarButton
0
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 16982949
Few points:

1. During attachment save it should show Browse Button to select folder.
2. For message forward I don't want to show the Forward Message Window. It should directly forward the message without any warning message.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 76

Expert Comment

by:David Lee
ID: 16987499
1.  That wasn't mentioned in your question.  You said "will ask me for a path " and that's what this code does.  I'll look and see how involved it'd be to add a browse capability.  If it's not too involved, then I'll add it to the code.

2.  As I mentioned in my 11:04AM post on 6/24, the code can send, but you'll get that security dialog-box each time it does.  That's going to be way more annoying than having to click Send.  If you want it to send instead of displaying, then change

    olkForward.Display

to

    olkForward.Send
0
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 16999511
Hi BlueDevilFan,

Thank you very much for your response. It was my fault not to mention that I want a Browse Button. Ok never mind, this is not important and for the warning message I will try to show the foward window.
But my last question:
Is there any way that this whole thing can be done using VB. As a programmer it is easy for me to follow the process that you specify but I dont want my client (for whom I can doing this) to go through the whole process. It should be installed as a outlook add-in and he can un-install this later.

I want to mention that in the very 1st requirement I specified that I want a VB application.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 16999713
> in the very 1st requirement I specified that I want a VB application.
Yes, I noticed that.  Some questions authors don't draw a distinction between an Outlook macro and a VB application, so I wasn't sure in this case.  With a couple of modifications this could be a VB application, but it really doesn't make sense to do that.  What would we gain by making this a full-fledged VB program?  It won't have an interface and as a separate app the user would have to be careful about starting it when Outlook wasn't running and would need to remember to close it one they'd shut Outlook down.  You mention that you "don't want my client to go through the whole process".  Go through what process?  Once the code is in place there's nothing to do.  If you're talking about the process of putting the code in place, then I submit that as an app or add-in the user would have to run an installation program or copy files to certain folders, etc.  With the exception of sending, which I've mentioned is going to be a problem, everything is done automatically.  You mention doing this as an Outlook add-in, which is different from both a VB application and a macro.  Doing this as an add-in would make more sense than doing it as a VB app, but I'm still not sure I see the point.  What's gained?  Without regard to which approach you ultimately use, the code remains essentially the same.  
0
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 16999761
Thanks for your quick response. May be I was not too clear in my comment. Regarding Vb app what I meant is not that the user have to run the app every time with outlook. But what I intended to say, is it possible to run the commands (copy paste the code, setting the security level etc) from a VB app so that I can do the coding in the exe and user have to click a button and every thing will be done. After that everything will work as you specified and ther user dont need to run the vb program again.
Actually I dont have much idea about add-in. So if you give me some information about how the add-in can be useful in this case (instead of VB) and also how to make an add-in for this, then that will be a great help.

Thanks
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17005529
Oops, sorry, I didn't understand your intent for the VB app.  I don't think what you described is going to work.  I won't say that it can't work, I've seen some awfully inventive solutions to various things, but I can say that there's no provision for doing this from VB.  Microsoft hasn't provided any APIs in Outlook or CDO for doing this.  Changing the security level from code would be a dangerous thing to allow anyway since it'd give a hacker or virus author a potential means of altering Outlook's security setting to allow their code to work better.  Microsoft also didn't provide any programmatic means of getting macro code into Outlook.  I think their design philosophy must've been that macros would be written by individual users for their own use and that developers would distribute code they created as add-ins.  Here are a coupleof links to more information about add-ins.

http://www.outlookcode.com/d/comaddins.htm
http://www.microeye.com/resources/template.htm
0
 
LVL 11

Author Comment

by:Rajesh Dalmia
ID: 17007362
Thank you very much BlueDevilFan for your reply.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17007964
You're welcome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now