• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1775
  • Last Modified:

Outlook VBA for Reply To All with Attachment

Hi,

I want a Outlook VBA which will be able to do the following:

1) I am selecting a message from my Inbox.
2) I am pressing a shortcut key for the VBA
3) This will do the "Reply to All" and a customized signature will be added as my "Reply". The Attachment (if any) will also automatically addded with the reply.

PLease help me. I am using MS Office outlook 2003.
0
suvmitra
Asked:
suvmitra
  • 8
  • 6
1 Solution
 
Chris BottomleyCommented:
SOmething like right click on an email and have an option to reply special ... or whatever you want to call it?

Customised signature - with outlook 2003 I don't think the signature can be added 'automatically that easily but it could be hard coded though.

Attachment automatically added - how would that be identified?

Chris
0
 
Shanmuga SundaramCommented:
0
 
Chris BottomleyCommented:
Assuming yes then two stages:

One add a sub to a normal code module as below:

Sub replySpecial()
Dim maiNew As Outlook.mailitem
Dim maiOrig As Object
   
    Set maiOrig = Application.ActiveExplorer.Selection.Item(1)
    If Not maiOrig Is Nothing Then
        Set maiNew = maiOrig.reply
        CopyAttachments maiOrig, maiNew
        maiNew.Display
    End If
   
Set maiNew = Nothing
Set maiOrig = Nothing
End Sub

Sub CopyAttachments(objSourceItem, objTargetItem)
' See outlookcode.com
Dim fso As Object
Dim fldTemp As Object
Dim strPath As String
Dim strFile As String
Dim objatt As Object

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) 'Temp
   strPath = fldTemp.path & "\"
   For Each objatt In objSourceItem.Attachments
      strFile = strPath & objatt.FileName
      objatt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objatt.DisplayName
      fso.DeleteFile strFile
   Next

   Set fldTemp = Nothing
   Set fso = Nothing
End Sub

Two add a sub to thisoutlooksession as Application_ItemContextMenuDisplay and paste the body of teh code from teh code in teh snippet therein.

Now when you right click on a mail in the explorer the menu has an option for reply special click and the 'new' mail is displayed for you to send

Chris
Private Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Office.CommandBar, ByVal Selection As Selection)
Const msoControlButton = 1
Const msoButtonIconAndCaption = 3
Dim objButton As CommandBarButton
    If Selection.count = 1 Then
        If Selection.Item(1).Class = olMail Then
            Set objButton = CommandBar.Controls.Add(msoControlButton)
            With objButton
                .Style = msoButtonIconAndCaption
                .Caption = "Reply Special with Attachments"
                .Parameter = Selection.Item(1).EntryID
                'List of face IDs here: http://www.kebabshopblues.co.uk/2007/01/04/visual-studio-2005-tools-for-office-commandbarbutton-faceid-property/'
                .FaceId = 355
                .OnAction = "ReplySpecial"
            End With
        End If
    End If
End Sub

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
suvmitraAuthor Commented:
chris_bottomley:
I believe I did exactly as you guided but I am not able to see any "Reply Special with Attachment" option at my outlook context menu. Please suggest. PS. I have restarted the outlook but there is nothing special things are happening.
 
0
 
Chris BottomleyCommented:
In thisoutlooksession click on application and then ItemContextMenuDisplay - make sure the supplied code is therein and that there is only one such sub declaration in the module.

Chris
0
 
suvmitraAuthor Commented:
Hi,
My outlook does not have any ItemContextMenuDisplay into the application. I copy and paste the entire code but this is not working. Please suggest.

outlook.JPG
0
 
Chris BottomleyCommented:
I really must get head out from under the pillow!

Okay that is an outlook 2007 feature and I forget how much is new - Sorry.

Next opportunity then is to trigger a modified macro via a button on outlook itself i.e. under tools macro menu or perhaps a button, (not 100% about buttons in OLK2003 but I will try if needed.

Any good?

Chris
0
 
suvmitraAuthor Commented:
Can you please do that and test. I need to implement this in outlook 2003 environment. Thank you for your help and effort.
0
 
Chris BottomleyCommented:
Okay the the modified snippet below goes in a code module.

In the outlook interface:

Right click in teh toolbar white space and click customise
On teh commands tab : macros | find reply special and drag it to a toolbar.

Now exit back to the outlook screen and select an email to reply to and use the reply special entry in the relevant menu.

Chris
Sub replySpecial()
Dim maiNew As Outlook.mailitem
Dim maiOrig As Object
    
    If TypeName(Application.ActiveWindow) = "Explorer" Then
        Set maiOrig = Application.ActiveExplorer.Selection.Item(1)
    ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
            Set maiOrig = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If
    If Not maiOrig Is Nothing Then
        Set maiNew = maiOrig.reply
        CopyAttachments maiOrig, maiNew
        maiNew.Display
    End If
    
Set maiNew = Nothing
Set maiOrig = Nothing
End Sub
 
Sub CopyAttachments(objSourceItem, objTargetItem)
' See outlookcode.com
Dim fso As Object
Dim fldTemp As Object
Dim strPath As String
Dim strFile As String
Dim objatt As Object
 
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) 'Temp
   strPath = fldTemp.path & "\"
   For Each objatt In objSourceItem.Attachments
      strFile = strPath & objatt.FileName
      objatt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objatt.DisplayName
      fso.DeleteFile strFile
   Next
 
   Set fldTemp = Nothing
   Set fso = Nothing
End Sub

Open in new window

0
 
suvmitraAuthor Commented:
What I can say..this is simply excellent piece of Art. I just want to add to more things:
a) If you look at the picture it is taking the entire refernce as name and it seems too long. How do I rename it say "Reply Special" instead of Project1.Project1.Module2.Pastespecial.
b) I want to populate a generic message with the Reply to All like,
"Hi,
This is the Reply.
Regards,
ABC"

special.JPG
0
 
Chris BottomleyCommented:
a) If you look at the picture it is taking the entire refernce as name and it seems too long. How do I rename it say "Reply Special" instead of Project1.Project1.Module2.Pastespecial.

IN an open area of the menus, click customise now right click the button and you can edit the name in the text field.

b) I want to populate a generic message with the Reply to All like,See replyspecial as updated below

Chris
Sub replySpecial()
Dim maiNew As Outlook.mailitem
Dim maiOrig As Object
    
    If TypeName(Application.ActiveWindow) = "Explorer" Then
        Set maiOrig = Application.ActiveExplorer.Selection.Item(1)
    ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
            Set maiOrig = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If
    If Not maiOrig Is Nothing Then
        Set maiNew = maiOrig.reply
        maiNew.body = "Hi, " & vbCrLf & vbCrLf & _
            "This is the Reply." & vbCrLf & vbCrLf & _
            "Regards," & vbCrLf & vbCrLf & _
            "ABC" & _
            maiNew.body
        CopyAttachments maiOrig, maiNew
        maiNew.Display
    End If
    
Set maiNew = Nothing
Set maiOrig = Nothing
End Sub

Open in new window

0
 
suvmitraAuthor Commented:

Brilliant!!

Everything is just perfect!

But still I am not able to rename the Macro. I am not getting any right click / context menu option there. Please see the attached.

not.JPG
0
 
Chris BottomleyCommented:
NO ...

Having launched the customise box, ignore it and move to your button and right click that - on completion of the edit THEN close the customise box

Chris
0
 
suvmitraAuthor Commented:
This is Done!! Thank you.
0
 
Chris BottomleyCommented:
Glad to help

Chris
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now