AutoHi

Status
Not open for further replies.

ska

New Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Hello, I want to insert greeting in reply message.

Sub a1()

'Now Time
Dim st As String
F3 = Format(Time, "h")
'MsgBox (F3)
'============
Select Case F3
Case Is >= 18
st = "Good evening, "
Case Is >= 10
st = "Good day, "
Case Is >= 6
st = "Good morning, "
End Select
'============
' Reply all
Dim o As MailItem
Set o = Application.ActiveExplorer.Selection.Item(1)
o.ReplyAll.Display

' get recipient name
Komy = o.Recipients.Item(1)
' If 0 - an error with massive.
' If 1 - I get my name, but not a name of recipient.
' If there are a few recipients and I use (2) - I get the name of second recipient.
'============
' I want to insert "Good evening, %Komy%" in the begin of letter.
' How to do it? Now I can only display message box:
MsgBox (st & Komy)

End Sub


Please help.
 
The ReplyAll function returns a reference on the new mailitem, you can use the available variable for it:
set o=o.replyall

Now work with that variable, that is:
o.display

This adds your text to the beginning of the body of the email, then it adds the already existing body:
o.body=st & o.body
 
Michael, thank you very much! It's working!
Can I ask you a few questions? :)
1. How to save on original format of message? If I use HTMLBody, I get a garbage:

...
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
</xml><![endif]--><link rel=themeData href="~~themedata~~"><link rel=colorSchemeMapping href="~~colorschememapping~~"><!--[if gte mso 9]><xml>
<w:WordDocument> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting/> <w:EnvelopeVis/> <w:ValidateAgainstSchemas/>
...


2. How to add my macros in context menu of message (near with reply, reply all and forward)?
 
1. Do you mean you have an html formatted message and want your new text be html, too? Then write it to the HtmlBody property instead of Body, and use html tags. For instance:
o.htmlbody="<p>" & st & "<p>" & o.htmlbody

2. Here's an example for an appointment item, please adapt it yourself:

Private WithEvents ConfirmAppointment As Office.CommandBarButton

Private Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Office.CommandBar, _
ByVal Selection As Selection)
Dim obj As Object
Dim Item As Outlook.AppointmentItem
Dim Btn As Office.CommandBarButton
Dim Caption$
Caption = "Confirm Appointment"
If Selection.Count = 1 Then
Set obj = Selection(1)
If TypeOf obj Is Outlook.AppointmentItem Then
Set Item = obj
Set Btn = CommandBar.Controls.add(msoControlButton, , , , True)
Btn.Style = msoButtonCaption
Btn.Caption = Caption
Set ConfirmAppointment = Btn
End If
End If
End Sub
 
Inserted, but I don't see anything.
 
It adds a button when you right click on an appointment item in the calendar. Please look at the code, and adapt so it works for mailitems.
 
Thank you, adapted. Please, tell me, how call AutoHi?

Code:
Private WithEvents ConfirmAppointment As Office.CommandBarButton
Private Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Office.CommandBar, ByVal Selection As Selection)
Dim Item As Outlook.MailItem
Dim Btn As Office.CommandBarButton
Dim Caption$
Caption = "Replay + hello"
Set Btn = CommandBar.Controls.Add(msoControlButton, , , , True)
Btn.Style = msoButtonCaption
Btn.Caption = Caption
End Sub

Also I optimize the code:

Code:
Sub AutoHI()
    'Current time
    Dim h As Integer
    h = DatePart("h", Time)
    '============
    ' Hello.
    Dim greeting As String
    Select Case h
        Case Is < 6
            greeting = "Good night, "
        Case Is < 10
            greeting = "Good morning, "
        Case Is < 18
            greeting = "Good day, "
        Case Is < 22
            greeting = "Good evening, "
        Case Else
            greeting = "Good night, "
    End Select
    '============
    ' Reply All.
    Dim o As MailItem
    Set o = ActiveExplorer.Selection(1).ReplyAll
    ' Add recipients.
    Dim rec As Recipient
    For Each rec In o.Recipients
        greeting = greeting & ", " & rec.Name
    Next rec
    'Insert in the begining.
    o.HTMLBody = "<P>" & greeting & "!</P>" & o.HTMLBody
    'Show.
    o.Display
End Sub
 
The variable declared WithEvents has a click event, which will be called when you hit the button. That's where you call AutoHi.
 
Code:
Btn.OnAction = "AutoHI"
Many thanks, Michael!!!
 
Status
Not open for further replies.
Back
Top