Consulting

Results 1 to 11 of 11

Thread: Solved: Outlook 2003 Selecting an email and forwarding it...

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    Solved: Outlook 2003 Selecting an email and forwarding it...

    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.

  2. #2
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Alternatively, can anyone suggest a work around?

  3. #3
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    To identify the currently selected item, use

    [VBA]ActiveExplorer.Selection.Item(1)[/VBA]

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

    For example,

    [VBA]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[/VBA]

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

    [VBA]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[/VBA]

    To attach the code to a toolbar button, see Create Outlook toolbar buttons using VBA.

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

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  4. #4
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Fantastic help.
    Thank you.

  5. #5
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    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.

  6. #6
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    ... 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.

  7. #7
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.

    Quote Originally Posted by ukdane
    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.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  8. #8
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.


    Quote Originally Posted by ukdane
    ... 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.
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  9. #9
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    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.

  10. #10
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  11. #11

    Exclamation Getting the excel file...

    Is there any way to get the coded excel file to see how it works...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •