views:

170

answers:

1

Hi all,
As the Title suggests, i am trying to find how to insert image in MS Word(.doc file) using ruby Win32Ole api.
I have tried the function InsertFile of Range Object but it seems, it is only made for inserting other doc file in our file in question.
Does anyone know anything related to this . It will very helpful.

+1  A: 

You can do this by calling the Document.InlineShapes.AddPicture() method.

The following example inserts an image into the active document, before the second sentence.

require 'win32ole'

word = WIN32OLE.connect('Word.Application')
doc = word.ActiveDocument

image = 'C:\MyImage.jpg'
range = doc.Sentences(2)

params = { 'FileName' => image, 'LinkToFile' => false, 
           'SaveWithDocument' => true, 'Range' => range }

pic = doc.InlineShapes.AddPicture( params )

Documentation on the AddPicture() method can be found here.

Additional details on automating Word with Ruby can be found here.

David

David Mullet
Hey are U the same guy from (Ruby on Windows)? I am a big Fan! I have nearly read all ur post about MS Word Automation. Thanks!
Webbisshh
Yes, I am. Thanks!Sorry that I didn't already have an article on the site about inserting images into Word documents. I'll write that soon.
David Mullet