Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 13-Mar-13 6:43am
Edited 13-Mar-13 6:46am
v2
Comments
ThePhantomUpvoter at 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 at 13-Mar-13 14:49pm
   
In what line the exception is thrown?
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 495
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 200
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 13 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100