Solved

error on docx file conversion using doc.saveas method

Posted on 2010-09-02
16
1,148 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
login in with administrative account, and run your code.
0
 

Author Comment

by:Skerw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I think it is an IIS problem, but not sure what.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Expert Comment

by:Inteqam
Comment Utility
0
 

Author Comment

by:Skerw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
usually IIS runs under NETWORK SERVICES user
0
 

Author Comment

by:Skerw
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

10 Experts available now in Live!

Get 1:1 Help Now