用Ruby的OLE 自动化把文本文件内容插入到Word中

博文要我写一本有关Ruby的书,其中对文本文件(代码,配置文件)的格式是有特殊要求,文件路径用Arial Narrow 小四、斜体,正文用Courier New 五号。下面的这个Ruby程序使用OLE Automation来做这个工作。

#insert_file_to_word.rb
require "win32ole"

begin
      word = WIN32OLE.connect("word.application")
rescue
      #如果没有打开的Word程序,创建一个
      word = WIN32OLE.new("word.application")
      word.documents.add
end

word.visible = true

doc = word.ActiveDocument()

#保存原有的文本字体
old_font_name = word.selection.font.name
old_font_size = word.selection.font.size
old_font_italic = word.selection.font.italic

#创建文件路径字体
word.selection.font.name = "Arial Narrow"
word.selection.font.size = 12
word.selection.font.italic = true

#输入文件路径
word.selection.typeText("#{ARGV[0]}/n")

#创建文件文本字体
word.selection.font.name = "Courier New"
word.selection.font.size = 10.5
word.selection.font.italic = false

#一行行写入,换行符用/11,这样可以作为一个整体来
#处理文件,而不用分段。
ARGF.each do |line|
      word.selection.typeText("#{line.chomp}/x0b")
end

word.selection.typeText("/n")

#恢复原有字体
word.selection.font.name = old_font_name
word.selection.font.size = old_font_size
word.selection.font.italic = old_font_italic

可以在命令行中输入 ruby insert_file_to_word.rb <FILE_TO_BE_INSERTED>,我用的是UltraEdit
的自定义工具功能,新建一个叫做"Insert to word"的菜单项,菜单的命令行部分使用ruby
d:/work/ruby_works/ruby_book/win32/insert_file_to_word.rb %F
,这样一来,只要打开这个文件,然后按一个快捷键,文件文本就会出现到Word文档中。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 Word 添加 OLE 对象,可以使用 Java 的 COM Bridge 组件,通过调用 Word 的 COM 接口实现。 以下是一个示例代码,演示如何在 Word 添加 Excel 表格: ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordOLE { public static void main(String[] args) { ActiveXComponent word = new ActiveXComponent("Word.Application"); word.setProperty("Visible", new Variant(true)); Dispatch documents = word.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Add").toDispatch(); Dispatch selection = Dispatch.get(word, "Selection").toDispatch(); // 添加 OLE 对象 Dispatch shapes = Dispatch.get(selection, "Shapes").toDispatch(); Dispatch oleObject = Dispatch.call(shapes, "AddOLEObject", "Excel.Sheet", "C:\\example\\example.xlsx", false, false, "", 0, "", 0).toDispatch(); // 调整 OLE 对象的位置和大小 Dispatch frame = Dispatch.get(oleObject, "Anchor").toDispatch(); Dispatch.call(frame, "Select"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveRight"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(selection, "MoveDown"); Dispatch.call(frame, "Select"); Dispatch.call(oleObject, "Activate"); // 保存并关闭文档 Dispatch.call(document, "SaveAs", "C:\\example\\example.docx"); Dispatch.call(document, "Close", new Variant(false)); Dispatch.call(word, "Quit"); } } ``` 在这个例子,我们使用了 jacob 库来调用 COM 接口。首先,我们打开 Word 应用程序并创建一个新文档。然后,我们获取当前的选区并在其添加 OLE 对象。接下来,我们使用选区调整 OLE 对象的位置和大小,然后激活它以确保它能够正常工作。最后,我们保存并关闭文档,并退出 Word 应用程序。 请注意,在此过程,我们需要提供 Excel 表格的路径和类型。在本例,我们将路径设置为 "C:\\example\\example.xlsx",并将类型设置为 "Excel.Sheet"。您需要根据您的情况进行更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值