The user wanted to know how to insert an emoji using VBA:
Is there a way to create a VBA code to paste an emoji. For example, Unicode 128123? :ghost:
I want to insert the text where ever my cursor currently is.
Sure, you can do that using VBA. You'll need to use the Word Object model to insert the text in the cursor position. You can use this method to insert emoji, symbols, or text.
For emoji, you'll need to use ChrW and the UTF-16 code (which you can find at many sites online, I used The Ultimate Emoji Guide). You'll replace the 0x in the UTF-16 code with &H, so
ChrW(&HD83D) & ChrW(&HDC7B)
To use this macro, customize the ribbon to add the button. Click in a message you are composing and run the macro.
Sub InsertText() Dim objItem As Object Dim objInsp As Outlook.Inspector Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection Set objItem = Application.ActiveInspector.currentItem Set objInsp = objItem.GetInspector Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection ' https://emojiguide.org/ghost ' Ghost in UTF-16 hex is 0xd83d 0xdc7b ' 0x is replaced with &H objSel.Text = ChrW(&HD83D) & ChrW(&HDC7B) ' to deselect the insertion point objSel.Move wdCharacter, 1 Set objItem = Nothing Set objInsp = Nothing Set objDoc = Nothing Set objWord = Nothing Set objSel = Nothing End Sub
Use with multiple emoji
If you want to create a set of emoji or phrases to insert, you can use a macro to set the variable that contains the code or phrase then call the InsertText macro.
Dim strCode As String Sub InsertLeaf() strCode = ChrW(&HD83C) & ChrW(&HDF41) InsertText End Sub Sub InsertGhost() strCode = ChrW(&HD83D) & ChrW(&HDC7B) InsertText End Sub Sub InsertPhrase() strCode = "This is only a test!!!! " InsertText End Sub Private Sub InsertText() Dim objItem As Object Dim objInsp As Outlook.Inspector Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection Set objItem = Application.ActiveInspector.currentItem Set objInsp = objItem.GetInspector Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection objSel.InsertBefore strCode objSel.Move wdCharacter, 1 Set objItem = Nothing Set objInsp = Nothing Set objDoc = Nothing Set objWord = Nothing Set objSel = Nothing End Sub
Insert into a new message
If you want to insert it at the beginning or end of a new message or reply, you can insert it into the HTML body using the HTML code for the symbol, You can also insert it in the subject.
Public Sub New_Mail() Dim olNS As Outlook.NameSpace Dim oMail As Outlook.MailItem Set olNS = Application.GetNamespace("MAPI") Set oMail = Application.CreateItem(olMailItem) With oMail .Subject = ChrW(&HD83C) & ChrW(&HDF41) & " This is the subject " & ChrW(&HD83C) & ChrW(&HDF41) .BodyFormat = olFormatHTML .HTMLBody = "👻" & VBCrLF & .HTMLBody .Display End With Set oMail = Nothing Set olNS = Nothing End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page should be placed in a module.
The macros on this page need to go into ThisOutlookSession.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.
- Go to Tools, References menu.
- Locate the Word object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
More information as well as screenshots are at How to use the VBA Editor