0

EDIT: apparently I have a problem with invoking ItemSend as breakpoint don't break when e-mail is sent

Code is in ThisOutlookSession in Application Object window

list of activated references

====

We started using different signatures for internal only e-mails and for e-mails with at least one external user. It is not much suitable to select proper signature every time manually, so I tried to use Outlook Macro for this

I used this ExtendOffice setup and tried to combine this with array from answer of this post on stackoverflow. My result code is below:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim pa As PropertyAccessor

    Dim prompt As String
    Dim rAddress As String

    Dim lLen As Long
    Dim Str1 As String

    Dim arrayDomains() As Variant
    Dim i As Long

    Dim internalFlag As Boolean
    Dim externalFlag As Boolean

    Dim strExtAdd As String

    Dim xMailItem As MailItem
    Dim xFSO As Scripting.FileSystemObject
    Dim xSignatureFile, xSignaturePath As String
    Dim xDoc As Document


    Set xMailItem = Item

    arrayDomains = Array("internal.com", "subsidiary.com")

    Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"

    xSignaturePath = "C:\Users\" & Environ("username") & "\AppData\Roaming\Microsoft\Signatures\" 'CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"

    Set recips = Item.Recipients

    For Each recip In recips

        Set pa = recip.PropertyAccessor

        rAddress = LCase(pa.GetProperty(PR_SMTP_ADDRESS))
        lLen = Len(rAddress) - InStrRev(rAddress, "@")
        Str1 = Right(rAddress, lLen)

        internalFlag = False

        For i = LBound(arrayDomains) To UBound(arrayDomains)
            If Str1 = arrayDomains(i) Then
                internalFlag = True
                Exit For
            End If
        Next

        If internalFlag = False Then
            externalFlag = True
        End If

    Next

    If externalFlag = True Then

        xSignatureFile = xSignaturePath & "ext.htm"

    Else

        xSignatureFile = xSignaturePath & "int.htm"

    End If


    VBA.DoEvents
    Set xDoc = xMailItem.GetInspector.WordEditor
    xDoc.Application.Selection.EndKey
    xDoc.Application.Selection.InsertParagraphAfter
    xDoc.Application.Selection.MoveDown Unit:=wdLine, Count:=1
    xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub

The result is that it is not working, I cannot get the signature to the e-mail. The working solution should be, that everyone from parent company and subsidiaries should receive signature with some internal links, and if in the recipients is someone who is not from parent company or subsidiary, the links in signature should be removed.

zmk
  • 25
  • 5
  • And what is not working? Add the signature? Check for an external recipient? Choose the correct signature file? – Shrotter Jun 20 '22 at 09:02
  • not sure how to check where is the problem. debug.print and msgbox i tried to submit to each steps get me nothing – zmk Jun 20 '22 at 09:46
  • Try to set a breakpoint in the `ItemSend` event handler and when it is hit run the code line-by-line checking the results. What exactly is not working for you? – Eugene Astafiev Jun 20 '22 at 14:49
  • `If Str1 = arrayDomains(i) Then` – niton Jun 20 '22 at 16:11
  • @niton thanks, I forgot <> when tried reverse logic out of despair – zmk Jun 21 '22 at 06:19
  • @EugeneAstafiev I put breakpoint on first For and it didn't do anything, the mails are just sent and received by other party without any modification and without any break in the code, also breakpoint on the first line do nothing, it allows mails to be send – zmk Jun 21 '22 at 06:33
  • Edit the question to indicate it is about invoking `ItemSend` and the failure of the breakpoint. The code to pick the signature is not relevant at this point. – niton Jun 21 '22 at 10:38
  • Are you sending by clicking the Send button on the mail or other VBA code? Not for instance through C#. – niton Jun 21 '22 at 11:08
  • send button in new mail or reply or forward – zmk Jun 22 '22 at 06:07
  • Sending from outside of Outlook is the usual cause. Last guess then. Would you confirm other VBA code runs. – niton Jun 23 '22 at 16:55
  • i tried these two and no, they seems not to be working for me – zmk Jun 28 '22 at 06:22
  • Another test. See if `Application_Startup` runs. Include the outcome in the question. Similar question https://stackoverflow.com/questions/73425308/application-startup-not-called-event-code-triggers-only-when-visual-basic-edito – niton Aug 23 '22 at 14:16

1 Answers1

-1

The result is that it is not working, I cannot get the signature to the e-mail.

First of all, you need to subscribe to the ItemSend event of the Application class to get the event fired correctly. As soon as you get the event fired you may start the code under the debugger and go through each line checking the results. So, following that way you can find the cause and check how the code is working.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45