Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I am using the Mirosoft office interop for reading data from the word document placed on the local system but when I execute the code it opens a window saying open in read-only mode and some other option when I select it opens a actual word document but it fails to fetch the data and cause the exception.
Object reference not set to an instance of an object.
The file is present and path is also valid.
[Removed urgency comment]

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object file = Filename;
object readOnly=false;
object nullobj = System.Reflection.Missing.Value;
Document docs = wordApp.Documents.Open(
                        ref file, ref nullobj, ref readOnly,
                     ref nullobj, ref nullobj, ref nullobj,
                     ref nullobj, ref nullobj, ref nullobj,
                     ref nullobj, ref nullobj, ref nullobj,
                     ref nullobj, ref nullobj, ref nullobj, ref nullobj);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();//data is null when I debug the application
FileContent = data.GetData(DataFormats.Text).ToString();//exception occured here 'Object reference not set to an instance of an object.'
Posted
Updated 13-Mar-13 5:46am
v2
Comments
[no name] 13-Mar-13 11:53am    
Maybe your web application does not have permission to access the servers clipboard? Seems like that would be a huge security hole anyway.....
Sergey Alexandrovich Kryukov 13-Mar-13 14:49pm    
In what line the exception is thrown?
—SA

System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
This means that data is NULL and thus when you tried data.GetData , you got an error.
I guess, you have not set any data in clipborad before doing get data. Hence nothing was returned. Try to setData first and then getData. An example here: MSDN: Clipboard Class[^]


In general, Object reference not set to an instance of an object:
This error happens when you try to use a property or call a method of an object that is null. More details: here[^]
 
Share this answer
 
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 

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