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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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)

Method 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.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x