Wordarounds

jplindstrom on 2004-12-22T22:45:49

More fiddling around with Word documents using the Win32::OLE module (and my, now more capable, Win32::OLE::Word::Writer).

The mission, which I chose to accept, is to extract our Coding Standard from the MoinMoin wiki (a few pages per language) and put into a proper document.

Why not keep it in the wiki? Well, a proper document is thought to be more authoritative. But keeping two sources in sync won't work. Don't Repeat Yourself, so a Word document is created from the wiki pages.

At first I thought about fetching the Wiki markup and parse it, so I tried the Inline::Python module to re-use the original Python parser source. But it seems Inline::Python doesn't work on Windows, so that didn't work out very well. Would have been extremely cool if it had worked.

The next idea was to transform the HTML since there is slightly more prior art when it comes to parsing that particular format. Ovid's HTML::TokeParser::Simple turned out to be very useful (nice work!).

The simple module which I asked about on PerlMonks is now capable of dealing with styles (both paragraph and character based), multi-level bullet lists and (soon) tables.

Generally, working with Office Automation is a jungle. Okay, the documentation is pretty good, but sometimes things don't work as advertised. One problem I bumped into was that it didn't work to turn off the do-you-want-to-save-your-document? dialog.

The documented way is to:

$oWord = Win32::OLE->new('Word.Application'), 'Quit') );
$oWord->{DisplayAlerts} = $rhConst->{wdAlertsNone};


That does't work at all in my Word 2000. And I found posts stating the same thing when searching. The workaround is to do this:

$oWord = Win32::OLE->new('Word.Application');
and in the DESTROY of the containing Win32::OLE::Word::Writer object first insist that the document is already saved, and then quit:
$self->oDocument->{Saved} = 1;
$self->oWord->Quit();


The bullet list was also a bit tricky to get working properly so I think there is quite a bit of complexity hidden in the new module.

There are a few working code examples available in Perl, but when figuring out how to do something it's often useful to record a macro and then inspect it.

The OLE browser is available from within Word: press Alt-F11, open the Object Browser, find your method or property and Right-click->Help to bring up the proper VBA help page. And I've probably bookmarked fifteen web pages with documentation and tips & tricks.

In addition to the Word module I see a html2word.pl and a moin2word.pl script on the horizon.