Salutation macro for Outlook
Hi,
I am trying to get a Macro to run in Outlook that inserts a salutation/greeting.
When I run it, I am getting a Microsoft Visual Basic box with error:
Run-time error '5': Invalid procedure call or argument
I then click debug and it highlights the following code on line 28:
strGreetName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
I am running Win7 Pro with Outlook 2010.
Thanks in advance for any help.
Here is the complete macro:
Sub Greeting()
Dim Msg As Outlook.MailItem
Dim MsgReply As Outlook.MailItem
Dim strGreetName As String
Dim lGreetType As Long
' set reference to open/selected mail item
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set Msg = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set Msg = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0
If Msg Is Nothing Then GoTo ExitProc
' figure out greeting line
On Error Resume Next
lGreetType = InputBox("How to greet:" & vbCr & vbCr & "Type '1' for name, '2' for time of day")
On Error GoTo 0
If lGreetType = False Then GoTo ExitProc
If lGreetType = 1 Then
strGreetName = Left$(Msg.SenderName, InStr(1, Msg.SenderName, " ") - 1)
ElseIf lGreetType = 2 Then
Select Case Time
Case Is < 0.5
strGreetName = "Good morning"
Case 0.5 To 0.75
strGreetName = "Good afternoon"
Case Else
strGreetName = "Good evening"
End Select
Else ' something else entered??
GoTo ExitProc
End If
Set MsgReply = Msg.Reply
With MsgReply
.Subject = "RE:" & Msg.Subject
.HTMLBody = "<span style=""font-family : verdana;font-size : 10pt""><p>Hello " & strGreetName & ",</p></span>" & .HTMLBody
.Display
End With
ExitProc:
Set Msg = Nothing
Set MsgReply = Nothing
End Sub
Re: Salutation macro for Outlook
Just a guess... Perhaps Msg.SenderName doesn't contain any space character when that happens? That would lead to calling Left$() with a negative number passed as the second parameter, which definitely would raise the error you report.
Re: Salutation macro for Outlook
Quote:
Originally Posted by
Eri523
Just a guess... Perhaps Msg.SenderName doesn't contain any space character when that happens? That would lead to calling Left$() with a negative number passed as the second parameter, which definitely would raise the error you report.
I found the following:
Outlook blocks code that attempts to access the SenderName property for security reasons. If you run a third-party add-in, custom solution, or other program that uses the SenderName property in Office Outlook 2003, you may receive the following warning:
Source: http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
Perhaps I can use a different property in place of SenderName.
Re: Salutation macro for Outlook
AFAICT that wouldn't raise an Error 5, though.
Re: Salutation macro for Outlook
Outlook has blocked auto-send for at least 10 years. For 2-3 you could use a macro, but that was killed as well. 2 minute per send afaik
Re: Salutation macro for Outlook
Back to the original error....
You have to keep in mind that the way Office 2010 works from ( for example Office 2003 ) has changed significantly. I'd suggest having a look at MSDN's documentation on Outlook 2010 with VBA