# Win32ole, how to detect docfile is already opened?

**URL:** <https://www.ruby-forum.com/t/win32ole-how-to-detect-docfile-is-already-opened/129708>\
**Category:** Ruby\
**Created:** [February 20, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/win32ole-how-to-detect-docfile-is-already-opened/129708 "2008-02-20T21:30:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [February 20, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/win32ole-how-to-detect-docfile-is-already-opened/129708/1 "2008-02-20T21:30:46Z")

</div>

Hi,

running ruby 1.8.4 on Win2000, some word scripting  
with Office 2003

require ‘win32ole’

word = WIN32OLE.new(‘Word.Application’)  
doc = word.Documents.Open(‘c:/foobar.doc’)

Question =

i want to do a text search and replace  
and i need to detect whether the docfile  
is already opened by someone else, when  
word comes up with a dialog (3 radiobuttons)  
similar to that =

Title = bla.doc already in use  
|o| Open write protected readoonly  
|o| Open a copy and merge afterwards  
|o| Tell me when bla.doc is open

I want to check whether the docfile is  
already opened, and if yes, i start  
to loop until the docfile is free, then  
do the replace …

How do i realize whether the docfile is already opened ??

I searched already in irb via

puts docs.ole\_methods.sort  
and  
puts docs.ole\_methods.sort

but didn’t come up with a solution

Any hints ?

Regards, Gilbert

---

<div class="post-metadata">

**Author:** ![Gilbert\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c68b51/32.png) [@Gilbert\_R](https://www.ruby-forum.com/u/Gilbert_R)\
**Post date:** [February 21, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/win32ole-how-to-detect-docfile-is-already-opened/129708/2 "2008-02-21T20:46:08Z")

</div>

> i want to do a text search and replace  
> I want to check whether the docfile is  
> already opened, and if yes, i start  
> to loop until the docfile is free, then  
> do the replace …

OK, solved it ‘lowlevel’ with

File.exists?(’~$foobar.doc’)

means, if the file is already opened, there is  
a corresponding file with leading ~$ before  
the orginal filename, i.e. ~$foobar.doc

Regards, Gilbert
