Skip Navigation LinksHome > Tips and Tricks > Office .NET > How to: Insert programmatically a bitmap to Microsoft Word documents   

  Tips & Tricks - Cool .net tips and tricks.  Tips & Tricks 

   Active Directory and .NET 
   ASP.NET 
   Arrays 
   Data Access Layer
   Data Structures 
   MCMS 2002
   Office and .NET
   OOSE
   Recursion 
   String manipulation


  Technical Writing - Technical writing articles and news.  Technical Writing 


   Information Design 

  Tools - Tools to share with everyone.  Tools 

   My tools
   My favorite .NET Tools


  Contact - Contact information.  Contact 

   About 
   Blog
   Contact


  Search - Search  Search 

  
  Search powered by MSN Search


        

  Search powered by  Powered by Google
 




How to: Insert programmatically a bitmap to Microsoft Word documents

Erika Ehrli Cabral 
July 2005

Introduction

Many developers are writing code to generate reports in Microsoft Word to present data and calculations, and most of the time, developers have no time to research on how to add nice-to-have images inside reports that are generated automatically with a tool.

You can find conceptual and procedural documentation on how to work with Microsoft Word and .NET; however, I have not found any article or how to topic that explains how to insert bitmaps to Microsoft Word documents from your .NET project.

As a Word user, you can create beautiful reports that take advantage of styles and visual design principles. You can create documents with headers that have a logo or insert other images to a template or document that will make them visually attractive.

One of the biggest advantages of Word programmability is that you are able to do almost everything that you do as Word application user. If you are working with a .NET application, follow this easy steps to insert programmatically a bitmap to Microsoft Word documents.

Previous steps:

Create a .NET project that generates Word documents.

For more information about Microsoft  Word and .NET, take a look at Understanding the Word Object Model from a .NET Developer's Perspective.

Procedures

To insert programmatically a bitmap to Microsoft Word documents

  1. Add to your .NET project a reference to System.Drawing.
  2. Add to your .NET project a reference to System.Windows.Forms.
  3. Add a reference to the previous namespaces in your code.

    [C#]

    using System.Drawing;
    using 
    System.Windows.Forms;

  4. Define the range where you need to insert the image.

    object start 0;
    object 
    end 0;
    Word.Range rng ThisDocument.Range(ref start, ref end);

  5. Define the image.

    Image img = new Image();
    // img = your image goes here.

    Note: You should assign the image you want to insert to the img object.

  6. Copy the image to the clipboard.

    Clipboard.SetImage(img);

  7. Paste the image to the range defined in step 4.

    rng.Paste();


See Also

Inserting images into Word documents using XML.


.NET Treats © 2005