none
Adding picture to Word 2010 content control fails with unspecific COM Exception RRS feed

  • Question

  • Hi!

    I'm having trouble with adding and removing images from Word 2010 rich text content controls. After the second or third time a very unspecific COM Exception gets thrown. The following code reproduces this behaviour:

     

    public const string ImagePath = @"C:\Users\Developer\Documents\Images\Sample.jpg";
    
    private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
        var contentControl = ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, Paragraphs[1].Range);
        for (var i = 1; i <= 10; i++) {
            foreach (Word.InlineShape item in contentControl.Range.InlineShapes) item.Delete();
            contentControl.Range.InlineShapes.AddPicture(ImagePath, SaveWithDocument: true);
        }
    }
    

     

    The following Exception gets thrown:

     

    System.Runtime.InteropServices.COMException was unhandled by user code
      Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
      Source=WordDocument1
      ErrorCode=-2147467259
      StackTrace:
           at Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range)
           at WordDocument1.ThisDocument.ThisDocument_Startup(Object sender, EventArgs e) in C:\Projects\WordDocument1\WordDocument1\ThisDocument.cs:line 24
           at Microsoft.Office.Tools.Word.DocumentImpl.OnStartup()
           at Microsoft.Office.Tools.Word.DocumentImpl.DocumentExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup()
           at Microsoft.Office.Tools.Word.DocumentBase.OnStartup()
           at WordDocument1.ThisDocument.FinishInitialization() in C:\Projects\WordDocument1\WordDocument1\ThisDocument.Designer.cs:line 57
           at Microsoft.Office.Tools.Word.DocumentBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization()
           at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)
           at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()
      InnerException: 
    

    Regards,
    Oliver Hanappi 

     


    • Edited by Oliver Hanappi Tuesday, January 31, 2012 5:56 PM Added Word version to question
    Tuesday, January 31, 2012 1:57 PM

Answers

  • Hi Oliver

    You really need to provide full repro steps. Not just remarks and code snippets.

    And I'll say it again, more clearly perhaps this time: Why not just set the contentcontrolRange.Text = "". Why bother to go through all the loops and deletions?

    The following code works just fine, as long as the content control really is a RichText content control. I can run it as often as I like. The lines I commented out I used for trouble-shooting.

            private void InsertPicsInCC_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document doc = Globals.ThisDocument.InnerObject;
                foreach (Word.ContentControl cc in doc.ContentControls)
                {
                    if (cc.Type == Word.WdContentControlType.wdContentControlRichText)
                    {
                        cc.Range.Text = "";
                        //System.Windows.Forms.MessageBox.Show(cc.ID);
                        //cc.Range.Select();
                        cc.Range.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", ref missing, ref missing, ref missing);
                    }
                }
            }
    
    


     


    Cindy Meister, VSTO/Word MVP
    Sunday, February 5, 2012 6:15 PM
    Moderator
  • Finally I solved the issue. The problem was twofold:

    1. Like Cindy Meister already pointed out, clearing inline shapes by setting the Text property to an empty string prevents the exception, so iterating over the inline shape collection and deleting each item is a bad idea.
    2. I did not include in the code snippets a part where I set LockContents to false before and to true afterwards. Once LockContents is set to true on a single, completely unrelated content control, adding a picture to some other content control fails.

    • Marked as answer by Oliver Hanappi Wednesday, February 8, 2012 4:11 PM
    • Edited by Oliver Hanappi Wednesday, February 8, 2012 4:12 PM fixed typo
    Wednesday, February 8, 2012 4:11 PM

All replies

  • Hi Oliver

    Why aren't you using an Image content control?

    Why are you adding a content control and an image in the Startup method? Why not have the content control in the document to begin with?

    Since you're always adding the content control, why bother deleting any InlineShapes? There won't be any present in a new content control.

    Why would you be looping and deleting/adding the same picture 10 times to the content control?


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 31, 2012 2:45 PM
    Moderator
  • This is just code for reproduction of the behaviour.
    Tuesday, January 31, 2012 2:49 PM
  • But it makes no sense and without proper context it's impossible to try to understand the problem.

    There are all kinds of possible reasons for what you showing us not being able to work, not the least of which that it's firing when the VSTO document is still loading.


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 31, 2012 3:00 PM
    Moderator
  • The real action is triggered by a button click from the action pane. Users can place various content controls (by other buttons on the actions pane) on the document and part of the content that gets generated within the content controls are pictures.
    Tuesday, January 31, 2012 3:05 PM
  • And what does this code look like? By that I mean, what actions will be performed when the user clicks a button that causes the error?

    And what line of code is triggering the error?


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 31, 2012 5:48 PM
    Moderator
  • My application iterates through all content controls within the document and clears each by removing all inline shapes and tables and setting the Range.Text to an empty string. Then, depending on the type of content control (managed via its Tag), pictures are being added to the content control along with some text. The error occurs when calling the AddPicture method.

    The sample code above also causes the same exception, but works only on one content control rather than many.

    Tuesday, January 31, 2012 6:27 PM
  • I can't reproduce the behavior seeing, based on the information you provide. I can loop ContentControl.Range.InlineShapes.AddPicture, inserting a picture from my local disk, as often as a wish with no issues.
    Cindy Meister, VSTO/Word MVP
    Sunday, February 5, 2012 4:23 PM
    Moderator
  • It is critical to remove all inline shapes in each iteration before adding the picture.
    Sunday, February 5, 2012 5:46 PM
  • Hi Oliver

    You really need to provide full repro steps. Not just remarks and code snippets.

    And I'll say it again, more clearly perhaps this time: Why not just set the contentcontrolRange.Text = "". Why bother to go through all the loops and deletions?

    The following code works just fine, as long as the content control really is a RichText content control. I can run it as often as I like. The lines I commented out I used for trouble-shooting.

            private void InsertPicsInCC_Click(object sender, RibbonControlEventArgs e)
            {
                Word.Document doc = Globals.ThisDocument.InnerObject;
                foreach (Word.ContentControl cc in doc.ContentControls)
                {
                    if (cc.Type == Word.WdContentControlType.wdContentControlRichText)
                    {
                        cc.Range.Text = "";
                        //System.Windows.Forms.MessageBox.Show(cc.ID);
                        //cc.Range.Select();
                        cc.Range.InlineShapes.AddPicture(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", ref missing, ref missing, ref missing);
                    }
                }
            }
    
    


     


    Cindy Meister, VSTO/Word MVP
    Sunday, February 5, 2012 6:15 PM
    Moderator
  • Finally I solved the issue. The problem was twofold:

    1. Like Cindy Meister already pointed out, clearing inline shapes by setting the Text property to an empty string prevents the exception, so iterating over the inline shape collection and deleting each item is a bad idea.
    2. I did not include in the code snippets a part where I set LockContents to false before and to true afterwards. Once LockContents is set to true on a single, completely unrelated content control, adding a picture to some other content control fails.

    • Marked as answer by Oliver Hanappi Wednesday, February 8, 2012 4:11 PM
    • Edited by Oliver Hanappi Wednesday, February 8, 2012 4:12 PM fixed typo
    Wednesday, February 8, 2012 4:11 PM