Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to open a word file in a text box. I am using this code but it is giving me the following error:

Object reference not set to an instance of an object


Code snippet:
C#
Microsoft.Office.Interop.Word.ApplicationClass wordObject=new Microsoft.Office.Interop.Word.ApplicationClass();
                object File=txtshowfile.Text; //this is the path
                object nullobject=System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document docs=wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                    ref nullobject, ref nullobject, ref nullobject, ref nullobject);
                docs.ActiveWindow.Selection.WholeStory();
                docs.ActiveWindow.Selection.Copy();
                IDataObject data = Clipboard.GetDataObject();
                txtshowfile.Text = data.GetData(DataFormats.Text).ToString();
                docs.Close(ref nullobject,ref nullobject,ref nullobject);


it takes null in txtshowfile.Text file.
Posted
Updated 4-Jan-12 0:13am
v4
Comments
Slacker007 4-Jan-12 6:10am    
Edits: formatting, tag, and readability.

Hi,


I've tried to reproduce your error, but with no success.
You can look at my source code. There are a few difference between mine and yours, but it works.


The version of the Microsoft.Office.Interop.Word library, that i've test it, is 14.0.0.0, and i've created a document in Office 2010 and save it as a 97-2003 word document.


I taught that if you have in your document only an image, maybe it will cause a null exception, but it dosen't. It gives you an empty string.


C#
object File = @"c:\WordFile.doc";
            object nullobject = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application();
            _Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject,
                ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                ref nullobject, ref nullobject, ref nullobject, ref nullobject,
                ref nullobject, ref nullobject, ref nullobject, ref nullobject);
            
            docs.ActiveWindow.Selection.WholeStory();
            docs.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();

            string wordString = data.GetData(DataFormats.Text).ToString();
            Debug.WriteLine(wordString);

            docs.Close(ref nullobject, ref nullobject, ref nullobject);


Hope that this helps you.
 
Share this answer
 
Comments
mayankshrivastava 4-Jan-12 6:54am    
it gives error
<pre lang="text">Debug does not exist in current context</pre>
incaunu 4-Jan-12 7:09am    
In your using statements put: using System.Diagnostics;
Or you can delete that line, and modify the code to your needs.
Hi,

Follow the link below already having conversation on this!
http://www.daniweb.com/web-development/aspnet/threads/265281[^]
 
Share this answer
 
do not try to use Clipboard. Textbox is capable of showing only plain text. not graphical formats like color and textformatting. so you can read text from word file and show it in textbox
change your code a little to get all text from your word file

C#
txtshowfile.Text = docs.Contents.Text;
 
Share this answer
 
Comments
mayankshrivastava 4-Jan-12 6:21am    
it doesn't work ..
same error
mayankshrivastava 4-Jan-12 6:24am    
it gives me error in that line-
docs.ActiveWindow.Selection.WholeStory();
mayankshrivastava 4-Jan-12 6:24am    
<pre lang="c#">
docs.ActiveWindow.Selection.WholeStory();
</pre>

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