CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    2

    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

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jul 2013
    Posts
    2

    Re: Salutation macro for Outlook

    Quote Originally Posted by Eri523 View Post
    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.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Salutation macro for Outlook

    AFAICT that wouldn't raise an Error 5, though.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured