Win32ole - Word 在错误位置插入书签页码

时间:2021-03-08 23:00:43

标签: ruby

我正在使用 ruby​​ 自动创建一些 Word 文档,但遇到了一个奇怪的问题,即在表格中插入一个引用书签的页码,它会将其插入到错误的位置。

这是一些重现问题的示例代码。

require 'win32ole'

#setup new document
word = WIN32OLE.new('Word.Application')
word.Visible = true
word.Documents.Add('Normal.dotm')
doc = word.ActiveDocument

#create new table and merge cells
doc.Tables.Add(word.Selection.Range, 2, 2)
table_number = 1
doc.Tables(table_number).Borders.Enable = true
start_cell = doc.Tables(table_number).Cell(1,1)
end_cell = doc.Tables(table_number).Cell(1,2)
start_cell.Merge('MergeTo' => end_cell)

#putting some dummy text in
doc.Tables(table_number).Cell(1,1).Range.Text = 'Example Text'
doc.Tables(table_number).Cell(2,1).Range.Text = 'Filler'

#creating a bookmark
doc.Tables(table_number).Cell(1,1).Select
current_BM = "BM"
doc.Bookmarks.Add(current_BM, word.Selection)

#Attempting to insert the bookmark in the 2,2 cell
doc.Tables(table_number).Cell(2,2).Select
word.selection.InsertCrossReference("Bookmark",7,current_BM,true)

您希望书签插入到 2,2 单元格中,但它会将其插入到 2,1 单元格的开头。

Ruby script output

关于如何让它插入正确位置的任何想法?任何帮助将不胜感激。

0 个答案:

没有答案
相关问题