简体   繁体   中英

Render Word Document from Remote Server using WIN32OLE in ruby on rails

When i am using win32ole as stand alone application at that time everything seems working fine, as soon as i put into my rails application which is running on mongrel server it goes into infinite loop.

I am trying to access "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

When i run this code stand alone at that time one Pop up come for Microsoft Share point credentials. however in mongrel rails it goes into infinite loop.

Do i need to handle this pop up to appear through Rails?

Have you looked into patching the win32ole.rb file?

Basically, here's the reason for the patch:

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

And here's the modified win32ole.rb file to fix the threading issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM