none
C# - PictureContentControl delete problems RRS feed

  • Question

  • I'm currently working in a VSTO Word Addin. Basics:
    - A word document is generated through openXML and a template
    - Not all fields are allways filled up from the xml stream, so we created an addin to help the user fill up the remaining fields.
    - The document should be saved on a different location in the fully filled in state
    - In that document the contentcontrols should also be removed, but the content should remain.
    So currently i'm at the last step, by using following code I loop over every contentcontrol and delete it.
        public static void ClearContentControls(Document doc)
    	    {
                // loop over every contentontrol and delete it, but keep the content
                foreach (var cc in doc.ContentControls.OfType<ContentControl>())
                {
                    // Delete(true) to also delete the content
                    cc.Delete();
                }
    	    }
    


    Every content deletes itself properly (leaving the content in place), except the pictureContentControl. This one also deletes it content for some strange reason.
    Anybody has any experience with this? And how did you solve it?
    EDIT:
    It appears that when I call delete on the picturecontentcontrol it deletes the whole paragraph which contains the control. While with the RichtTextContentControl and PlainTextContentControl it doesn't...
    Any solution to this?


    • Edited by KyorCode Wednesday, October 12, 2011 1:51 PM
    Wednesday, October 12, 2011 1:29 PM

All replies

  • Hi KyorCode,

     

    Thanks for posting in the MSDN Forum.

     

    I try your snippet on my side, it works fine. My snippet has a little different than yours. Please check whether my snippet will work on your side.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordAddIn3
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document wdDoc = Globals.ThisAddIn.Application.ActiveDocument;
                foreach (var cc in wdDoc.ContentControls.OfType<Word.ContentControl>())
                {
                    cc.Delete();
                }
            }
        }
    }
    

    By the way, would you please tell me your Word version and VSTO runtime version? On my side, I use Word 2010, VSTO 4.0.

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 14, 2011 3:12 AM
    Moderator
  • Hi KyorCode,

    I'm glad to hear you have solved your issue. This snippet I shown work fine on my side (Visual Studio 2010 (x86), Office 2010 (x86)). Thanks for sharing your experience here.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 18, 2011 6:18 AM
    Moderator