Click here to Skip to main content
16,015,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object not set to an instance of object error message is poping up once i placed the project on the server though it was working fine on the the local machine.

Server Error
Object reference not set to an instance of an object.
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:


C#
Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
count = aDoc.ComputeStatistics(stat, ref missing);//error occuring in this line of code.
WordApp.Quit(ref objDNS, ref missing,/ ref missing);


What could be the issue.....thanks
Posted
Updated 29-Oct-12 18:26pm
v3

1 solution

I think you are missing the 'aDoc' initialization. Try the following code.


var appWord = new Application { Visible = false };
object oMissing = Missing.Value;
Document aDoc = appWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);


Please revert back, if you face any issue.
 
Share this answer
 
Comments
MT_ 30-Oct-12 0:47am    
It can't be the case, as the OP is saying, its working fine on local. !
Member 9291223 30-Oct-12 15:12pm    
Actually im already doing what you have said.below is my code:
Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


// give any file name of your choice.
string tempath = numberofpage;
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
tempath = regex.Replace(tempath, @"");
tempath = tempath.Replace(" \\", "\\");
object fileName = tempath;
object readOnly = false;
object isVisible = true;

// the way to handle parameters you don't care about in .NET
object missing = System.Reflection.Missing.Value;
object objDNS = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

// Make word visible, so you can see what's happening
//WordApp.Visible = true;
// Open the document that was chosen by the dialog
Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName,
ref missing, ref readOnly, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref isVisible);

Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;
count = aDoc.ComputeStatistics(stat, ref missing);

WordApp.Quit(ref objDNS, ref missing, ref missing);

aDoc = null;

WordApp = null;

GC.Collect();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900