Putting a VB net image into a Word document.

Posted on 2011-10-14
Last Modified: 2012-08-14
Hello, Experts,

I am creating a report in Microsoft Word (2K) from information held in a VB.Net application.  I would like to include graphic images that I have available in that application.  

In the VB.Net application, I typically display the images using the paint event (of a PictureBox, etc.) through calls of the form:


Any suggestions about how I can place a copy of the resulting image into an object in my Word report will be gratefully received.  


Question by:omegaomega
    LVL 6

    Accepted Solution

    You should create a Bitmap, create a Graphics object based on the Bitmap, draw on the Graphics (or copy from another Graphics object) and save the Bitmap to an image file which you can embed in your Word document
    'Specify the Height and Width of the Image
    Dim width As Integer = 500
    Dim height As Integer = 100 
    'Create a Bitmap instance of size 500x100
    Dim objBitmap = New Bitmap(width, height)
    Dim objGraphics = Graphics.FromImage(objBitmap) 
    'Create a black background for the border
    objGraphics.FillRectangle(New SolidBrush(Color.Black), 0, 0, width, height) 
    'Draw some text in the image
    objGraphics.DrawString("Sample Image generated on the Fly", New Font("Arial", 16, FontStyle.Bold, FontStyle.Italic), New SolidBrush(Color.Black), New Rectangle(0, 0, width, height), strFormat) 
    'Save this image in the File system
    'Don’t forget for clean up

    Open in new window

    Code snippet based on

    If you wish to copy Graphics from one object to another, you should get the hDC (Device Context handle) of each Graphics object and use BitBlt, as described in
    LVL 83

    Assisted Solution

    I am assuming that you have you image in a Image or Bitmap object. Once you have a reference to the Document object, you can use the Document.InlineShapes.Add() method to insert the image to word document.
    LVL 12

    Author Comment

    Hi, regevha and CodeCruiser,

    Thanks very much for your suggestions.   I'm sorry that my question wasn't as clear as it could have been.  CodeCruiser's assumption is basically correct.  My problems are more on the Word side than on the VB side.  I have already got the image available on the VB side, and regevha's reply shows clearly how to get that into a file that can be loaded into Word.  And CodeCruiser's reply shows how that file can be loaded on the Word side.

    But I had really hoped to be able to do this without having to use the file system as an intermediary.  There can potentially be a large number of these images, and I am concerned about the performance cost of using disk files to make the transfers.

    Also, the report is being generated from a Word template file.  In my initial approach I had embedded a "place-holder" picture in a section of this template.  (That section then gets duplicated as many times as required to accommodate the number of pictures to be included.)  The picture in the template does not reference any file.  This technique allows me to position the picture appropriately within the other report information without having to use any code.

    I had naively assumed there would be a way to pass the bitmaps directly between the applications and associate them with the pre-existing pictures in Word.  But after digging further it now seems that not only can I not get a Device Context handle for the Word picture (possibly allowing me to use BitBlt), I can't even load an image from a file into a pre-existing picture.   It seems that the association with a file can only be made when the picture is first created/added.  

    Is there really no other way than to use the file system for the transfer of the image?

    Is there really no way to load an image into a pre-existing Word picture?

    (I'm really afraid that the answers to both questions may be "yes".  Sigh...)

    Thanks again for your input, and for any additional insights you may be able to offer.

    LVL 83

    Expert Comment

    I am inclined to say yes as well. You can delete files afterwards.
    LVL 6

    Expert Comment

    According to Microsoft.Office.Interop,Word documentation, Shapes.AddPicture method requires a filename as input -
    Like CodeCruiser expert comment, you cannot avoid saving the image to file (temporarily).
    LVL 12

    Author Comment

    Hello, regevha and CodeCruiser,

    Thanks very much for your input.  I have used the approach that you have recommended.  

    I was able to reposition the pictures relatively easily by copying properties from the template pictures to the new pictures, and then deleting the templates.  I'm a bit disappointed to have to use the file system for the transfer, but the implementation does work, so thanks again.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
    In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
    The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now