Consulting

Results 1 to 8 of 8

Thread: ToggleButton for Smart Quotes

  1. #1
    VBAX Newbie
    Joined
    Feb 2025
    Posts
    2
    Location

    ToggleButton for Smart Quotes

    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.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,327
    Location
    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
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    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

    Capture1.JPG

    Capture.JPG

    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,327
    Location
    Sigh.... I'm so '70's
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    Here's a more 'polished version' that uses custom XML

    The toggle buttom stays pressed or unpressed


    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Newbie
    Joined
    Feb 2025
    Posts
    2
    Location
    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?

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    Capture.JPG

    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>
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Banned VBAX Newbie
    Joined
    Feb 2025
    Posts
    1
    Location
    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:

    1. In RibbonX, add a toggle button to your ribbon:

    xml

    HTML Code:
    <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>
    1. 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
    1. 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.
    Last edited by Aussiebear; 02-17-2025 at 08:21 PM. Reason: Removed the spam links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •