![](/img/trans.png)
[英]I want to change a word in a windows DOC using win32ole in Ruby
[英]Render Word Document from Remote Server using WIN32OLE in ruby on rails
當我當時使用win32ole作為獨立應用程序時,一切似乎都正常運行,一旦我將其放入運行在mongrel服務器上的rails應用程序中,就會陷入無限循環。
我正在嘗試訪問“ 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
當我當時單獨運行此代碼時,將彈出一個彈出窗口,以獲取Microsoft共享點憑據。 但是在雜種軌道中,它會陷入無限循環。
我是否需要處理此彈出窗口才能通過Rails出現?
您是否研究過修補win32ole.rb文件?
基本上,這是補丁的原因:
事實證明,win32ole.rb修補了線程,以圍繞該塊的收益率調用Windows OleInitialize()和OleUninitialize()函數。 但是,CoInitialize的MS文檔(OleInitialize在內部對其進行調用)指出:“應用程序中調用CoInitialize的值為0(或CoInitializeEx的值為COINIT_APARTMENTTHREADED)的第一個線程必須是調用CoUninitialize的最后一個線程。否則,后續對CoInitialize的調用STA將失敗,應用程序將無法運行。” http://msdn.microsoft.com/zh-CN/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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.