macro to autoreply & forward

User avatar
stuck
Panoramic Lounger
Posts: 8125
Joined: 25 Jan 2010, 09:09
Location: retirement

macro to autoreply & forward

Post by stuck »

I get messages from our db that prompt me to create a PDF. Once I've done that I then have to:
1) reply to the incoming message, except that I first change the 'To' field from the db address to a short distribution list I have. The reply is std text, currently I copy & paste from a previous reply.
2) forward the incoming message to another short distribution list, adding some std text and attaching the PDF I've just created.

Presumably these sort of repetitive tasks could be done at the click of a button? If Outlook 2007 had a macro recorder I'd start there but it hasn't. The only VBA I've done is some XL stuff and then not very well.

Any suggestions gratefully received.

Ken

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: macro to autoreply & forward

Post by HansV »

Hi Ken,

Perhaps you can use this as starting point. It merely displays a reply and a forwarded message, for you to complete and send. We can add bells and whistles if necessary.

Code: Select all

Sub ReplyAndForward()
  Dim strMsg As String
  Dim objMsgIn As MailItem
  Dim objMsgRpl As MailItem
  Dim objMsgFwd As MailItem

  On Error GoTo ErrHandler

  If TypeName(ActiveWindow) = "Explorer" Then
    If ActiveExplorer.CurrentFolder.Items.Count > 0 Then
      If ActiveExplorer.Selection.Count = 1 Then
        If ActiveExplorer.Selection.Item(1).Class = olMail Then
          Set objMsgIn = ActiveExplorer.Selection.Item(1)

          ' Create a reply
          Set objMsgRpl = objMsgIn.Reply
          With objMsgRpl
            .To = "stuck@upnorth.co.uk"
            .Body = "This is my reply" & vbCrLf & .Body
            .Display
          End With

          ' Forward the message
          Set objMsgFwd = objMsgIn.Forward
          With objMsgFwd
            .To = "someone.else@somewhere.com"
            .Body = "This is a forwarded message" & vbCrLf & .Body
            .Display
          End With
        Else
          strMsg = "Please select an e-mail"
        End If
      Else
        strMsg = "Please select a single message."
      End If
    Else
      strMsg = "This folder doesn't contain any items."
    End If
  End If
  If Not strMsg = "" Then
    MsgBox strMsg, vbExclamation
  End If
  Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation
End Sub
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8125
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: macro to autoreply & forward

Post by stuck »

Yes, thank you, that's a good starting point.

So good in fact that once I've tidied up the layout of my own fixed text (I need a couple more " & vbCrLf & _ to space it correctly) it might be a finishing point too!

I'll be back tomorrow if I need more. Meanwhile, have a :chocciebar:

:cheers:

Ken

User avatar
stuck
Panoramic Lounger
Posts: 8125
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: macro to autoreply & forward

Post by stuck »

I'm back... no sorry, I'm stuck...

This probably should be in a new thread but this morning I found the above code was disabled. I can make it work again by turning off the macro security in the Trust Center (not recommended) but that seems a bit heavy handed.

How can I make Outlook run 'my own' code (or is it so clever it knows the code was really written by Hans :grin: ) without having to get a digital signature?

Ken

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: macro to autoreply & forward

Post by HansV »

You can create a digital signature for your personal use: see Digitally sign a macro project. This signature will not be valid on other computers.
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8125
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: macro to autoreply & forward

Post by stuck »

Ah! didn't realise I could create a self-sign cert. That does the job nicely.

Thanks again.

Ken