Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1254
  • Last Modified:

error on docx file conversion using doc.saveas method

I am writing code to convert docx file into a text file in ASP.net form using VB.net server side script.
My code is working fine in development environment. Where as I get an error "Object reference not set to an instence of an object" When published.

The error is coming in the line doc.SaveAs(newfilename, newfileformat)

Here is my code snippet

**************************************
Sub wrdfile(ByVal flpath As String, ByVal flnames As String, ByVal conv As String)
       Dim filepath As String = flpath
        Dim newfilename As Object = conv & flnames.ToString & ".txt"
        Dim newfileformat As Object = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDOSText
        Dim wordapp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
        newfilename = conv.ToString & flnames.ToString.Replace(".", "") & ".txt"
        Dim doc As Microsoft.Office.Interop.Word.Document = wordapp.Documents.Open(filepath, False)
        doc.SaveAs(newfilename, newfileformat)

        doc.Close()
        wordapp = Nothing
End sub
************************************************

here are the error details

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 293:        Dim doc As Microsoft.Office.Interop.Word.Document = wordapp.Documents.Open(filepath, False)
Line 294:        doc.SaveAs(newfilename, newfileformat)
Line 295:
Line 296:        doc.Close()
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Any help in resolving this is greatly appreciated

Thank You
0
Skerw
Asked:
Skerw
  • 7
  • 5
  • 4
1 Solution
 
ChadFoldenCommented:
You may have already double checked, but are you sure that you have MS Word installed on the machine that's getting this error (sometimes the simple things are the most difficult thing to see)?  
0
 
InteqamCommented:
if you are sure that MS office is installed, it might be an authority problem. run the application under administrator account.
0
 
SkerwAuthor Commented:
Guys,

Thank You for your responses.

ChadFolden: Yes, I am sure the MS office (including MS word) is installed on the machine.

Integam: I am not sure what do you mean by running application under administrator account. Can you give me steps on how to set it up that way?

Thank You,
-- Sree
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
InteqamCommented:
login in with administrative account, and run your code.
0
 
SkerwAuthor Commented:
Yes, I logged in as administrator and ran the website from IE. I still get the same error.

It works perfectly as expected when I am running the code in VS environment..

:(

Any help is greatly appreciated.

Thank You,
0
 
ChadFoldenCommented:
Your code looks fine to me (I even set up a test app and it workes just fine for me too).  I'm sure you know , but when you see "Object Reference Not Set to an Instance of an Object" it's generic, but normally means that you're trying to access an object that has not been properly intialized (in your case wordapp).  So to me it's got to be one of 3 things:
1) Word is not installed ... or perhaps a different version is installed that what your program is expecting
2) Your file.doc is corrupt and there's no document to open (wordapp.Documents.Open .. so the document class is throwing that error)
or
3) like Integam mentioned, the context in which you are running this app under, does not have the appropriate rights to use MS Word.

To point #3, have you tried to set <identity impersonate="true" /> in your web.config .. or use integrated authentication in your website's IIS config so that requests are not being processed as anoymous users, which (depending how things are set up) may not have rights to use word
0
 
SkerwAuthor Commented:
Hi,

Yes, I did made the impoersonate to true and provided specific user in the set. Please see the attached screen shots that would describe the settings and issue.

I am not sure where else to change the configuration. I made sure whereever I see the authentication, there are all using the specific user who is an admin on the computer.

I am getting desparate. Any help in resolving this would be greatly appreciated.

Thank You

CorrectlyWorkin-While-Running-wi.jpg
Error-when-Using-IIS.jpg
0
 
InteqamCommented:
I think it is an IIS problem, but not sure what.
0
 
SkerwAuthor Commented:
yes, I think so too. But not sure as to where is the problem and what user it is using while opening the word when running via IIS. At lease if I know who is the user it is using when running via IIS so I can provide proper rights to that user.

Does anyone know how to determine what is the user a process is using?

Any help is greatly appreciated.

Thank You.
0
 
ChadFoldenCommented:
I'm still guessing you're site is still using the anonymous user (why else would you get access denied).  Can you try disabling Anonymous Access all together (looks like it is still enabled).  
If you're the only one using the server where your app is installed, you can easily see who IIS is authenticating as by making a request and then looking at the Security Log in the Event Logs
0
 
ChadFoldenCommented:
Was just thinking too ... if winword.exe is really running as a specific user as you've defined, you should be able to verify and see who that is in Task Manager too (on the processes tab, but showing User Name field).
0
 
InteqamCommented:
one last thing

does the user have access to the file?
the file to open or WinWord.exe?!
0
 
InteqamCommented:
usually IIS runs under NETWORK SERVICES user
0
 
SkerwAuthor Commented:
Guys,

Thank you very much for your support and help on resolving this issue. yes, I made sure the access to run WinWord.exe. I am very very puzzled by the behavior of this on my Vista machine. Hence I published the site on a Win 2008 server (I did not want to touch it as it is a production server )  and changed the impersonate to a real account and it started working.

So, I now decided to upgrade this vista into Windows 7 and see what is going to happen..

I appreciate your help.

Sometimes, it is what it is and it is not worth wasting time.

Thank You,
0
 
InteqamCommented:
well, you do your math.

we are sorry we couldn't help, sometimes; it cannot be fixed unless we are there.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now