使用 WIN32OLE 的文字自动化

发布于 2024-07-07 21:51:09 字数 101 浏览 9 评论 0原文

我正在尝试将图像(jpg)插入到Word文档中,但win32old似乎不支持Selection.InlineShapes.AddPicture,或者我做错了什么。 有没有人有幸插入图像。

I am trying to insert an image (jpg) in to a word document and the Selection.InlineShapes.AddPicture does not seem to be supported by win32old or I am doing something wrong. Has anyone had any luck inserting images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人 2024-07-14 21:51:09

您可以通过调用 Document.InlineShapes.AddPicture() 方法来完成此操作。

以下示例将图像插入到活动文档中的第二个句子之前。

    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 )

有关 AddPicture() 方法的文档可以在此处找到。

有关使用 Ruby 自动执行 Word 的更多详细信息,请访问此处

这是David Mullet 的回答,可以在此处找到

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.

This is the answer by David Mullet and can be found here

疑心病 2024-07-14 21:51:09

在 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

它似乎有效。 有什么用吗?

Running on WinXP, Ruby 1.8.6, Word 2002/XP SP3, I recorded macros and translated them, as far as I could understand them, into this:

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

It seems to work. Any use?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文