Click here to Skip to main content
11,920,211 members (49,879 online)
Rate this:
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;
       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);
       // Get data from clipboard
       IDataObject data=Clipboard.GetDataObject();
    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!
Posted 10-Jan-13 2:52am
Edited 10-Jan-13 3:03am
aspnet_regiis -i 10-Jan-13 9:34am
Does it work with only English in the filter??
What do you mean by .doc? Microsoft Word files? They were proprietary and, thanks goodness, are no long gone, supported only as legacy...
Rate this: bad
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!
Rate this: bad
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.

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

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
3 F-ES Sitecore 130
4 RyanDev 120
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2015
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