Documents.Add fails on ASP.NET (VB.NET)

2019-02-25 03:29发布

问题:

I am having an issue with opening a document using Microsoft Word from ASP.NET MVC.

This works perfectly on my developer machine, but not when deployed to IIS.

Dim word = New Microsoft.Office.Interop.Word.Application

'This line is failing to return a document object
Dim letter = word.Documents.Add(letter_doc_path)

'This line then fails due to [letter] being null
letter.MailMerge.OpenDataSource(csvPath)

I have added permissions in "Component Services" (dcomcnfg) to the NETWORK SERVICE user which allows the creation of the Word object in the first place, but I am completely stuck as what to do with this one.

I have also tried suppressing Word dialogs with the following line just in case

word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone

The issue isn't helped by not having an error (apart from the null object reference obviously) - maybe there's a way to query Word for a specific error message?

回答1:

Word requires the normal.dot template file when opening any document, the problem was occurring because the IIS user didn't have anywhere to create the normal.dot so it was failing in the background.

This was fixed by setting the UserTemplate path for the newly created word instance (immediately after creating it).

The path must be writeable by the IIS user (NETWORK SERVICE in my case).

word.Options.DefaultFilePath(Microsoft.Office.Interop.Word.WdDefaultFilePath.wdUserTemplatesPath) = working_folder

So just for completeness, here's the original example with the winning line included:

Dim word = New Microsoft.Office.Interop.Word.Application

'this line fixed it
word.Options.DefaultFilePath(Microsoft.Office.Interop.Word.WdDefaultFilePath.wdUserTemplatesPath) = working_folder

Dim letter = word.Documents.Add(letter_doc_path)


回答2:

I was having the same problem, and the settings that wheelibin suggested weren't enough to create documents using the NETWORK SERVICE account.

What I ended up doing is:

  • Create a user account for this process to run under.

  • Login as the user and run Word (this does various setup tasks in Word so the application doesn't try putting up modal dialogs when running as a service).

  • Create a new application pool and set the pool to run as the user account.

If you're using Windows Authentication, and your server is Windows 2003 (or 2000, presumably), then this issue applies, and you need to either change the SPN of the server, which will break Windows Authentication for any application running under a different user account, or you have to switch the authentication provider over to NTLM instead of Kerberos.

IIS 7 can use Kernel Mode Authentication to avoid the issue.



回答3:

I am not sure how are you catching the errors.

Please take a look at the following pages if you find some clue from that.

  • Error while using Microsoft Office 2003 in web application

  • Error while calling MS-Word from ASP.NET

  • "There is insufficient memory or disk space. Save the document now" - Opening MS Word from ASP.NET