none
Can't add an image and convert to Shape to position correctly (vsto and Word) RRS feed

  • Question

  • Hi,


    Been wrestling this one for a few days and can't seem to find the answer. We have a Picture ContentControl in a Word document and need to do something that seems straightforward. Add a picture to the content control, convert it to a Shape and then set the positioning. Wrote the following code which is fine in the debugger but creates an error at

    Microsoft.Office.Interop.Word.Shape floatShape = myShape.ConvertToShape();

    Does anyone know why this creates an error of 'Error HRESULT E_FAIL has been returned from a call to a COM component.'

                        ContentControls listCCs = oWordDoc.SelectContentControlsByTag("ccSectorGraphic3");
                        if (listCCs.Count != 0) 
                        {
                            foreach (ContentControl thisCC in listCCs)
                            {
                                if (thisCC.Type == WdContentControlType.wdContentControlPicture && thisCC.Tag == "ccSectorGraphic3")  // looking for the picture content control i want to update  
                                {
                                    MessageBox.Show(thisCC.Title + " - " + listCCs.Count.ToString()); 
                                    thisCC.Range.Select();
    
                                    // Add our image
                                    InlineShape myShape = thisCC.Range.InlineShapes.AddPicture(strFilename, ref oMissing, ref oMissing, thisCC.Range);
    
                                    // We need to convert the picture from an inLine shape to a normal shape!
                                    Microsoft.Office.Interop.Word.Shape floatShape = myShape.ConvertToShape();
                                    
                                    // Now let's position and scale it correctly
                                    floatShape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
                                    floatShape.Left = oWordApp.CentimetersToPoints(1);
                                    floatShape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
                                    floatShape.Top =oWordApp.CentimetersToPoints(25);
                                    floatShape.WrapFormat.Type = WdWrapType.wdWrapTopBottom;
    
                                }
                            }
                        }


    Monday, July 7, 2014 6:08 AM

Answers

All replies