Skip to main content

How to automatically change signature based on recipients in Outlook?

By default, Outlook has a build-in function for users to automatically change signature while sending emails through different email accounts. But beyond that, here I will show you method of automatically changing signature based on different recipients in the To field in Outlook.

Change signature based on recipients automatically with VBA code


Change signature based on recipients automatically with VBA code

Please follow the below steps to apply different signatures to corresponding recipients while sending emails in Outlook.

1. Firstly, you need to disable the auto attached signature feature in Outlook. Please click File > Options to open the Outlook Options window.

2. In the Outlook Options window, select Mail in the left pane, then click the Signatures button in the Compose messages section. See screenshot:

3. In the Signatures and Stationery dialog box, go to the Choose default signature section under the E-mail Signature tab, select an email account in the Email-account drop-down list, and then choose (none) from the New messages and Replies/forwards drop-down lists. Repeat these steps until all email accounts are set to (none). Then click the OK button.

Note: You can also create your needed signatures in this Signatures and Stationery dialog box.

4. Click the OK button when it returns the Outlook Options window.

5. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

6. In the Microsoft Visual Basic for Applications window, double click ThisOutlookSession in the left pane to open the Code window, and the copy below VBA code into the window. See screenshot:

VBA code: Automatically change signature based on recipients in Outlook

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/08/01
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
Dim xFindStr As String
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
    If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
        xRcpAddress = xRecipient.AddressEntry.Address
    End If
    Select Case xRcpAddress
        Case "Email Address 1"
            xSignatureFile = xSignaturePath & "aaa.htm"
            Exit For
        Case "Email Address 2", "Email Address 3"
            xSignatureFile = xSignaturePath & "bbb.htm"
            Exit For
        Case "Email Address 4"
            xSignatureFile = xSignaturePath & "ccc.htm"
            Exit For
    End Select
Next
VBA.DoEvents
Set xDoc = xMailItem.GetInspector.WordEditor
xFindStr = "From: " & xMailItem.Recipients.Item(1).Name & " <" & xRcpAddress & ">"
If VBA.InStr(1, xMailItem.Body, xFindStr) <> 0 Then
    xDoc.Application.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With xDoc.Application.Selection.Find
        .ClearFormatting
        .Text = xFindStr
        .Execute Forward:=True
    End With
    With xDoc.Application.Selection
        .MoveLeft wdCharacter, 2
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
Else
    With xDoc.Application.Selection
        .EndKey Unit:=wdStory, Extend:=wdMove
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
End If
xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub

Notes:

  • 1). In the VBA code, please replace the “Email Address 1/2/3/4” with the certain email addresses of the recipients.
  • 2). "aaa.htm", "bbb.htm" and "ccc.htm" are the specified signatures you will send to corresponding recipients.
  • 3). In this case, signature “aaa” will be sent to “Email Address 1”, signature “bbb” will be sent to “Email Address 2” and “Email Address 3, and “Email Address 4” will receive the email embedded with signature “ccc”. Please change them based on your needs.
  • 4). If there are multiple recipients in an email, the code only takes the first recipient into consideration. In this case, other recipients will receive the emails with the same signature as the first recipient.

7. Then click Tools > References to go to the References-Project dialog box. In the dialog, please check both the Microsoft Word Object Library and the Microsoft Scripting Runtime options, and then click OK button, see screenshot:

8. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

From now on, after composing an email and hitting the Send button, the corresponding signature will be automatically inserted at the end of the email body based on the recipient’s email address in the To field.


Automatically Insert Current Date as Signature When Sending Email In Outlook:

If you want to insert timestamp as signature into the email body while creating/replying/forwarding new email in your Outlook, you can enable the Add date signature when create new, reply and forward email option of Kutools for Outlook to acheive it. See screenshot:
Download and try it now (60-day free trail)


Best Office Productivity Tools

Kutools for Outlook - Over 100 Powerful Features to Supercharge Your Outlook

Email Automation: Auto Reply (Out of Office)  /  Schedule Send emails  /  Auto CC/BCC  /  Advanced Auto Forward  /  Auto Add Greating ...

Email Management: Easily Recall Emails  /  Block Scam Emails  /  Delete Duplicate Emails  /  🔎Advanced Search  /  Consolidate Folders ...

📁 Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface & Interaction Magic: 😊More Pretty and Cool Emojis   /  Brings Browser Tabs Right Into Your Outlook  /  Minimize Outlook Instead of Closing ...

👍 One-click Wonders: Reply All with Incoming Attachments  /   Anti-Phishing Emails  /  🕘Show Sender's Time Zone  /  Send to Recipients Separately ...

👩🏼‍🤝‍👩🏻 Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Over 100 Features Await Your Exploration! Click Here to Discover More.

Read More       Free Download      Purchase
 
Comments (43)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi Crystal,

first of all thanks a lot for your effort in devolping a such useful and smart code!

I'm facing a very similar problem to Josh's one #40488 (also if not identical). I merged two versions of your code in way to have the internal and external different signature feature depending on the recipient address (Pauli #33113) and to solve the problem about the signature being posted at the last selection point in the message (Eric #38525).

When creating a new email everything works flawlessy but unfortunately when replying (both if only to internals, only to externals or both) the signature is being posted at the end of the entire conversation thread instead of at the end of my last reply.

I performed different tests and tryed some mods but i didn't came up. May you please help me in solving this problem? I'm looking to assemble a code with internal and external different signature depending on recipient feature posting the signature always at the end of my last message (in both new message and reply message).

I'm at your complete disposal for helping debugging and testing. I think this should be a definitive full feature version for lot of users.

Thanks a lot in advance anyway and have a nice day.
This comment was minimized by the moderator on the site
Hi Lorenzo,

Can you tell me which code are you using? Is it the one I replied to Pauli Pauli #33114?
This comment was minimized by the moderator on the site
Hi Crystal,
first thanks a lot for your efforts in coding these Outlook signature smart solutions!

I'm facing a similar problem to Josh #40488: I merged your code from your answer to Pauli #33114 (std version + internal and external signature recognition) with the update you gave as answer to the problem faced by Lelian #39096 (signature posted at the end of all the conversation history when replying).

Unfortunately the update given to Lelian #39096 seems not to work in my case very likely such as (but not identical) posted by Josh. When i create a new message everything it's working flawlessy but unfortunately when replying with both the internal or external version of the signature, that is posted at the end of all the conversation thread instead of at the end of only my last message.

I tried different tests and mods but I'm still not managing in understanding why. May you please help me/us to find a solution to this problem? I'm at your disposal for debugging and testing purposes. I think that this one should be a definitive version for lot of users.

Thanks in advance anyway and have a nice day.
This comment was minimized by the moderator on the site
Hi! I have implemented the code at for the most part it works as intended. However, every now and then it inserts the signature in the middle of a message. it first happened when I attached in table in the body. the signature was inserted into the table. Also, it has happened that it cuts off the some of the text such that two-three lines of text are in the end of the mail (after the signature). This doesn't happen all the time, but I hope you can help to solve it so that it becomes more reliable.
This comment was minimized by the moderator on the site
Hi Mikkel Lundsgaard,
The code works well in my case. Which Outlook version are you using?
This comment was minimized by the moderator on the site
I suppose it's the latest version.
This comment was minimized by the moderator on the site
Hi Crystal,

Hope you are well.

Can you advise if it is possible to add a different signature based upon the subject field having specific words.

Many thanks
Matt
This comment was minimized by the moderator on the site
Hi Matt Read,
Thank you for your comment. I am not able to solve this problem yet.
This comment was minimized by the moderator on the site
Ok thank you for taking the time
This comment was minimized by the moderator on the site
Hi Crystal,

Same as others in this thread, I'd like my signature to default to an external signature if there's any non-internal email addresses in the to or cc line and switch to an internal signature when it's only internal email addresses. To do so, I've combined your most updated code (response to Lelian) with Random_Guest's response to Daniela (defaulting to the external signature). The result is that when I'm only responding to internal email addresses, the signature works perfectly; however, when my external signature is pulled in, it drops the signature into the bottom of the email chain, not to the end of the email I'm sending. Can you please advise how to correct? I've included the code below:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/08/01
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
Dim xFindStr As String
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
Else
xRcpAddress = xRecipient.AddressEntry.Address
End If
If VBA.InStr(VBA.LCase(xRcpAddress), "@mycompany'sname") = 0 Then 'Enter the string in the double quotation marks. If the recipient email address contains this string, the below signature "internal.htm" will be assigned to the email. Otherwise, assign the signature "external.htm".
xSignatureFile = xSignaturePath & "External.htm"
Exit For
Else
xSignatureFile = xSignaturePath & "Internal.htm"
End If
Next
VBA.DoEvents
Set xDoc = xMailItem.GetInspector.WordEditor
xFindStr = "From: " & xMailItem.Recipients.Item(1).Name & " <" & xRcpAddress & ">"
If VBA.InStr(1, xMailItem.Body, xFindStr) <> 0 Then
xDoc.Application.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
With xDoc.Application.Selection.Find
.ClearFormatting
.Text = xFindStr
.Execute Forward:=True
End With
With xDoc.Application.Selection
.MoveLeft wdCharacter, 2
.InsertParagraphAfter
.MoveDown Unit:=wdLine, Count:=1
End With
Else
With xDoc.Application.Selection
.EndKey Unit:=wdStory, Extend:=wdMove
.InsertParagraphAfter
.MoveDown Unit:=wdLine, Count:=1
End With
End If
xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub
This comment was minimized by the moderator on the site
Hi Josh,
This problem is a bit complicated. I need time to find a solution. Just not able to handle it right now. Sorry for that.
This comment was minimized by the moderator on the site
No problem -- thanks for giving it a look!
This comment was minimized by the moderator on the site
Bonjour,

Quand je réponds à des mails, la signature automatique s'insère tout en bas, mais j'aimerais qu'elle s'insère en bas de mon message à moi.

Avez vous une solution ?

Lélian
This comment was minimized by the moderator on the site
Hi LÉLIAN ALEMPS.
The VBA code has been updated. Thank you for your feedback. Please give it a try.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/08/01
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
Dim xFindStr As String
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
    If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
        xRcpAddress = xRecipient.AddressEntry.Address
    End If
    Select Case xRcpAddress
        Case "Email Address 1"
            xSignatureFile = xSignaturePath & "aaa.htm"
            Exit For
        Case "Email Address 2", "Email Address 3"
            xSignatureFile = xSignaturePath & "bbb.htm"
            Exit For
        Case "Email Address 4"
            xSignatureFile = xSignaturePath & "ccc.htm"
            Exit For
    End Select
Next
VBA.DoEvents
Set xDoc = xMailItem.GetInspector.WordEditor
xFindStr = "From: " & xMailItem.Recipients.Item(1).Name & " <" & xRcpAddress & ">"
If VBA.InStr(1, xMailItem.Body, xFindStr) <> 0 Then
    xDoc.Application.Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    With xDoc.Application.Selection.Find
        .ClearFormatting
        .Text = xFindStr
        .Execute Forward:=True
    End With
    With xDoc.Application.Selection
        .MoveLeft wdCharacter, 2
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
Else
    With xDoc.Application.Selection
        .EndKey Unit:=wdStory, Extend:=wdMove
        .InsertParagraphAfter
        .MoveDown Unit:=wdLine, Count:=1
    End With
End If
xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub
This comment was minimized by the moderator on the site
Hi Crystal - I also have the same issue as Tim (#33997) and Greg (#34358) referenced above, but do not see a solution. The signature appears in my email in the last place I click before I hit 'send, thus often appearing in the middle of the email. Any help/solutions?

Thank you!

Eric
This comment was minimized by the moderator on the site
Hi Eric Anderson,
Thank you for your feedback. The code has now been updated and the issue has been solved. Please give it a try.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/6/24
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
    If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
        xRcpAddress = xRecipient.AddressEntry.Address
    End If
    Debug.Print xRcpAddress
    Select Case xRcpAddress
        Case ""
            xSignatureFile = xSignaturePath & "aaa.htm"
            Exit For
        Case "", ""
            xSignatureFile = xSignaturePath & "bbb.htm"
            Exit For
        Case ""
            xSignatureFile = xSignaturePath & "ccc.htm"
            Exit For
    End Select
Next
VBA.DoEvents
Set xDoc = xMailItem.GetInspector.WordEditor
xDoc.Application.Selection.EndKey Unit:=wdStory, Extend:=wdMove
xDoc.Application.Selection.InsertParagraphAfter
xDoc.Application.Selection.MoveDown Unit:=wdLine, Count:=1
xDoc.Application.Selection.InsertFile FileName:=xSignatureFile, Link:=False, Attachment:=False
End Sub
This comment was minimized by the moderator on the site
I have multiple email accounts setup on Outlook and have your script setup to email different signatures to the internal and external emails.

How can I modify the script so that it only sends these signatures if I'm sending from ?

In other words, I don't want to send these signature when I'm sending from an email address other than .

Thanks,
This comment was minimized by the moderator on the site
Hi Jeff Weaver,
The following VBA code has been modified to insert these signatures when sending emails only from a specified email account. Please give it a try. hope I can help.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2022/06/10
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
If xMailItem.SendUsingAccount.SmtpAddress <> "" Then Exit Sub 'The email account you send emails from
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
For Each xRecipient In xRecipients
    If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
        xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
        xRcpAddress = xRecipient.AddressEntry.Address
    End If
    Select Case xRcpAddress
        Case "Email Address 1"
            xSignatureFile = xSignaturePath & "aaa.htm"
            Exit For
        Case "Email Address 2", "Email Address 3"
            xSignatureFile = xSignaturePath & "bbb.htm"
            Exit For
        Case "Email Address 4"
            xSignatureFile = xSignaturePath & "ccc.htm"
            Exit For
    End Select
Next
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
This comment was minimized by the moderator on the site
Hello Crystal,
I have a question about the source code below.
I would like to send an internal signature only to certain email recipients (30), as soon as another email address is added, the external signature should be used.
Can you help me with my request?
Many thanks in advance.
This comment was minimized by the moderator on the site
This code worked for me until I restarted my PC.When I open alt+F11 again, all the code is still in the same place, but when I send an e-mail, it just sends it without a signature and without issuing any sort of warning message.
This comment was minimized by the moderator on the site
Hi Ivan,The problem is caused by Excel disabling the Macro option. You need to get into the Outlook Options window by clicking File > Options. In the Outlook Options window, click Trust Center in the left pane, and then click Trust Center Settings button. In the Trust Center window, click Macro Settings in the left pane, and then select the Enable all macros radio button and check the Apply macro security settings to installed add-ins box. See the attached screenshot below.
This comment was minimized by the moderator on the site
This script is great and functional for what I was looking for. Is it possible to have the code differentiate if the message is new or a reply as well as the email domain? For example to further select a separate signature for replies to external recipients vs a new message to external recipients?
Thank you for sharing.
This comment was minimized by the moderator on the site
Hi Seth,The code below differentiates whether the message is new or a reply to insert a signature. You need to manually change the "Email Address" and "Reply Email Address" and the corresponding signature names in the code.
<div data-tag="code">Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
'Updated by ExtendOffice 2020/12/24
Dim xMailItem As MailItem
Dim xRecipients As Recipients
Dim xRecipient As Recipient
Dim xRcpAddress As String
Dim xSignatureFile, xSignaturePath As String
Dim xFSO As Scripting.FileSystemObject
Dim xDoc As Document
On Error Resume Next
Set xFSO = New Scripting.FileSystemObject
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
Set xRecipients = xMailItem.Recipients
xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"
If InStr(xMailItem.Subject, "RE: ") <> 1 Then
For Each xRecipient In xRecipients
If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
Else
xRcpAddress = xRecipient.AddressEntry.Address
End If
Select Case xRcpAddress
Case "Email Address 1"
xSignatureFile = xSignaturePath & "aaa.htm"
Exit For
Case "Email Address 2", "Email Address 3"
xSignatureFile = xSignaturePath & "bbb.htm"
Exit For
Case "Email Address 4"
xSignatureFile = xSignaturePath & "ccc.htm"
Exit For
End Select
Next
Else
For Each xRecipient In xRecipients
If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
Else
xRcpAddress = xRecipient.AddressEntry.Address
End If
Select Case xRcpAddress
Case "b"
xSignatureFile = xSignaturePath & "111.htm" '111.htm is the name of the signature you will insert when reply to the "Reply Email Address 1"
Exit For
Case "Reply Email Address 2", "Reply Email Address 3"
xSignatureFile = xSignaturePath & "222.htm"
Exit For
Case "Reply Email Address 4"
xSignatureFile = xSignaturePath & "333.htm"
Exit For
End Select
Next
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
This comment was minimized by the moderator on the site
Great script. Thank you.Any way to insert the signature before hitting send to preview. I know I can delay send and look at it in outbox. Currently it doesn't appear until I hit send. If not is there any software that will automatically assign a signature based on Contact. We have been using a program for many years that worked great but it does not work in new versions of Outlook.
This comment was minimized by the moderator on the site
Hello Crystal,

I am interested in the VBA code you wrote for "pauli" below, but when I run it, the following error is generated (and the "XDoc as Document" code line is highlighted):

"Compile error: User-defined type not defined"

How can I resolve this issue please?

Thank you!
This comment was minimized by the moderator on the site
Hi Tim,Before applying the code, please go to the References dialog to check the Microsoft Word Object Library box (as the attached image shown).
This comment was minimized by the moderator on the site
Hello Crystal,
I am interested in the VBA code you wrote for "pauli" below, but when I run it, the following error is generated (and the "XDoc as Document" code line is highlighted):
"Compile error: User-defined type not defined"
How can I resolve this issue please?

Thank you, Tim
This comment was minimized by the moderator on the site
Tim I have same issue. Wherever the user clicks last is where the image is inserted. Does anyone have a way to force the image just above the signature?
This comment was minimized by the moderator on the site
I have some weird behaviour with VBA-generated Outlook emails. The signature is added to the mail as intended, but not positioned in the bottom of the email but rather in the middle (looks like at the first empty space). Any idea why and how to overcome it?
This comment was minimized by the moderator on the site
Hi, I would like to apply this to differentiate signatures when sending internal and external emails. Therefore instead of recognizing specific email addresses I'd need to just differentiate by the recipient email address containing my firm's name inside it or not. Could you let me know how the code would be for this specific case?


(For example if i wanted to sign with signature "internal.htm" when the recipient email contains the string "microsoft" and signature "external.htm" if it doesn't contain the string "microsoft". In this case addresses like '', '' and '' would all be considered like the internal recipients for a Microsoft employee).

Thanks!!
This comment was minimized by the moderator on the site
Hi pauli,
Please try the below code. Before applying the code, please go to the References dialog to check the Microsoft Word Object Library box (as the attached image shown).

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

'Updated by ExtendOffice 2020/6/12

Dim xMailItem As MailItem

Dim xRecipients As Recipients

Dim xRecipient As Recipient

Dim xRcpAddress As String

Dim xSignatureFile, xSignaturePath As String

Dim xFSO As Scripting.FileSystemObject

Dim xDoc As Document

On Error Resume Next

Set xFSO = New Scripting.FileSystemObject

If Item.Class <> olMail Then Exit Sub

Set xMailItem = Item

Set xRecipients = xMailItem.Recipients

xSignaturePath = CreateObject("WScript.Shell").SpecialFolders(5) + "\Microsoft\Signatures\"

For Each xRecipient In xRecipients

If xRecipient.AddressEntry.AddressEntryUserType = olExchangeUserAddressEntry Then

xRcpAddress = xRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress

Else

xRcpAddress = xRecipient.AddressEntry.Address

End If

If VBA.InStr(VBA.LCase(xRcpAddress), "@microsoft") > 0 Then 'Enter the string in the double quotation marks. If the recipient email address contains this string, the below signature "internal.htm" will be assigned to the email. Otherwise, assign the signature "external.htm".

xSignatureFile = xSignaturePath & "internal.htm"

Exit For

Else

xSignatureFile = xSignaturePath & "external.htm"

End If

Next

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
This comment was minimized by the moderator on the site
Hi, Crystal,I have a question. When I am sending emails to both external and internal recipients how can I make it differentiate by choosing always external signature? Thanks
This comment was minimized by the moderator on the site
You might have found the solution for yourself by now or abandoned this thread long time ago, but I feel the urge to finish this now. The simple answer to this question:
Edit the If-Else-Clause (that differentiates between the two signatures) to the following:
If VBA.InStr(VBA.LCase(xRcpAddress), "@microsoft") = 0 Then 'Enter the string in the double quotation marks. If the recipient email address contains this string, the below signature "internal.htm" will be assigned to the email. Otherwise, assign the signature "external.htm".
xSignatureFile = xSignaturePath & "external.htm"
Exit For
Else
xSignatureFile = xSignaturePath & "internal.htm"
End If

What happens now:
If the recipient address from a list of recipient addresses does NOT contain the given string, use the external signature and stop looking at further recipients. Otherwise use the internal signature and look for the next recipient address.
This comment was minimized by the moderator on the site
Hi Crystal,

what did you change to fix the images issue? I am using your latest code and I have the same problem as Amanda.
Thanks
This comment was minimized by the moderator on the site
Besides the code, the operation of step 7 has also changed. Please follow the instruction step by step to get it down.
This comment was minimized by the moderator on the site
Hi,
Sorry for the mistake. The VBA has been updated again, and the images issue is totally fixed now.
This comment was minimized by the moderator on the site
Very nice script but the image files in my signature are not delivered right. Can you fix this issue ?
This comment was minimized by the moderator on the site
Hi Vysakh,
The code has been updated, and the images issue is fixed now. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
I added this clever script to Outlook 2013 and it correctly identifies and selects the different email signatures I use.

I have a problem with one of the graphics that is part of one signature. Instead of the graphic being shown the "Sent Items" folder (and recipient) shows the email with the attached screen shot and trying to download the image does not work.

If I disable the script and sign manually then the outgoing email is correct and the recipient gets what I intend. Even more interesting is that with another simpler signature where the graphic is just a straight line this is included albeit that the graphic is changed slightly.

The graphic is a PNG file 80KB 5904 x 1024 pixels with a bit depth of 32 and I have tried smaller sizes down to 10KB 369 x 64 pixels which has not helped. My Outlook version is 15.0.5189.1000 32Bit Professional Plus 2013 on a Windows 10 Pro platform.

I wonder if you can suggest a solution for this please.
This comment was minimized by the moderator on the site
Dear Amanda,
We have updated the code. Thank you for reminding me the error.
This comment was minimized by the moderator on the site
Hello! Very nice script, but there is a problem when sending to excahnge addresses, the xRcpAddress returns the X400 name not the smtp address, this makes it impossible to select based on domain. Is there a workaround this?


Just to improve I have changed the case statement to if using the inStr function to discern mails in bulk

If InStr(xRcpAddress, "@example") Then
xSignatureFile = xSignaturePath & "aaa.htm"
End If
This comment was minimized by the moderator on the site
How would this behave if there are multiple recipients?
This comment was minimized by the moderator on the site
Hi Devansh,
If there are multiple recipients in an email, the code only works for the first one. And all recipients will receive the email with the same signature specified to that person.
If you want to incude different signatures when there are multiple recipients, the email should be sent separately to different recipients. And that will need another code to achieve.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations