none
Don't Keep Content Controls RRS feed

  • Question

  • Hi,

    When copying all the text from one Content Control and pasting into another, by default the content control pasted as well. I want to stop the content control from being pasted. Changing the default is acceptable for me. Is there a way to change the default for “Keep Content Controls” in the paste options?

     

    Failing that my destination document is a VSTO document and the user will be pasting into a host control, is there a way to capture the paste event and remove the content control(s) that way? 

     

    Thanks

    Ellis

    Thursday, July 7, 2011 12:52 AM

Answers

  • Thanks again to Lupu and Cindy for pointing me in the right direction.

    I tried both suggestions and thought I'd post them here for anybody wanting to do something similar.

     

    The first and what turned out to be my preferred option is to use the ContentControlAfterAdd event to capture when a control is being added, and if it’s not my code adding the control then delete the control.

     

    In ThisDocument add this…

        Private Sub ThisDocument_ContentControlAfterAdd(ByVal NewContentControl As icrosoft.Office.Interop.Word.ContentControl, ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

            ' insertControl gets set to True when I want to dynamically add a control

            If Not insertControl Then

                NewContentControl.LockContentControl = False

                NewContentControl.LockContents = False

                NewContentControl.Delete(False)

            End If

        End Sub

     

     

    The second option was to Repurpose the Paste event, note that is was not fully tested.

    Using the Ribbon xml to override the Paste event I simply get the rich text from the clipboard, clear the clipboard and then add the rich text back to the clipboard, it was a bit of hack for me to use but maybe useful to somebody.

        Public Sub myPaste(ByVal control As IRibbonControl, ByRef cancelDefault As Boolean)

            Dim rtf As Object = Clipboard.GetDataObject.GetData("Rich Text Format")

     

            Clipboard.Clear()

            Clipboard.SetData("Rich Text Format", rtf)

     

            Globals.ThisDocument.Application.Selection.Paste()

     

            cancelDefault = True

        End Sub


    • Edited by Ellis Miller Friday, July 22, 2011 4:02 AM Added thank you
    • Marked as answer by Ellis Miller Friday, July 22, 2011 4:02 AM
    Friday, July 22, 2011 4:01 AM
  • Hello Ellis,

     

    What you can do is use the event ContentControlAfterAdd event on the application, and somehow (using a title/tag) to identify the contentcontrol to know that this si caused by Copy and Paste, and then MessageBox.Show(question).

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    • Marked as answer by Ellis Miller Wednesday, July 13, 2011 8:54 PM
    Tuesday, July 12, 2011 5:42 AM
  • Hi Ellis

    Lupu has suggested one method.

    Taking your question literally, you can repurpose almost any command in an office application by providing a Ribbon XML as part of the VSTO solution. It has to be Ribbon XML and not the Designer because the Designer doesn't support anything except the Ribbon controls.

    What you'd need is a <commands> section in the <ribbon> section, with one <command> for every built-in command you want to re-purpose. You specify the idMso for the command for that attribute and assign the code you want to have run to the onAction attribute.

    You can download the Office 2010 Control IDs here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&clcid=0x411&id=6627

    More information about working with the Ribbon XML:
    http://msdn.microsoft.com/en-us/office/aa905530.aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Ellis Miller Wednesday, July 13, 2011 8:54 PM
    Tuesday, July 12, 2011 8:32 AM
    Moderator

All replies

  • Hi Ellis

    As far as I know, there's no option for "keeping content controls" (or not) when copying/pasting. This has come up before, see:

    http://social.msdn.microsoft.com/Forums/en-IE/worddev/thread/c6b7b92a-e914-453d-82b9-2d6fc3de88bb


    Cindy Meister, VSTO/Word MVP
    Thursday, July 7, 2011 9:56 AM
    Moderator
  • Hi Cindy, thanks for your reply

    I had read three of Lauro2's posts, but just wanted confirmation that the "Keep Content Controls" default couldn't be changed.

    I have also tried repurposing the copy paste events, it works in vba but not in VSTO, so is there a way to capture the paste event in VSTO?

    Thanks

    Friday, July 8, 2011 4:51 AM
  • <<is there a way to capture the paste event in VSTO?>>

    Which version of Office are we discussing?


    Cindy Meister, VSTO/Word MVP
    Monday, July 11, 2011 11:00 AM
    Moderator
  • Hi Cindy, sorry for the lack of info, I'm using a Word 2010 document with VSTO40, I don't at this point have SP1 for office but would consider it if it would help.

    Thanks

    Tuesday, July 12, 2011 12:51 AM
  • Hello Ellis,

     

    What you can do is use the event ContentControlAfterAdd event on the application, and somehow (using a title/tag) to identify the contentcontrol to know that this si caused by Copy and Paste, and then MessageBox.Show(question).

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    • Marked as answer by Ellis Miller Wednesday, July 13, 2011 8:54 PM
    Tuesday, July 12, 2011 5:42 AM
  • Hi Ellis

    Lupu has suggested one method.

    Taking your question literally, you can repurpose almost any command in an office application by providing a Ribbon XML as part of the VSTO solution. It has to be Ribbon XML and not the Designer because the Designer doesn't support anything except the Ribbon controls.

    What you'd need is a <commands> section in the <ribbon> section, with one <command> for every built-in command you want to re-purpose. You specify the idMso for the command for that attribute and assign the code you want to have run to the onAction attribute.

    You can download the Office 2010 Control IDs here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&clcid=0x411&id=6627

    More information about working with the Ribbon XML:
    http://msdn.microsoft.com/en-us/office/aa905530.aspx


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Ellis Miller Wednesday, July 13, 2011 8:54 PM
    Tuesday, July 12, 2011 8:32 AM
    Moderator
  • Thanks for the suggestion Lupu I'll try it out.
    Wednesday, July 13, 2011 8:52 PM
  • Thanks for the Suggestion Cindy, I'll try give it a go.
    Wednesday, July 13, 2011 8:54 PM
  • Thanks again to Lupu and Cindy for pointing me in the right direction.

    I tried both suggestions and thought I'd post them here for anybody wanting to do something similar.

     

    The first and what turned out to be my preferred option is to use the ContentControlAfterAdd event to capture when a control is being added, and if it’s not my code adding the control then delete the control.

     

    In ThisDocument add this…

        Private Sub ThisDocument_ContentControlAfterAdd(ByVal NewContentControl As icrosoft.Office.Interop.Word.ContentControl, ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

            ' insertControl gets set to True when I want to dynamically add a control

            If Not insertControl Then

                NewContentControl.LockContentControl = False

                NewContentControl.LockContents = False

                NewContentControl.Delete(False)

            End If

        End Sub

     

     

    The second option was to Repurpose the Paste event, note that is was not fully tested.

    Using the Ribbon xml to override the Paste event I simply get the rich text from the clipboard, clear the clipboard and then add the rich text back to the clipboard, it was a bit of hack for me to use but maybe useful to somebody.

        Public Sub myPaste(ByVal control As IRibbonControl, ByRef cancelDefault As Boolean)

            Dim rtf As Object = Clipboard.GetDataObject.GetData("Rich Text Format")

     

            Clipboard.Clear()

            Clipboard.SetData("Rich Text Format", rtf)

     

            Globals.ThisDocument.Application.Selection.Paste()

     

            cancelDefault = True

        End Sub


    • Edited by Ellis Miller Friday, July 22, 2011 4:02 AM Added thank you
    • Marked as answer by Ellis Miller Friday, July 22, 2011 4:02 AM
    Friday, July 22, 2011 4:01 AM
  • Thank you for posting the results of your research :-) Since the question came up twice already in this forum, it's certain to come up again and this will be a useful resource!
    Cindy Meister, VSTO/Word MVP
    Friday, July 22, 2011 7:59 AM
    Moderator