简体   繁体   中英

Outlook: Need to insert text and text variables into body of email reply based on selections from a custom form

My client service system sends email notifications when a new inquiry comes in. I am able to reply to the notification and the system will update the inquiry with information from my email reply.

Reply example:

To: "client inquiry system"
Subject: Re: I am having password trouble Inquiry:5601 
Body of email below:

Your password has been reset.

The above will append "Your password has been reset." to the inquiries description.

I am also able to trigger changes to Status ( ie Closed, Resolved, Defunct) if I place special syntax at the top of the email body.

To: "client inquiry system"
Subject: Re: Inquiry:5601 -- I am having password trouble
Body of email below:

Status=Closed
Your password has been reset.

The above will set the inquiry to Closed in my system.

I would like to use a form or macro button that will provide users with drop down selections or free form text that will be added to the top of the email body once set.

I have some familiarity with VBA, but very new. Please help!

I am not convinced by your reply to my comment but this answer is an attempt to be helpful. It includes four macros that demonstrate functionality you will need. I hope it is enough to get you started.

When you open Outlook's Visual Basic Editor, you will see something like the following down the left side of the screen. If you do not see it, click Ctrl + R .

- Project 1 (VbaProject.OTM)
  - Microsoft Office Outlook Objects
    ThisOutlookSession
  - Modules
      Module1

The hyphens will be in little boxes. If any hyphen is a plus, click the plus to expand the list under the heading.

Click ThisOutlookSession . You will get an empty code area on the right. This is like a module code area but is used for event routines. Copy this code into that area:

Option Explicit
Public WithEvents MyNewItems As Outlook.Items
Private Sub Application_Startup()

  ' This event routine is called when Outlook is started

  Dim NS As NameSpace
  Dim UserName As String

  Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")

  With NS
    UserName = .CurrentUser
   Set MyNewItems = .GetDefaultFolder(olFolderInbox).Items
  End With

  MsgBox "Welcome " & UserName

End Sub
Private Sub myNewItems_ItemAdd(ByVal Item As Object)

  ' This event routine is called each time an item is added to Inbox
  ' because of:
  '   Public WithEvents MyNewItems As Outlook.Items
  '   Set MyNewItems = .GetDefaultFolder(olFolderInbox).Items

  With Item
    Debug.Print "#####" & Format(Now(), "dMmmyy hh:mm:ss") & _
                ": Item added to Inbox with Subject: [" & .Subject & _
                "] from [" & .SenderEmailAddress & "] with Text body"
    Debug.Print .Body
  End With

End Sub

Close Outlook and click Yes for “Do you want to save the VBA project 'VbaProject.OTM?'”

Reopen Outlook. You will be told a program is trying to access email addresses. Click Allow access for , select 10 minutes and click Yes . You will get a window saying “Welcome John Doe”.

If this does not happen, select Tools then Macros then Security . Security level Medium must be selected to use macros safely.

The macro Application_Startup() has accessed Outlook's email database. It is not easy to avoid the user being asked to allow access since Outlook has a very robust security system. There is a four step self-certification process which should allow you suppress this question for your own macros. I have successfully performed the first three steps but have never mastered the fourth step. I have carefully followed such instructions as I can find on the web but nothing has worked for me. Perhaps you will be more successful or perhaps you have access to an expert who can guide you if you want to suppress this question

The macro Application_Startup() has done two things: issued the welcome message and initialised MyNewItems . The welcome message is just a demonstration that you can access the user's name which might be useful if you have a shared Inbox. Initialising MyNewItems activates the event routine myNewItems_ItemAdd() . This outputs details of the each new item to the Immediate Window.

This is a quick demonstration of event routines which I thought would be useful to you. However, I have discovered that if myNewItems_ItemAdd() is busy with one item when a second arrives, it is not called for the second item. I use a very old version of Outlook and this may be a bug that has been cleared in later releases. If you decide to use event routines, you need to check this out.

Another way of getting access to emails is Explorer . Insert a new module and copy the following code into it:

Option Explicit
Public Sub DemoExplorer()

  Dim Exp As Outlook.Explorer
  Dim ItemCrnt As MailItem
  Dim NumSelected As Long

  Set Exp = Outlook.Application.ActiveExplorer

  NumSelected = Exp.Selection.Count

  If NumSelected = 0 Then
    Debug.Print "No emails selected"
  Else
    For Each ItemCrnt In Exp.Selection
      With ItemCrnt
        Debug.Print "From " & .SenderName & " Subject " & .Subject
      End With
    Next
  End If

End Sub

DemoExplorer() shows another way of giving a macro access to mail items. The user selects one or more emails and then activates the macro DemoExplorer() . Again this just outputs some properties of a mail item to the Immediate Window.

Click F2 and the code window is replaced by a list of libraries. Scroll down the list of Classes and select MailItem . The right hand window displays all the members of MailItem . Some, such as ReceivedTime , are obvious but you will probably have to look up most. I suggest you make a note of all that look useful. Click a module, to get back to a code window when you have finished.

DemoReply() , below, is an updated version of DemoExplorer() which replies to selected emails. Add this code to your module:

Public Sub DemoReply()

  Dim Exp As Outlook.Explorer
  Dim ItemCrnt As MailItem
  Dim Reply As MailItem
  Dim Subject As String
  Dim SenderAddr As String
  Dim Received As Date

  Set Exp = Outlook.Application.ActiveExplorer

  If Exp.Selection.Count = 0 Then
    Debug.Print "No emails selected"
  Else
    For Each ItemCrnt In Exp.Selection

      ' Get properties of message received
      With ItemCrnt
        Subject = .Subject
        SenderAddr = .SenderEmailAddress
        Received = .ReceivedTime
      End With

      ' Create reply
      Set Reply = CreateItem(olMailItem)
      With Reply
        .BodyFormat = olFormatPlain
        .Body = "Thank you for your enquiry" & vbLf & _
                "  Subject: " & Subject & vbLf & _
                "  Received at: " & Format(Received, "d Mmm yyyy h:mm:ss") & vbLf & _
                "which will be handled as soon as an analyst is available."
        .Subject = "Thank you for your enquiry"
        .Recipients.Add SenderAddr
        ' Display allows the user to review the reply before it is written to Outbox
        ' but control is not returned to this macro. Only the first select mail item
        ' will be processed
        ' Send gives the user no opportunity to review the replies but the macro does not
        ' use control so all replies are sent.
        '.Display
        .Send
      End With

    Next
  End If

End Sub

I use an Outlook address for my private email and a Gmail address for my public email. I sent myself some text emails from the Gmail address. In Outlook, I selected these emails and activated DemoReply() . The expected replies arrived in my Gmail Inbox. Try sending yourself some emails and the try replying.

To demonstrate the use of a useform within Outlook, I inserted a new form and left the name as the default UserForm1 . I dragged two text boxes to the form which I left with their default names of TextBox1 and TextBox2 . I also dragged a command button which I renamed cmdSend .

An Outlook macro can only communicate with a user form via global variables. Add the following at the top of the module; they must be placed before any macros:

Public Box1 As String
Public Box2 As String

Add this macro to the module:

Sub DemoForm()

  ' Initialise global variables to be used by form before it is loaded
  Box1 = "Initial value for text box1"
  Box2 = "Initial value for text box2"

  Load UserForm1
  UserForm1.Show vbModal
  ' Control does not return to this module until user releases control of form

  Debug.Print Box1
  Debug.Print Box2

End Sub

Add this code to the form:

Private Sub cmdSend_Click()

  Box1 = TextBox1
  Box2 = TextBox2

  Unload Me

End Sub
Private Sub UserForm_Initialize()

  TextBox1 = Box1
  TextBox2 = Box2

End Sub

Activate DemoForm() . The form will appear with the text boxes set to "Initial value for text box1" and "Initial value for text box2". Change these values and click Send . Control will be returned to DemoForm() which outputs the new values to the Immediate Window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM