使用WIN32OLE在红宝石轨道上使用WIN32OLE渲染Word文档

当我在当时使用win32ole作为独立应用程序时,一切似乎都正常工作,只要我把它放到我的rails应用程序中,无限循环。使用WIN32OLE在红宝石轨道上使用WIN32OLE渲染Word文档

我试图访问“的https://microsoft/sharepoint/document.doc”

def generatertm(issue) 

begin

word = WIN32OLE.new('word.application')

logger.debug("Word Initialized...")

word.visible = true

myDocLink = "https://microsoft/sharepoint/url.doc"

myFile = word.documents.open(myDocLink)

logger.debug("File Opened...")

puts "Started Reading bookmarks..."

myBookMarks = myFile.Bookmarks puts "bookmarks fetched working background task..."

print ("Bookmakr Count : " + myBookMarks.Count.to_s + "\n")

myBookMarks.each do |i|

logger.warn ("Bookmark Name : " + i.Name + "\n")

end

rescue WIN32OLERuntimeError => e

puts e.message

puts e.backtrace.inspect

else

ensure

word.activedocument.close(true) # presents save dialog box

#word.activedocument.close(false) # no save dialog, just close it

word.quit

end

end

当我在那个时候单独运行这段代码放置一次拉网来为微软共享点凭据。然而在杂种的轨道上它进入了无限循环。

我需要处理这个弹出来通过Rails出现吗?

回答:

您是否打算修补win32ole.rb文件?

基本上,这里的修补程序的原因:

t turns out that win32ole.rb patches the thread to call the windows OleInitialize() & OleUninitialize() functions around the yield to the block. However, the MS documentation for CoInitialize (which OleInitialize calls internally) state that: "the first thread in the application that calls CoInitialize with 0 (or CoInitializeEx with COINIT_APARTMENTTHREADED) must be the last thread to call CoUninitialize. Otherwise, subsequent calls to CoInitialize on the STA will fail and the application will not work." http://msdn.microsoft.com/en-us/library/ms678543(v=VS.85).aspx

而这里的修改win32ole.rb文件来解决线程问题:

require 'win32ole.so' 

# Fail if not required by main thread.

# Call OleInitialize and OleUninitialize for main thread to satisfy the following:

#

# The first thread in the application that calls CoInitialize with 0 (or CoInitializeEx with COINIT_APARTMENTTHREADED)

# must be the last thread to call CoUninitialize. Otherwise, subsequent calls to CoInitialize on the STA will fail and the

# application will not work.

#

# See http://msdn.microsoft.com/en-us/library/ms678543(v=VS.85).aspx

if Thread.main != Thread.current

raise "Require win32ole.rb from the main application thread to satisfy CoInitialize requirements."

else

WIN32OLE.ole_initialize

at_exit { WIN32OLE.ole_uninitialize }

end

# re-define Thread#initialize

# bug #2618(ruby-core:27634)

class Thread

alias :org_initialize :initialize

def initialize(*arg, &block)

if block

org_initialize(*arg) {

WIN32OLE.ole_initialize

begin

block.call(*arg)

ensure

WIN32OLE.ole_uninitialize

end

}

else

org_initialize(*arg)

end

end

end

http://cowlibob.co.uk/ruby-threads-win32ole-coinitialize-and-counin

以上是 使用WIN32OLE在红宝石轨道上使用WIN32OLE渲染Word文档 的全部内容, 来源链接: utcz.com/qa/258816.html

回到顶部