View Full Version : [SOLVED:] ToggleButton for Smart Quotes
wb2025
02-11-2025, 07:49 AM
I'd like to make a toggleButton on the ribbon in Word to turn Smart Quotes on/off and indicate whether they're on/off. Seems like it shouldn't be too complicated, but I've spent hours trying to figure it out. I'm familiar with RibbonX and VBA but I'm missing something. Thanks for your help.
Aussiebear
02-11-2025, 01:01 PM
Whilst we wait for The Word guru's to float by, maybe something like this:
Sub CreateSmartQuotesToggleButton()
' Get the Ribbon object
Dim objRibbon As IRibbonUI
' Check if the custom tab already exists. If so, exit.
' This prevents adding the tab multiple times.
On Error Resume Next
' Handle potential error if tab doesn't exist
Application.CommandBars("Ribbon").FindControl(ID:="SmartQuotesTab")
If Err.Number = 0 Then
' Tab already exists On Error GoTo 0
' Reset error handling
Exit Sub
End If
On Error GoTo 0
' Reset error handling
' Create the custom Ribbon tab
Set objRibbon = Application.CommandBars("Ribbon")
With objRibbon.Controls.Add(Type:=msoControlCustom, ID:="SmartQuotesTab")
.Caption = "Smart Quotes"
' Tab caption .Visible = True
' Create the toggle button within the tab
With .Controls.Add(Type:=msoControlToggleButton, ID:="SmartQuotesToggle")
' Button caption .
.Caption = "Smart Quotes"
' Tooltip
.Description = "Toggle Smart Quotes on/off"
' Procedure to execute on click
.OnAction = "ToggleSmartQuotes"
' Procedure to get the button's pressed
.OnGetPressed = "GetSmartQuotesToggleState"
' Procedure to get the button's pressed state
.ImageMso = "Symbol"
' Choose an appropriate icon (or create a custom one) - "Symbol" is a placeholder. Look up msoImageMso values for options.
.Visible = True
End With
End With
' Invalidate the Ribbon to reflect changes
objRibbon.Invalidate
' Initialize the Smart Quotes state and the button state
bSmartQuotesEnabled = Application.AutoCorrect.AutoFormatAsYouTypeReplaceQuotes
' Update the button state
objRibbon.InvalidateControl "SmartQuotesToggle"
End Sub
' Callback procedure for the toggle button's OnAction event
Sub ToggleSmartQuotes(control As IRibbonControl, pressed As Boolean)
' Update global variable
bSmartQuotesEnabled = pressed
Application.AutoCorrect.AutoFormatAsYouTypeReplaceQuotes = pressed Application.AutoCorrect.AutoFormatReplaceQuotes = pressed
' Important: Set both!
' Optionally, provide visual feedback (e.g., a message box or status bar update)
' MsgBox "Smart Quotes are now " & IIf(pressed, "On", "Off") <=== ' Remove Rem if required
' Invalidate the control to update the button's appearance if needed.
' In most cases this will not be necessary.
' Application.CommandBars("Ribbon").InvalidateControl "SmartQuotesToggle"
End Sub
' Callback procedure for the toggle button's OnGetPressed event
Sub GetSmartQuotesToggleState(control As IRibbonControl, pressed As Boolean)
' Set the button's pressed state based on the global variable
pressed = bSmartQuotesEnabled
End Sub
' Procedure to run when the document opens (or when the add-in is loaded)
Sub AutoOpen()
' Create the button if it doesn't exist
CreateSmartQuotesToggleButton
End Sub
' Procedure to run when the add-in is loaded
Sub AddInInitialize()
' Create the button if it doesn't exist
CreateSmartQuotesToggleButton
End Sub
Paul_Hossler
02-11-2025, 02:05 PM
The easiest was I think would be to use Options, Customize Ribbon and run the macro
This doesn't require any CustomXML, but not as polished
31887
31886
Option Explicit
Sub ToggleSQ()
With Options
If .AutoFormatAsYouTypeReplaceQuotes Then
.AutoFormatAsYouTypeReplaceQuotes = False
MsgBox "Smart Quotes turned OFF"
Else
.AutoFormatAsYouTypeReplaceQuotes = True
MsgBox "Smart Quotes turned ON"
End If
End With
End Sub
Aussiebear
02-11-2025, 02:45 PM
Sigh.... I'm so '70's:(
Paul_Hossler
02-11-2025, 03:00 PM
Here's a more 'polished version' that uses custom XML
The toggle buttom stays pressed or unpressed
31890
wb2025
02-11-2025, 05:58 PM
Thank you!!! That works!
Your nice quotes icon gave me another idea. Could we have the icon change from curly quote to straight quote when we toggle?
Paul_Hossler
02-12-2025, 11:10 AM
31891
I couldn't get the toggleButton to handle getImage so I used two buttons and getVisible to hide/unhide
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnLoad">
<ribbon startFromScratch="false">
<tabs>
<tab idMso="TabHome">
<group id="grpSQ" insertAfterMso = "GroupFont">
<button id="btnSQ"
size="large"
label="Straight Quotes"
onAction="btnAction"
image = "imgRQ"
getVisible = "btnVisible"
screentip="Turn on Straight Quotes"/>
<button id="btnRQ"
size="large"
label="Smart Quotes"
onAction="btnAction"
image="imgSQ"
getVisible = "btnVisible"
screentip="Turn on Smart Quotes"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Billo
02-17-2025, 05:53 PM
To add a toggle button for Smart Quotes in Word, you'll need to use RibbonX with a bit of VBA to change the settings. Here’s a simple outline of what you can do:
In RibbonX, add a toggle button to your ribbon:
xml
<ribbon>
<tabs>
<tab id="customTab" label="Custom Tab">
<group id="customGroup" label="Smart Quotes">
<toggleButton id="toggleSmartQuotes" label="Smart Quotes" onAction="ToggleSmartQuotes" />
</group>
</tab>
</tabs>
</ribbon>
Then, in your VBA code, write a subroutine to toggle the setting:
Sub ToggleSmartQuotes(control As IRibbonControl)
Dim currentSetting As Boolean
currentSetting = Application.Options.UseSmartQuotes
' Toggle the smart quotes setting
Application.Options.UseSmartQuotes = Not currentSetting
' Update the toggle button to reflect the new state
If Application.Options.UseSmartQuotes Then
control.Label = "Smart Quotes: On"
Else
control.Label = "Smart Quotes: Off"
End If
End Sub
To indicate whether Smart Quotes are on or off, you'll need to dynamically update the label or the button icon depending on the state. The code checks the current setting and updates the button accordingly.
This should give you a working toggle button for Smart Quotes. You can also adjust the logic to better fit your needs.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.