• Problem with Address property (97)

    Author
    Topic
    #362013

    To all:

    I’m trying to trap the Send event in Outlook to make sure the email address is valid before sending. Basically, we have a situation where email addresses from InterAction have a wrong email address format, so I decided to write some code (below) in Outlook’s Send event to correct it.

    OK, testing for and correcting a bad email address is no big deal. What I’m having a problem with is writing the corrected email address back to the Recipients(n).Address property. The Microsoft documentation says the Address property is read/write, but every time I run the code, I get an error message saying that the Address property is read-only!

    What am I doing wrong here? Is this because I shouldn’t even be using the Send event to try to do this, because the email has actually been sent off and it’s already too late to change the bad email address?

    Thanks for your help,

    Stephan

    ‘****************************************** BEGIN CODE ******************************************
    Option Explicit

    ‘ ————————————————————————
    ‘ Purpose: Traps the Item_Send event so that invalid email addresses in
    ‘ the format (name ) from InterAction get changed.

    ‘ Inputs: None

    ‘ Written: 10/26/01 by Stephan Ip
    ‘ ————————————————————————
    Function Item_Send()
    Dim n
    Dim strEmailAddr
    Dim lngBeginBracket, lngEndBracket

    ‘ loop through all the recipients
    For n = 1 To Item.Recipients.Count
    strEmailAddr = Item.Recipients(n).Address
    lngBeginBracket = InStr(1, strEmailAddr, “”)
    ‘ test to see if email address has beginning and end brackets,
    ‘ which means it’s an invalid email address
    If lngBeginBracket 0 And lngEndBracket 0 Then
    ‘ parse email address to strip it of the brackets
    strEmailAddr = Mid(strEmailAddr, lngBeginBracket + 1, _
    lngEndBracket – (lngBeginBracket + 1))
    End If
    ‘ change email address to corrected parsed address
    Item.Recipients(n).Address = strEmailAddr ‘*** MS OUTLOOK VB HELP SAYS ADDRESS PROPERTY IS READ/WRITE
    ‘*** WHY DO I GET A MESSAGE SAYING IT’S READ-ONLY WHEN RUN???
    Next
    End Function

    Viewing 0 reply threads
    Author
    Replies
    • #548897

      I get the same error in Outlook 2000 SR-1 (without the “object model guard” update).

      I tried saving the information, deleting the bad address, and adding a new one in a separate loop. This works if you don’t mind losing the display name (the name also is a read-only property). Hope you can find a better solution:

      Sub Item_Send_test()
      Dim n
      Dim strEmailAddr
      Dim lngBeginBracket, lngEndBracket
         
      ''for testing as VBA module only
      'Dim item As MailItem
      'Set item = ActiveInspector.CurrentItem
         
      'new array
      Dim nameAddress()
      ReDim nameAddress(1 To 2, 1 To item.Recipients.Count)
         
      ' loop through all the recipients
      For n = item.Recipients.Count To 1 Step -1
          strEmailAddr = item.Recipients(n).Address
          lngBeginBracket = InStr(1, strEmailAddr, "")
          ' test to see if email address has beginning and end brackets,
          ' which means it's an invalid email address
          If lngBeginBracket  0 And lngEndBracket  0 Then
              ' parse email address to strip it of the brackets
              strEmailAddr = Mid(strEmailAddr, lngBeginBracket + 1, _
              lngEndBracket - (lngBeginBracket + 1))
          End If
          ' store new valid address and display name for adding later
          nameAddress(1, n) = strEmailAddr
          nameAddress(2, n) = item.Recipients(n).Name
          ' delete bad address
          item.Recipients(n).Delete
      Next
      ' add valid addresses
      For n = 1 To UBound(nameAddress, 2)
          If nameAddress(1, n)  vbNullString Then
              item.Recipients.Add nameAddress(1, n)
          End If
      Next
      End Sub
      • #549134

        Thank you so much for getting back to me. I revised your code slightly (below) to work in Outlook 97/VBScript. Any invalid email addresses are in fact being to corrected to valid formats (and no I don’t mind losing the display name), which is good, but now the send operation is failing (after the email address gets corrected). I get an error message saying “The operation failed”.

        I’m beginning to think what I’m trying to do (correct bad email address formats before sending) may be impossible to do programmatically in Outlook? Were you able to send the email successfully after your code ran?

        Also, can you recommend some decent Outlook 97 documentation?

        Thanks again,

        Stephan

        ‘************************************** BEGIN CODE **************************************
        Option Explicit

        ‘ ————————————————————————
        ‘ Purpose: Traps the Item_Send event so that invalid email addresses in
        ‘ the format (name ) from InterAction get corrected.

        ‘ Inputs: None

        ‘ Written: 10/26/01 by Stephan Ip
        ‘ ————————————————————————
        Function Item_Send()
        Dim n
        Dim strEmailAddr
        Dim lngBeginBracket, lngEndBracket
        Dim intRecipients

        ‘ initialize
        intRecipients = CInt(Item.Recipients.Count)

        ‘ new array
        Dim nameAddress()
        ReDim nameAddress(2, intRecipients)

        ‘ loop through all the recipients
        For n = intRecipients To 1 Step -1
        strEmailAddr = Item.Recipients(n).Address
        lngBeginBracket = InStr(1, strEmailAddr, “”)
        ‘ test to see if email address has beginning and end brackets,
        ‘ which means it’s an invalid email address
        If lngBeginBracket 0 And lngEndBracket 0 Then
        ‘ parse email address to strip it of the brackets
        strEmailAddr = Mid(strEmailAddr, lngBeginBracket + 1, _
        lngEndBracket – (lngBeginBracket + 1))
        End If
        ‘ change email address to corrected parsed address
        ‘*** FOLLOWING LINE REM’ED OUT BECAUSE ADDRESS PROPERTY APPEARS TO BE READ-ONLY
        ‘*** EVEN THOUGH OUTLOOK VB HELP SAYS OTHERWISE
        ‘Item.Recipients(n).Address = strEmailAddr
        ‘ store new valid address and display name for adding later
        nameAddress(1, n) = strEmailAddr
        nameAddress(2, n) = Item.Recipients(n).Name
        ‘ delete bad address
        Item.Recipients(n).Delete
        Next

        ‘ add valid addresses
        For n = 1 To UBound(nameAddress, 2)
        ‘If nameAddress(1, n) vbNullString Then
        If nameAddress(1, n) “” Then
        Item.Recipients.Add nameAddress(1, n)
        End If
        Next
        End Function

        • #549592

          > Were you able to send the email successfully after your code ran?

          blush I didn’t test it because I was using invalid old imported addresses as test data. My mistake.

          > Also, can you recommend some decent Outlook 97 documentation?

          Not really, I never learned VBScript and have been learning the Outlook 2000 object model in VBA, which probably doesn’t match up too well.

          The basics, of course, can be found at MS, e.g., OL97: Questions About Customizing or Programming Outlook. Sorry I can’t tell you more.

          • #549868

            I want to thank you again for your help on this.

            The problem here was really the limitations of what could happen in Outlook’s Send event, and even in Outlook 2000, I don’t know if this could work. (Of course, not having Outlook 2000 here, I can’t really test, but it’s a moot point, because I had to make this work in OL 97.)

            Your idea to delete and then re-add the bad email addresses was a good one. It’s just unfortunate that OL’s Send event refuses to send and gives me an error message when I do that. Curiously enough, if I add error handling, the mail message will be sent, but I still get the error message (which is an unacceptable solution).

            I also thought of a clunkier solution — which is to have the Item_Send function test for bad email addresses, then return FALSE if there are any, and then have the user click another Command Button to delete and re-add the bad emails, then have the user click the Send button again. Stupid, ugly and clunky, but i’tll work.

            Thanks again.

            Stephan

    Viewing 0 reply threads
    Reply To: Problem with Address property (97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: