Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 10-Jan-13 1:52am
Edited 10-Jan-13 2:03am
RyanDev110.8K
v2
Comments
aspnet_regiis -i at 10-Jan-13 9:34am
   
Does it work with only English in the filter??
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

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.
  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 428
1 Sergey Alexandrovich Kryukov 283
2 PhilLenoir 115
3 RajeeshMenoth 110
4 Peter Leow 100
0 Sergey Alexandrovich Kryukov 6,465
1 OriginalGriff 5,926
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 10 Jan 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