Insert image in .doc using win32ole Ruby library

As you can see from the header, I'm trying to find a way to insert an image into MS Word (.doc file) using ruby ​​Win32Ole api.
I tried the InsertFile function of the Range object, but it seems to be made only to insert another doc document into our file in question.
Does anyone know something related to this. It will be very helpful.

0
source share
1 answer

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

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 )

The documentation for the AddPicture () method can be found here .

Word Ruby .

+2

Source: https://habr.com/ru/post/1699008/


All Articles