Microsoft.Office.Interop.Word SaveAs stalling

Hi all. I’m finding my code stalling at a given line when I try to save a Word document as a PDF using Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF. By stalling I mean the browser hourglass just keeps going and going. The weird thing is, if I look on my server the PDF file has been created and saved fine, but the code never goes past the .SaveAs line. It stalls there.

Does anyone know if there’s a way to SEE what Word is doing? I figure it’s waiting for some input, but I have no idea what ><

I’ve tried making ms_word_app.Visible = True, but that seems to do nothing. I don’t SEE Word open on the server at all when this code runs so I’ve got no idea what input it is waiting for.

============================

More detail and code below:

============================

From my ASP.NET/VB.NET project, I’ve successfully managed to create and save a Word document based on a Word template file using this:

ms_word_doc = ms_word_app.Documents.Add(template_file)
ms_word_doc.SaveAs(“whatever.doc”)
ms_word_app.Documents.Close(True)
ms_word_app.Quit(True)

…everything saves, closes and quits fine.

but if I use the same code and try to save as a PDF, the program stalls at the .SaveAs line:

ms_word_doc = ms_word_app.Documents.Add(template_file)
ms_word_doc.SaveAs(“whatever.pdf”, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF)
’ … never gets here >< goto sitepoint for help!! :slight_smile:
ms_word_app.Documents.Close(True)
ms_word_app.Quit(True)

I have even tried the
ms_word_doc.ExportAsFixedFormat option but the same thing happens. It would SEEM as if Word is waiting for some input, but I have no idea why it is stalling.

My question is: Is there anyway for me to see what would be causing this to stall at this line? Any debugging ideas?

You really shouldn’t be doing any COM interop from ASP.NET. There are alot of things going wrong, not all of which you can detect nor fix. As you are finding out.

Ok. Any advice on another option? I need to take a Word template file from an ASP.NET web application, run MailMerge replacements, and save as a Word or PDF file. This is my first time working with Word from a web app, so if there’s a better way of doing things I’m open to any suggestions :slight_smile:

Or is now a good time to tell my manager “sorry we can’t save as PDF”?

There is no real good way to do word mail merges on the web server side. Best bet is to provide data to the customer and let them merge away.

If you’ve got to do it, you are looking at buying some rather pricey libraries (see aspose for examples).

Ouch I see what you mean by pricey.

Thanks very much for your help and advice. Is very much appreciated.

Just solved my annoying problem. So for anyone who encounters this problem in the future …

The .SaveAs method required additional empty/missing arguments *.

So instead of this:

>> ms_word_doc.SaveAs(destination_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF)

It needed this:

>> Dim s_missing As Object = System.Reflection.Missing.Value
>> ms_word_doc.SaveAs(destination_file, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing, s_missing)

  • Even though I read multiple times that you don’t need to provide the missing arguments when you use VB.NET >< Gotta love bad research information :wink: