Link to home
Start Free TrialLog in
Avatar of omegaomega
omegaomegaFlag for Canada

asked on

Putting a VB net image into a Word document.

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:

    MyObject.RenderImage(e.Graphics)

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

Thanks,
Randy

ASKER CERTIFIED SOLUTION
Avatar of regevha
regevha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of omegaomega

ASKER

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.

Cheers,
Randy
I am inclined to say yes as well. You can delete files afterwards.
Avatar of regevha
regevha

According to Microsoft.Office.Interop,Word documentation, Shapes.AddPicture method requires a filename as input - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addpicture.aspx
Like CodeCruiser expert comment, you cannot avoid saving the image to file (temporarily).
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.

Cheers,
Randy