PDA

View Full Version : Solved: Outlook 2003 Selecting an email and forwarding it...



ukdane
01-21-2011, 12:59 AM
Hi,

I have written a basic script, which shows a user form, and asks the user for some data, which will be used to create a subject for an email. It also populates and creates the email with the .to and .subject fields.
What I need to happen is for the user to select an email (either in their inbox, or in a sub folder), and forward the email, using the form to create the subject and to lines.

The emails will always be forwarded to the same email address, and will always include an attachment. The key is the subject line, as a program will strip the email and upload the attachment to a file location, which is based on the text included in the subject.

So, does anyone have any idea how to achieve this, specifically how to activate the script? I'm guessing that I need to create a command bar button, which utilises data on the currently selected email, but have no idea how to achieve this in Outlook 2003.

Thanks in advance for your help.

ukdane
02-02-2011, 03:55 AM
Alternatively, can anyone suggest a work around?

JP2112
02-02-2011, 08:47 AM
To identify the currently selected item, use

ActiveExplorer.Selection.Item(1)

You'll also want to check that the currently selected item is an email.

For example,

Dim obj As Object
Dim msg As Outlook.MailItem
Set obj = ActiveExplorer.Selection.Item(1)

If Not obj Is Nothing Then
If TypeName(obj) = "MailItem" Then
Set msg = obj
Else
MsgBox "Please run this code on a mail item only."
Exit Sub
End If
End If

Now you want to forward the selected email, and prompt for subject. Use the Forward Method and assign the result to a new object.

Sub tst()
On Error GoTo ErrorHandler
Dim obj As Object
Dim msg As Outlook.MailItem
Dim newMsg As Outlook.MailItem
Dim subject As String
' change this to reflect the actual recipient
Const recip As String = "somewhere@mycompany.com"
' check for multiple selections
If ActiveExplorer.Selection.Count > 1 Then
MsgBox "please select one email only"
GoTo ProgramExit
End If

Set obj = ActiveExplorer.Selection.Item(1)
If Not obj Is Nothing Then
If TypeName(obj) = "MailItem" Then
Set msg = obj
Set newMsg = msg.Forward
' prompt for subject
subject = InputBox("What is the subject of the email?")
If Len(subject) = 0 Then
GoTo ProgramExit
End If
With newMsg
.subject = subject
.Recipients.Add recip
.Display ' or .Send
End With
Else
MsgBox "Please run this code on a mail item only."
GoTo ProgramExit
End If
End If
ProgramExit:
Exit Sub
ErrorHandler:
MsgBox Err.number & " - " & Err.Description
Resume ProgramExit
End Sub

To attach the code to a toolbar button, see Create Outlook toolbar buttons using VBA (http://www.codeforexcelandoutlook.com/blog/2009/11/create-outlook-toolbar-buttons-using-vba/).

If you provide more information about the attachment, we can go from there.

ukdane
02-03-2011, 01:41 AM
Fantastic help.
Thank you.

ukdane
02-03-2011, 02:31 AM
I've followed the link to your tutorial, and it works really well. But I have a stupid question....

.... how do I then delete an unwanted Toolbar Button? (I can't see that it's been included in the tutorial).

Thanks.

ukdane
02-03-2011, 06:22 AM
... and another question;

If I then need to install the button (and Macro) on all the users computers within the company, short of doing it individually, is there anyway to create a self extracting/installing file that will install the code on the users computer?

My final script contains code in ThisOutlookSession, a Form, and a Module.

Thanks in advance for your help.

JP2112
02-03-2011, 06:53 AM
You can do it manually. Just hold down the Alt key and drag the button away from the toolbar.

If you need to do it by code, you can adapt the sample from the link I provided. Just call the Delete Method after setting an object reference to the button.


I've followed the link to your tutorial, and it works really well. But I have a stupid question....

.... how do I then delete an unwanted Toolbar Button? (I can't see that it's been included in the tutorial).

Thanks.

JP2112
02-03-2011, 07:01 AM
I'm afraid not. VBA in Outlook is meant as a single-user experience. There's no way to do it as you describe.

You would need to deploy your solution as an addin instead. Then you could distribute an installer.



... and another question;

If I then need to install the button (and Macro) on all the users computers within the company, short of doing it individually, is there anyway to create a self extracting/installing file that will install the code on the users computer?

My final script contains code in ThisOutlookSession, a Form, and a Module.

Thanks in advance for your help.

ukdane
02-04-2011, 01:35 AM
Thanks again for your reply JP.


You would need to deploy your solution as an addin instead. Then you could distribute an installer.



Do you have any links to tutorials for how to do this? Is it possible without using 3rd party software?

It seems to me that addins in Outlook 2003 can't be created in the same way as they can for example in Excel 2003.

JP2112
02-04-2011, 08:00 AM
Not really, but a search for "how to create outlook addin" returned some promising results. Here's one using VB6:

http://support.microsoft.com/kb/316983

Macrosian
04-23-2011, 11:18 PM
Is there any way to get the coded excel file to see how it works...