Paste PictureBox.Image into Word (Interop)

210 Views Asked by At

Is there a way to paste the image (eg. from a PictureBox) directly into a Word Shape?
I'm currently using Clipboard to copy then Clipboard.SetImage(Image); But was hoping to do it directly without the Clipboard. This link How to paste image from clipboard to Word references using the Range.FormattedText, but I'm not sure how to make the Image into a FormattedText?

Here's my code using the Clipboard:

foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
{
    if (s.Title.Equals("WordImage", StringComparison.OrdinalIgnoreCase))
    {
         Word.Range range = s.Anchor;
         Clipboard.SetImage(myPictureBox.Image);
         range.Paste();
     }
}
1

There are 1 best solutions below

0
On

Is there a way to paste the image (eg. from a PictureBox) directly into a Word Shape? [#word-interop]

Define "directly". If you mean "is there a property of type Shape or InlineShape we can set an instance of dotnet class Image?" then the answer is NO.

Both Shape and InlineShape types do not expose any method to pass argument in or property to set dotnet image object or data of image object.

Instances of dotnet class Image should be saved into a file then added into word document with Shapes.AddPicture() method, or saved into clipboard then pasted into word document with Range.Paste() method. There is no other way.

There is a property string Shape.RTF, which is set-only, intuitively suggests to set rtf data of an image. But the official documentation says it is "Reserved for internal use.".

Conclusion

Dotnet Image object cannot be inserted into Word document "directly". It canbe inserted via a file or clipboard.