Solved

error on docx file conversion using doc.saveas method

Posted on 2010-09-02
16
1,175 Views
Last Modified: 2012-05-10
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
Comment
Question by:Skerw
  • 7
  • 5
  • 4
16 Comments
 
LVL 4

Expert Comment

by:ChadFolden
ID: 33592188
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
 
LVL 7

Expert Comment

by:Inteqam
ID: 33592774
if you are sure that MS office is installed, it might be an authority problem. run the application under administrator account.
0
 

Author Comment

by:Skerw
ID: 33596332
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 7

Expert Comment

by:Inteqam
ID: 33596921
login in with administrative account, and run your code.
0
 

Author Comment

by:Skerw
ID: 33598124
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
 
LVL 4

Accepted Solution

by:
ChadFolden earned 500 total points
ID: 33598187
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
 

Author Comment

by:Skerw
ID: 33615002
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
 
LVL 7

Expert Comment

by:Inteqam
ID: 33615512
I think it is an IIS problem, but not sure what.
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 33615539
0
 

Author Comment

by:Skerw
ID: 33617625
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
 
LVL 4

Expert Comment

by:ChadFolden
ID: 33618886
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
 
LVL 4

Expert Comment

by:ChadFolden
ID: 33619028
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
 
LVL 7

Expert Comment

by:Inteqam
ID: 33622969
one last thing

does the user have access to the file?
the file to open or WinWord.exe?!
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 33622971
usually IIS runs under NETWORK SERVICES user
0
 

Author Comment

by:Skerw
ID: 33625842
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
 
LVL 7

Expert Comment

by:Inteqam
ID: 33627387
well, you do your math.

we are sorry we couldn't help, sometimes; it cannot be fixed unless we are there.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question