none
How to Hook Word Context Menu "Paste - Keep Source Formatting" RRS feed

  • Question

  • Hello, All -

    I've done some digging through the forums and docs, but haven't found what I need for this one. In short, how do I hook/catch the context menu "Paste - Keep Source Formatting" button? This is what I've done:


    I'm running VS2013 Community, writing a VSTO Word Add-In using C#, and my development machine is Win 8.x running Office 2013. In short, I am trying to notice/hook cut & paste operations. I am able to catch the Keyboard (Ctrl-C/Ctrl-V) and Ribbon Button usage using Ribbon XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <commands>
        <command idMso="Copy" onAction="OnButtonCopy"/>
        <command idMso="Cut" onAction="OnButtonCut"/>
        <command idMso="Paste" onAction="OnButtonPaste"/>
        <command idMso="PasteSourceFormatting" onAction="OnPasteSourceFormatting"/>
      </commands>
    ... etc.

    For discussion, it's safe to assume my onAction functions all just issue a MessageBox.Show("Handler foo has run."), like:

    public void OnButtonPasteSourceFormatting(Office.IRibbonControl control, ref bool CancelDefault)
            {
                MessageBox.Show("Caught context menu Paste Keep Formatting");
                CancelDefault = false;
                return;
            }


    My onAction functions are all triggered as expected ... except when the user issues a paste using the right-click context menu (right click, then "Paste - Keep Source Formatting"). In this case, I just can't catch the paste. Maybe I just have the wrong idMso, or maybe this is altogether not possible? All the other <command ...> statements seem to work fine, so I'm a bit lost as to what I'm doing wrong.


    Thanks for whatever advice you can provide.


    DLT

    Sunday, July 23, 2017 11:42 AM

Answers

  • Hello,

    The command PasteSourceFormatting doesn't work for me with Office 2016 and there is no ui error.

     "Paste - Keep Source Formatting" should be in the PasteGallery (in the ribbon) or PasteGalleryMini (in the context menu). Unfortunately, we are unable to customize the gallery. You could visit the similar thread: Intercepting Word 2010 Paste Commands is not as easy as in Word 2007

    If you want to customize all the Copy/Paste/Paste Special buttons or commands, I suggest you hide built-in ribbon and gallery and create your own buttons.

    E.g.

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <commands>
        <command idMso="Copy" onAction="OnButtonPasteSourceFormatting"/>
        <command idMso="Cut" onAction="OnButtonPasteSourceFormatting"/>
        <command idMso="Paste" onAction="OnButtonPasteSourceFormatting"/>
      </commands>
      <ribbon>
        <tabs>
          <tab idMso="TabHome">
            <group idMso="GroupClipboard" visible="false">
            </group>
          </tab>
        </tabs>
      </ribbon>
    <contextMenus>
      <contextMenu idMso="ContextMenuText" >
        <gallery idMso="PasteGalleryMini" visible="false">
        </gallery>
      </contextMenu>
    </contextMenus>
    </customUI>

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 24, 2017 3:32 AM
    Moderator

All replies

  • Hello Denton,

    Do you get any UI errors? 

    By default, if an VSTO Add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom Ribbon does not appear, or why a Ribbon appears but no controls appear. Read more about that in the How to: Show Add-in User Interface Errors article. 

    Also take a look at the following documents for control IDs:

    Office 2016 Help Files: Office Fluent User Interface Control Identifiers

    Office 2013 Help Files: Office Fluent User Interface Control Identifiers

    Office 2010 Help Files: Office Fluent User Interface Control Identifiers


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, July 23, 2017 6:48 PM
  • Hello,

    The command PasteSourceFormatting doesn't work for me with Office 2016 and there is no ui error.

     "Paste - Keep Source Formatting" should be in the PasteGallery (in the ribbon) or PasteGalleryMini (in the context menu). Unfortunately, we are unable to customize the gallery. You could visit the similar thread: Intercepting Word 2010 Paste Commands is not as easy as in Word 2007

    If you want to customize all the Copy/Paste/Paste Special buttons or commands, I suggest you hide built-in ribbon and gallery and create your own buttons.

    E.g.

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <commands>
        <command idMso="Copy" onAction="OnButtonPasteSourceFormatting"/>
        <command idMso="Cut" onAction="OnButtonPasteSourceFormatting"/>
        <command idMso="Paste" onAction="OnButtonPasteSourceFormatting"/>
      </commands>
      <ribbon>
        <tabs>
          <tab idMso="TabHome">
            <group idMso="GroupClipboard" visible="false">
            </group>
          </tab>
        </tabs>
      </ribbon>
    <contextMenus>
      <contextMenu idMso="ContextMenuText" >
        <gallery idMso="PasteGalleryMini" visible="false">
        </gallery>
      </contextMenu>
    </contextMenus>
    </customUI>

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 24, 2017 3:32 AM
    Moderator
  • Thanks for this, Eugene.

    Nope, no UI errors coming up. I already have the UI error messages turned on - setting the idMso to a nonexistent label (idMso="foobar") gives me the appropriate error message when I open a new document.

    Thanks for the Fluent doc links, but yep, already have those. That's where I found the idMso tags I'm using.

    Cheers ~

    DLT

    Monday, July 24, 2017 12:02 PM

  •  "Paste - Keep Source Formatting" should be in the PasteGallery (in the ribbon) or PasteGalleryMini (in the context menu). Unfortunately, we are unable to customize the gallery. You could visit the similar thread: Intercepting Word 2010 Paste Commands is not as easy as in Word 2007

    If you want to customize all the Copy/Paste/Paste Special buttons or commands, I suggest you hide built-in ribbon and gallery and create your own buttons.

     

    Thanks for providing this, Celeste - I hadn't caught that these context menu buttons were inside another container (the PasteGalleryMini). Clearly, this isn't as straightforward as catching the Ribbon buttons.

    For my own case, disabling the gallery and then re-coding a PasteGalleryMini clone is a bit overkill - really, I'm just setting a flag when a Cut/Paste/Copy action occurs. If I do end up following this pathway, I may post a summary of my code for others. It seems like a lot of work for very little benefit, but perhaps necessary.

    Cheers ~

    DLT

    Monday, July 24, 2017 12:16 PM
  • Hello,

    Do you have any other issue about this thread?

    If you don't have any issue, I suggest you mark posts as answer to close this thread.

    If you have any new issues when customizing, please feel free to post new threads.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 25, 2017 8:47 AM
    Moderator