Ruby & Word 2007 help

I have a batchmacro that I use in Ruby with Word 2003. I converted to
Word
2007 and now it doesn’t work. Can anyone tell me how to change the
code?
The code is below:

batchmacro.rb

Ruby script for batch running Word macros

require ‘win32ole’

Launch new instance of Word

wrd = WIN32OLE.new(‘Word.Application’)

wrd.Visible = 1

First argument to script is the name of the macro

macro1 = ARGV.shift()

Everything else is a document on which to run the macro

ARGV.each do |file|
doc = wrd.Documents.Open(File.expand_path(file))
wrd.Run(macro1)
doc.Save()
doc.Close()
end

wrd.Quit()

I’m not sure I can help, but I’m interested in what you’re doing.
However,
you didn’t provide details of the failure(s); please do so.

Craig

Also, even though it looks like you have a good idea of what you’re
doing
with the Word object model, these articles at the Ruby on Windows blog
might
be helpful:

Craig

I don’t have Word 2007 here, so I can’t duplicate your environment.
However,
here are a couple questions that I’d ask myself at this point.

Has the Word object model changed in Word 2007 in some way that doesn’t
work
as expected but doesn’t cause a noticeable error message?

Might there be a file format conversion dialog that simply isn’t being
shown
in front of Word and which is waiting for you to answer?

If I think of anything else, I’ll reply again. Good luck.

Craig

Craig,

Thanks for your e-mail. It is stopping at the Open command - it opens
Word
but then doesn’t load the first document.

I’m running a macro on a bunch of Word documents that sets the paper
trays
for printing.

Thanks again for your help.

Inge

Inge H. Zwart, RN, BSN
TeleTrans, Inc.
Medical Transcription Specialists since 1989
Phone: 623-505-4423 / 877-495-9041
Fax: 623-505-4423

Inge Zwart wrote:

doc = wrd.Documents.Open(File.expand_path(file))

I’m assuming the same code ran on the same files (or copies of) using
Word 2003, and so the only change has been the version of Word.

I am not running Word 2007, so I couldn’t test this, but I couldn’t find
any evidence/discussion online of changes to the Documents.Open method
for 2007.

The Microsoft Word 2007 documentation for the Documents.Open method is
here:

Open Method [Word 2007 Developer Reference] | Microsoft Learn

Just guessing, but you might try passing optional parameters, such as:

doc = wrd.Documents.Open(File.expand_path(file), false, true)

David

The documentation for Documents.Open lists two required parameters. The
first one is FileName, and the second is Format. What’s strange, though,
is
that Format is listed both as optional and required. Is that just a
typo,
since the description for each listing of the Format parameter is very
similar?

David,

Thanks for your help. I tried changing the Open code and this is the
error I received:

Batchmacro.rb:16:in ‘method_missing’: Open (WIN30LERuntineError)
OLE error code:800A1436 in Microsoft Word
(C:/daily%20print%20batch/Corzo%20Jr…)
HRESULT error code:0x80020009
from batchmacro.rb:15:in ‘each’
from batchmacro.rb:15

Inge H. Zwart, RN, BSN
TeleTrans, Inc.
Medical Transcription Specialists since 1989
Phone: 623-505-4423 / 877-495-9041
Fax: 623-505-4423

Inge Zwart wrote:

David,

Thanks for your help. I tried changing the Open code and this is the
error I received:

Batchmacro.rb:16:in ‘method_missing’: Open (WIN30LERuntineError)
OLE error code:800A1436 in Microsoft Word
(C:/daily%20print%20batch/Corzo%20Jr…)
HRESULT error code:0x80020009
from batchmacro.rb:15:in ‘each’
from batchmacro.rb:15

Double-check the filename string that is being passed to the
Documents.Open method. It may be getting mangled somewhere along the
line, with regard to backslashes and/or spaces.

David

Craig D. wrote:

The documentation for Documents.Open lists two required parameters. The
first one is FileName, and the second is Format. What’s strange, though,
is
that Format is listed both as optional and required. Is that just a
typo,
since the description for each listing of the Format parameter is very
similar?

I suspect that it is a typo, for the reason you mentioned. Also,
required parameters are usually (in my experience) near the front of the
list.

David

Just a quick hint, since it’s saved me a few times when the MSDN site
didn’t have good info, use the ole_get_methods, or its analog
ole_methods.
The help method, ole_method_help(), usually doesn’t do much other than
return the name of the method in my experience, but heck, try it :slight_smile:
maybe you’ll have more luck.

–Kyle

This problem has captured my attention. I downloaded and installed a
trial
of Office 2007. Since the error was that the Documents.Open method was
missing, I focused my investigation around it. I determined that the
method
exists and is available to call. Therefore, I decided to write a small
program that tries opening a document that is specified in a few
different
ways. First, here’s the program.

require ‘win32ole’

word = WIN32OLE.new(‘Word.Application’)
word.Visible = true
#doc = word.Documents.Open(‘C:/Documents and
Settings/Administrator/work/test.doc’)
#doc = word.Documents.Open(File.expand_path(ARGV[0]))
doc = word.Documents.Open(‘C:\Documents and
Settings\Administrator\work\test.doc’)
sleep 5 #seconds
word.Quit()

Now here’s the output of the three runs that I did.

C:\Documents and Settings\Administrator\work>ruby opendoc.rb
opendoc.rb:5:in `method_missing’: Open (WIN32OLERuntimeError)
OLE error code:800A1436 in Microsoft Word
(C://Documents%20and%20Settings/Admin…)
HRESULT error code:0x80020009
Exception occurred. from opendoc.rb:5

C:\Documents and Settings\Administrator\work>ruby opendoc.rb test.doc
opendoc.rb:6:in `method_missing’: Open (WIN32OLERuntimeError)
OLE error code:800A1436 in Microsoft Word
(C://Documents%20and%20Settings/Admin…)
HRESULT error code:0x80020009
Exception occurred. from opendoc.rb:6

C:\Documents and Settings\Administrator\work>ruby opendoc.rb

C:\Documents and Settings\Administrator\work>

Notice that the two times that the path was given with forward slashes,
once
hard-coded and once from calling File.expand_path() on a file name
passed to
the program, the program failed. However, when the path was given with
backslashes, it succeeded.

I’m not sure what to do next, since File.expand_path() returns a path
with
forward slashes on Windows. Nevertheless, I hope that this information
allows the next step toward a solution to be taken.

Regards,
Craig

Hello,

In message “Re: Ruby & Word 2007 help”
on 07/08/24, “Craig D.” [email protected] writes:

I’m not sure what to do next, since File.expand_path() returns a path with
forward slashes on Windows. Nevertheless, I hope that this information
allows the next step toward a solution to be taken.

How about GetAbsolutePathName of Scripting.FileSystemObject?

fso = WIN32OLE.new(“Scripting.FileSystemObject”)
path = fso.GetAbsolutePathName(“test.doc”)
doc = word.Documents.Open(path)

Regards,
Masaki S.

Mr Masaki,

The solution you suggested works!

I replaces the line:

doc = word.Documents.Open(File.expand_path(file))

with the three lines you suggested.

Thank you,
Mick

Masaki S. wrote:

Hello,

In message “Re: Ruby & Word 2007 help”
on 07/08/24, “Craig D.” [email protected] writes:

I’m not sure what to do next, since File.expand_path() returns a path with
forward slashes on Windows. Nevertheless, I hope that this information
allows the next step toward a solution to be taken.

How about GetAbsolutePathName of Scripting.FileSystemObject?

fso = WIN32OLE.new(“Scripting.FileSystemObject”)
path = fso.GetAbsolutePathName(“test.doc”)
doc = word.Documents.Open(path)

Regards,
Masaki S.