Word Automation Using WIN32OLE

I am trying to insert an image (jpg) into a text document, and Selection.InlineShapes.AddPicture does not seem to be supported by win32old or I am doing something wrong. Someone managed to insert images.

+3
source share
2 answers

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 )

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

More information on automating Word with Ruby can be found here .

This is David Malle's answer and can be found here.

+2
source

WinXP, Ruby 1.8.6, Word 2002/XP SP3, , :

require 'win32ole'

begin
  word = WIN32OLE::new('Word.Application')   # create winole Object
  doc = word.Documents.Add
  word.Selection.InlineShapes.AddPicture "C:\\pictures\\some_picture.jpg", false, true
  word.ChangeFileOpenDirectory "C:\\docs\\"
  doc.SaveAs "doc_with_pic.doc"
  word.Quit
rescue Exception => e
  puts e
  word.Quit
ensure
  word.Quit unless word.nil?
end

, . ?

+1

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


All Articles