-->

insert image in .doc using win32ole library of Rub

2019-08-20 08:25发布

问题:

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:

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