PowerPoint VBA技术问询:如何用宏插入FontAwesome图标
Fixing VBA Code to Insert FontAwesome Icons in PowerPoint
Your code is on the right track, but there’s one critical issue preventing it from working correctly: the CharNumber parameter expects a numeric decimal value corresponding to the Unicode character, not the hex string (like "f001"). Here’s how to fix it and make it work reliably:
Corrected VBA Code
Sub InsertFontAwesomeIcon() Dim txtBox As Shape Dim hexUnicode As String Dim decimalUnicode As Long ' Define the FontAwesome hex Unicode (e.g., f001 is the "music" icon) hexUnicode = "f001" ' Convert hex string to decimal (required for InsertSymbol) decimalUnicode = CLng("&H" & hexUnicode) ' Add a text box to the first slide Set txtBox = Application.ActivePresentation.Slides(1).Shapes.AddTextbox( _ Orientation:=msoTextOrientationHorizontal, _ Left:=100, Top:=100, Width:=100, Height:=100) ' Insert the FontAwesome icon into the text box With txtBox.TextFrame.TextRange .InsertSymbol FontName:="FontAwesome", CharNumber:=decimalUnicode, Unicode:=msoTrue ' Optional: Adjust font size to make the icon more visible .Font.Size = 48 End With End Sub
Key Fixes & Explanations
- Hex to Decimal Conversion: The
CLng("&H" & hexUnicode)line converts the hex string (like "f001") to its decimal equivalent (40961 in this case). PowerPoint’sInsertSymbolneeds this numeric value to locate the correct Unicode character. - Font Name Accuracy: Double-check that the font name matches exactly what’s installed on your system. For newer FontAwesome versions, this might be "FontAwesome 5 Free" or "FontAwesome 5 Solid"—confirm the name via your system’s Fonts control panel.
- Optional Font Size Adjustment: Added a line to boost the font size since FontAwesome icons are often too small at default settings.
Troubleshooting Tips
If the icon doesn’t appear (you see a square or placeholder):
- Verify FontAwesome is installed on your computer.
- Ensure the font name in the code matches the installed font’s exact spelling (case-insensitive, but typos will break it).
- Confirm the hex Unicode is valid for your installed FontAwesome version—some icons change IDs between releases.
Content of the question originates from Stack Exchange, asked by Lalit k
相关产品推荐
相关产品推荐

