Click here to Skip to main content
15,998,003 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
private void button1_Click(object sender, System.EventArgs e)
      {
      //Invoke and open file dialog to get Word, RTF, text file path name
     OpenFileDialog opd = new OpenFileDialog();
     opd.InitialDirectory = "c:\\";
     opd.Filter = "Word文档(*.doc)|*.doc|Text(*.txt)|*.txt|RTF File(*.rtf)|
		*.rtf|All files(*.*)|*.*";
     opd.FilterIndex = 1;

     if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
      {   
 
       // Create Word class sample
       Word.ApplicationClass app = new Word.ApplicationClass();
       Word.Document doc = null;
       object missing = System.Reflection.Missing.Value;

       object FileName = opd.FileName;
       object readOnly = false;
       object isVisible = true;
       object index = 0;
    try
      {
       doc = app.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, ref missing,
        ref missing, ref missing, ref missing);

       doc.ActiveWindow.Selection.WholeStory();
       doc.ActiveWindow.Selection.Copy(); 
       // Get data from clipboard
       IDataObject data=Clipboard.GetDataObject();
       this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
      }
   finally
   {
    if (doc != null)
    {
     doc.Close(ref missing, ref missing, ref missing);
     doc = null;
    }

    if (app != null)
     {
      app.Quit(ref missing, ref missing, ref missing);
      app = null;
     }
   }
 }
}

please ask why data is null in this program? i write the filename exactly. thank you!
novel
2013-1-10
Posted
Updated 10-Jan-13 2:03am
v2
Comments
bbirajdar 10-Jan-13 9:34am    
Does it work with only English in the filter??
Sergey Alexandrovich Kryukov 10-Jan-13 9:59am    
What do you mean by .doc? Microsoft Word files? They were proprietary and, thanks goodness, are no long gone, supported only as legacy...
—SA

It's not certain from your code that data is null - it could be the data.GetData(DataFormats.Text) that returns null and causes an exception - you should check in your code and make sure that neither of them are null before continuing.

But can I say that the way you are doing it is wrong - there must be a better way to access the data, as going via the Clipboard is a bad sign, and is guaranteed to annoy many users who (like me) use the clipboard extensively. If you start changing the contents without me knowing, your application is going to get removed from my system with extreme prejudice!
 
Share this answer
 
This code worked for me once I'd removed the 2 characters after opd.Filter = "Word## (but I do get a compliler warning about ambiguity on the doc.Close call)

Try adding a Catch block to the try/finally to display any error message that might be displayed - you're ignoring them at the moment which is not good practice.

If that doesn't give any obvious answer then use debug in the IDE to confirm that doc is not null - which may indicate that it can't find the filename.
 
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