Solved

error on docx file conversion using doc.saveas method

Posted on 2010-09-02
16
1,152 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now