insert image in .doc using win32ole library of Ruby

戏子无情 提交于 2019-12-12 03:07:59

问题


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



来源:https://stackoverflow.com/questions/1316162/insert-image-in-doc-using-win32ole-library-of-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!