none
Shape still on the Word Document surface when I deleted them in Word 2013 RRS feed

  • Question

  • Hi All,

    In my application, I insert some custom controls in Word Document. It works fine. Now I deleted them via C# code. Indeed, the custom controls have been removed from Word Document surface. But they still display on  the Word Document surface.

    The code regarding insert custom control in Word:

          var cs = vstoDoc.Controls.AddControl(CustomControl, range, width, height, controlName);
    
          cs.Name = controlName;

    The code regarding delete custom control in Word:

    	try
    	{
    		spliter.Control.Delete();
    	}
    	catch
    	{
    		foreach (Word.InlineShape inlineShape in Document.InlineShapes)
    		{
    			var shapeName = string.Empty;
    
    			if (inlineShape.Type != Word.WdInlineShapeType.wdInlineShapeOLEControlObject)
    			{
    				continue;
    			}
    
    			try
    			{
    				dynamic obj = inlineShape.OLEFormat.Object;
    
    				shapeName = obj.Name;
    			}
    			catch 
    			{
    				continue;
    			}
    
    			if (spliter.Name.Is(shapeName))
    			{
    				inlineShape.Delete();
    
    				var vstoDoc = Globals.Factory.GetVstoObject(Document);
    
    				try
    				{
    					vstoDoc.Controls.Remove(spliter.Name);
    				}
    				catch (Exception ex)
    				{
    					TraceHelper.GetInstance().Error(ex);
    				}
    
    				break;
    			}
    		}
    	}

    This only ocurred in Word 2013, but it works fine in Word 2007,2010.

    Thanks,

    Friday, March 13, 2015 3:06 AM

Answers

  • Hi Chenfei,

    I am trying to reproduce this issue as you metioned however still failed. Here is the test code for your reference:

         string controlName = "uc";
    
            private void btnInserControl_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
    
                float width = 127;
                float height = 70;
                for (int i = 1; i <= 100; i++)
                {
                    Globals.ThisAddIn.Application.Selection.MoveEnd();
                    uc = new UserControl1();
                    var cs = vstoDoc.Controls.AddControl(uc, Globals.ThisAddIn.Application.Selection.Range, width, height, controlName+i);
                  
                }
               
            }
    
            private void btnDelete_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                for (int i = 1; i <= 100; i++)
                {
                    vstoDoc.Controls.Remove(controlName + i);
                }
               
            }

    I suggest that you try to update or repair the Office to see wether the issue was fixed.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Yang,Chenfei Monday, March 23, 2015 12:44 PM
    Monday, March 23, 2015 11:45 AM
    Moderator

All replies

  • I did a lot of testing. I am afraid that it's an issue of Office 2013.
    Monday, March 16, 2015 12:59 AM
  • Hi Chenfei,

    Have you fixed this issue now?

    I am trying to reproduce the issue however failed. Here is my test code for your reference:

     UserControl1 uc;
            string controlName = "uc1";
    
            private void btnInserControl_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                uc = new UserControl1();
    
                float width = 200;
                float height = 200;
                
                var cs = vstoDoc.Controls.AddControl(uc, Globals.ThisAddIn.Application.Selection.Range, width, height, controlName);
                cs.Name = controlName;
            }
    
            private void btnDelete_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                vstoDoc.Controls.Remove(controlName);
            }

    The code works well for me. Did I misunerstand? Also it is much appreciate to upload a demo that runable to reproduce this issue.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, March 17, 2015 2:09 AM
    Moderator
  • Hi Fei,

    You should insert more than 100 controls in Word Document. And delete all of them.

    You can try it.


    The future belongs to those who believe in the beauty of their dreams.

    Tuesday, March 17, 2015 3:16 AM
  • Hi Chenfei,

    I am trying to reproduce this issue as you metioned however still failed. Here is the test code for your reference:

         string controlName = "uc";
    
            private void btnInserControl_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
    
                float width = 127;
                float height = 70;
                for (int i = 1; i <= 100; i++)
                {
                    Globals.ThisAddIn.Application.Selection.MoveEnd();
                    uc = new UserControl1();
                    var cs = vstoDoc.Controls.AddControl(uc, Globals.ThisAddIn.Application.Selection.Range, width, height, controlName+i);
                  
                }
               
            }
    
            private void btnDelete_Click(object sender, RibbonControlEventArgs e)
            {
                Document vstoDoc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
                for (int i = 1; i <= 100; i++)
                {
                    vstoDoc.Controls.Remove(controlName + i);
                }
               
            }

    I suggest that you try to update or repair the Office to see wether the issue was fixed.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Yang,Chenfei Monday, March 23, 2015 12:44 PM
    Monday, March 23, 2015 11:45 AM
    Moderator
  • Hi Fei,

    Thanks for your suggestion. I have fixed this issue now.


    The future belongs to those who believe in the beauty of their dreams.

    Monday, March 23, 2015 12:44 PM