Fog Creek Software
Discussion Board




Word automation woes

Trying to automate Word 2003 on a server from a (classic) ASP page.  I know it's a bad idea -- set that aside for the moment and accept that it's something I need to get working.  I don't know much about Office automation, just working with an ASP file that someone else wrote.

What it is trying to do is open a .html file with Word and save it as a .doc file.  Nothing else -- just open and save in another format.

The problem is that WINWORD.EXE starts up and seems to be doing something, then just goes to sleep after a second or so.  I suspect it is trying to pop up a dialog, because if I try to perform the Save As operation manually, I get a little panel with the "information" (i) icon informing me that linked stylesheets cannot be saved in the chosen format.  "Continue" and "Cancel" buttons are presented.

I have searched for any options or properties that I can set to make Word suppress this alert.  I tried setting the Word application DisplayAlerts property to False (also zero).  I  perused the options settings in Word for anything like "suppress warnings" but did not find anything that had the desired effect.

Am I SOL here or is there some other approach I can take?

AMS
Tuesday, August 24, 2004

Try this link, if you haven't read it already:

http://support.microsoft.com/default.aspx?kbid=257757

Basically Microsoft says, "You really shouldn't do this but, if you insist, here are a few suggestions that might help..."

John Rusk
Tuesday, August 24, 2004

I'll start with my condolences.  I spent 6 years working on a project which hinged on Word automation. 

The following is a hack - it's all we ever came up with - if you know that all the files you open/save as will throw the same error, program in an Enter or whatever clears the information dialog when you do it manually.

I'll offer a bit of unsolicted advice - if you're processing any significant volume of files:  You need a job that keeps track of the last file to be sent to the automation job.  You need a second job that monitors Word and kills the application if it doesn't process in some amount of time. Then, somehow, you need to move the file that hung up saved off somewhere, and get the automation to process the next file.  In the AM, or whenever, someone needs to hand process those that hung up the process the first time through.

Kate
Tuesday, August 24, 2004

I've run into these headaches before - password protected files, previous file versions, conversion prompts upon saving.  They really mess up automation.

One thing that I found helpful was to record a macro while manually stepping through the process.  There's often a property setting or two that can be set to avoid the dialog boxes.  Typically, this properties aren't documented very well in the Word VBA help files.

Yet another anon
Tuesday, August 24, 2004

Sorry, sorry, sorry that this is not a direct answer...

Opening an Office application as part of processing an ASP request is always going to cause headaches. Word still sometimes hangs, it uses lots of memory, and it takes a long time to start up.

Perhaps you can find a nice ASP component using Google that processes Word documents without launching Word. Although I suspect this won't allow you to do the "open as HTML, save as DOC" requirement easily, with a bit of lateral thinking you might find such a component has features that allow you to achieve your aim in a different manner. The component approach would probably be quicker, more stable, and easier.

Herr Herr
Tuesday, August 24, 2004

I just spotted this page, which has some stuff that might be relevant:

http://weblogs.asp.net/chris_pratley/archive/2004/04/28/122374.aspx

John Rusk
Tuesday, August 24, 2004

You __really__ do not want to do this unless you have to. Trying to automate office is bad enough as it is without drastically increasing the problems caused by trying to get the account the ASP process is running as to do it...

As someone else said its very easy for the processes to get stuck for so many reasons - open up Task Manager, and you might see WINWORD appaearing multiple times....

Does it have to be real-time or can it be near real time?

You'd probably be better off writing out documents to be converted to a file which is procedded by a scheduled task which runs every _n_ minutes either from a VBS or even an EXE - much easier to run as an alternate user, debug, enable logging etc. etc.

Gives you a lot more options as well, even if you have to resort to using SendKeys...

<-.->
Tuesday, August 24, 2004

Well thanks.  Short term there is no good way around using Word for this.  Longer term I absolutely want to find another approach (probably by converting the html to .pdf, which there are many options for doing).

I think I will try the approach of doing this outside of ASP with a separate script.  It will have to watch for the appearance of .html files and then do its thing.  It will not be real time but hopefully close enough that it will suffice.

AMS
Tuesday, August 24, 2004

How about converting the HTML to RTF? That would be a ton easier and there should be a fair number of options for doing it.

Steve Barbour
Tuesday, August 24, 2004

something like http://www.sautin.com/html-to-rtf/developers.htm might work?

http://www.google.com/search?&q=%22convert+html+to+rtf%22+component turns up

Just me (Sir to you)
Tuesday, August 24, 2004

On the topic of Office automation, way back in a prior life a project required that we create some reports for a client, and the client was extremely specific about the format of the reports - they supplied us with Excel templates and basically told us to generate the report exactly as they presented (they needed the ability to modify numbers using a tool they knew, Excel, using features like embedded graphs, worksheet cross referencing, etc). We tried Crystal Reports and a variety of other solutions that could export to the desired format, but it was always close but no cigar.

After much grumbling and resistance, we took on the task and built the system to automate Excel on the server, much like the OP's word scenario, and the result was precisely what the client wanted - on demand, user provided criteria Excel format reports utilizing a large number of complex Excel features. The reports were quite simply beautiful, and perfectly satisfied the business need. We could use this technique because the system in question was a very low volume, high-value-per-transaction system that could facilitate such a heavy weight solution. Furthermore once the scripts were cleaned up and proven the system ran reliably day after day.

The lesson, if there is one, is that the right solution for one task might not be right solution for another task - Just because this sounds ridiculous for say a high volume public ecommerce site, that doesn't mean it isn't a viable, entirely credible solution for the scenario portrayed above. Build your projects based upon your own project's world, rather than the world faced by hotmail or slashdot.

Dennis Forbes
Tuesday, August 24, 2004

Well put Dennis.

I have designed many processes around Office automation stuff like this. Most all of them are used once a day, some of them more often. Generally, they handle a low volume of data.

In the last six months I have had to do ZERO maintenance/trouble-shooting to any of these.

If the shoe fits... put your foot in it.

agreed
Tuesday, August 24, 2004

As other have mentioned, simply let the word macro record and write the code for you (the macros in word now output VB6 code...NOT vbscript!).

A quick try gives:

  Documents.Open FileName:="""Yahoo! Canada.htm""", ConfirmConversions:= _
      False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
      PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
      WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""

  ActiveDocument.SaveAs FileName:="Yahoo! Canada.doc", FileFormat:= _
      wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
      True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
      False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
      SaveAsAOCELetter:=False

  ActiveDocument.Close

Stripping everything out, and converting the word generated VB6 above macro to a vbscript macro, you get:

a very simple vbscript

---- start ---
strFromDoc = "c:\ytest\yahoo! canada.htm"
strToDoc  = "c:\ytest\Yahoo.doc"

Set wordApp = CreateObject("Word.Application")

WordApp.Documents.Open strFromDoc, false,,False

wordApp.ActiveDocument.SaveAs strToDoc, 0, , , False

wordApp.ActiveDocument.Close

wordApp.Quit

set wordApp = nothing

--- end ---

Note in vbscript you can’t use the word constants…but you can use words VB6 IDE to grab the value of wdFormatDocument in the debug windows (is is “0)

So, if you take the text between start and end, paste it into a text document, rename the document with a .vbs exentsion...you can then just click on the document...and you code will run.


Albert D. Kallal
Edmonton, Alberta Canada
kallal@msn.com
http://www.attcanada.net/~kallal.msn

Albert D. Kallal
Tuesday, August 24, 2004

Only it doesn't.  None of those options appears to suppress the alert about the linked stylesheets.  And it hangs there.

Interestingly I can make it work in Word 2000; it does not offer that info alert on the SaveAs operation.  But not 2002 which is what I'm dealing with.

AMS
Tuesday, August 24, 2004

*  Recent Topics

*  Fog Creek Home