首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby - WIN32OLE函数创建

Ruby - WIN32OLE函数创建
EN

Stack Overflow用户
提问于 2010-12-09 13:30:28
回答 1查看 891关注 0票数 2

我正在通过ruby做一些word自动化的工作,但我对它相对缺乏经验。我现在正在尝试使我的代码实现功能,但我遇到了这个错误

代码语言:javascript
复制
NameError: undefined local variable or method `doc' for main:Object
    from (irb):148:in `create_table'
    from (irb):152
    from C:/Ruby192/bin/irb:12:in `<main>'

这是我从我拼凑出来的示例代码中得到的

代码语言:javascript
复制
#Get the correct packages
require 'win32ole'

#setting up the Word
word = WIN32OLE.new('Word.Application')
#Shows the word Application
word.Visible = true
#Setting doc to the active document
doc = word.Documents.Add
doc = word.ActiveDocument

def create_table
  doc.Tables.Add(word.Selection.Range, 4, 2) #Creates a table with 3 rows and 2 columns
  doc.Tables(1).Borders.Enable = true
end

create_table
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-10 02:26:23

您的问题是,在create_table方法中,您引用了主范围中的变量,但没有传递给该方法。这就是你想要的:

代码语言:javascript
复制
require 'win32ole'

#setting up the Word
word = WIN32OLE.new('Word.Application')
#Shows the word Application
word.Visible = true
#Setting doc to the active document
doc = word.Documents.Add
doc = word.ActiveDocument

def create_table(d, w)
  d.Tables.Add(w.Selection.Range, 4, 2)
  d.Tables(1).Borders.Enable = true
end

create_table(doc, word)

注意,它现在将docword的引用传递给函数。另外,顺便说一下,您正在创建一个包含4行和2列的表。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4395172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档