none
C# Word 2010 Paste in image and move behind text RRS feed

  • Question

  • Hi, I have created a word addin that will paste my image into a word document however I would like to set the image behind the text, I have tried to use the zordercmd but have not had any success. This is my first VSTO project and I find it a bit daunting, I have read up on the shape object and shapeRange zorder method but I am missing something. I would really appreciate some advice.

    Many thanks

    Mark.

     Clipboard.SetImage(myBitmap);

     // insert text at the current cursor location

     Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application;

     Word.Range selection = word.Selection.Range;

     selection.Paste();
    
    

    Friday, June 29, 2012 6:01 PM

Answers

  • Hi again, after spending some time experimenting I have produced some code that fills my requirements, instead of using the clipboard I have instead used the InLineShapes.AddPicture method, as I have made changes to the original image and do not wish to save those changes I have had to save the image as a temporary file.

    string tmpFile = ""; tmpFile = Path.GetTempFileName(); //My code to manipulate image myBitmap = new Bitmap(MyImage); myBitmap.Save(tmpFile); // insert image at the current cursor location Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application; Word.Range selection = word.Selection.Range; selection.InlineShapes.AddPicture(tmpFile, false, true).ConvertToShape().ZOrder(Office.MsoZOrderCmd.msoSendBehindText); File.Delete(tmpFile); myImage.Dispose(); myBitmap.Dispose();

    After adding the image the code deletes the temporary file. The code appears to run with out any problems.

    kind regards

    Mark.


    • Edited by Mark_YT Sunday, July 1, 2012 5:46 PM
    • Marked as answer by Mark_YT Sunday, July 1, 2012 5:46 PM
    Sunday, July 1, 2012 5:43 PM

All replies

  • Hi again, after spending some time experimenting I have produced some code that fills my requirements, instead of using the clipboard I have instead used the InLineShapes.AddPicture method, as I have made changes to the original image and do not wish to save those changes I have had to save the image as a temporary file.

    string tmpFile = ""; tmpFile = Path.GetTempFileName(); //My code to manipulate image myBitmap = new Bitmap(MyImage); myBitmap.Save(tmpFile); // insert image at the current cursor location Microsoft.Office.Interop.Word.Application word = Globals.ThisAddIn.Application; Word.Range selection = word.Selection.Range; selection.InlineShapes.AddPicture(tmpFile, false, true).ConvertToShape().ZOrder(Office.MsoZOrderCmd.msoSendBehindText); File.Delete(tmpFile); myImage.Dispose(); myBitmap.Dispose();

    After adding the image the code deletes the temporary file. The code appears to run with out any problems.

    kind regards

    Mark.


    • Edited by Mark_YT Sunday, July 1, 2012 5:46 PM
    • Marked as answer by Mark_YT Sunday, July 1, 2012 5:46 PM
    Sunday, July 1, 2012 5:43 PM
  • Hi Mark,

    I’m glad to hear that you got it working to resolve your question.

    Thank you for sharing your solutions & experience here, it will be very beneficial for other community members who have similar questions.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, July 5, 2012 6:02 AM
    Moderator
  • Hey Mark,

    Thanks a lot for your post, I have a similar problem where I want to paste an image to a Word document multiple times but Instead of sending it to the back of the text I would like for the text to wrap around it. I think it is square format. Maybe when you were looking for the answer to your problem you stumbled upon a solution to this one. A little more info about my issue:

    -I have a document template where I have predefined positions for text and images as "merge fields"

    -I iterate through the fields and depending on their field codes, I determine what text/image to insert

    -The image is a bar code that I am able to paste using the Clipboard but I am sure your way will work as well.

    -The Word Document has field codes that hold codes that are either "{ MergeField Logo \* MERGEFORMAT }" (for the text, or "{ INCLUDEPICTURE "qrCode" \* MERGEFORMATINET}"

    Bitmap qrcode = encoder.Encode(txt); //This is the bitmap //holds the encoded qrcode Clipboard.SetImage(qrcode); foreach(Word.Field myMergeField in oDoc.Fields)//loops through all of the merge fields in my doc { Word.Range rngFieldCode = myMergeField.Code; string fieldText = rngFieldCode.Text; if (fieldText.StartsWith(" INCLUDEPICTURE")) {

    //Next 4 lines just get rid of the text in the merge field Int32 endMerge = fieldText.IndexOf("\\"); Int32 fieldNameLenght = fieldText.Length - endMerge; string fieldName = fieldText.Substring(11, endMerge - 11); fieldName = fieldName.Trim(); myMergeField.Select(); oWord.Selection.Paste(); } }



    Friday, August 3, 2012 5:36 PM
  • Hi Morenohtslabs,

    I have spent some time considering your question and I think I have a good solution for you. I like you utilized the clipboard however it did not give enough control of the image, also if you use the clipboard.clear() method it can be problematic on 64 bit operating systems. I have presented the answer in the same way as my original code example as it was easier for me to test. In my testing I declared the InlineShape and the Shape variables globally so as to have access throughout the project.

     Word.InlineShape myInlineShape; Word.Shape myShape;

    I replaced this line of code

    selection.InlineShapes.AddPicture(tmpFile, false, true).ConvertToShape().ZOrder(Office.MsoZOrderCmd.msoSendBehindText);

    with the following

     myShape = selection.InlineShapes.AddPicture(tmpFile, falsetrue).ConvertToShape();

    myShape.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;

    you can add myShape.Select(); if you would like the object selected, the code ran perfectly in my project and

    I hope will be of use to you, let me know how you get on.

    kind regards

    Mark.

    Saturday, August 4, 2012 4:10 PM