关于打开.doc flie [英] about open a .doc flie

查看:126
本文介绍了关于打开.doc flie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
     }
   }
 }
}



请问为什么此程序中的数据为空?我准确地写了文件名。谢谢!

小说

2013-1-10


please ask why data is null in this program? i write the filename exactly. thank you!
novel
2013-1-10

推荐答案

你的确不确定 data 的代码为null - 可能是返回<$ c $的 data.GetData(DataFormats.Text) c> null 并导致异常 - 你应该检查你的代码并确保在继续之前它们都不为null。



但是我可以说你做的方式是错误的 - 必须有更好的方式来访问数据,因为通过剪贴板是一个坏迹象,并保证惹恼许多用户(像我一样)广泛使用剪贴板。如果你在我不知情的情况下开始更改内容,你的应用程序将会被我的系统从极端的偏见中删除!
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!


这个代码在我删除了2个字符之后为我工作了opd.Filter =Word ##(但是我确实收到了关于doc.Close调用模糊性的compliler警告)



尝试向try /添加Catch块最后显示可能显示的任何错误消息 - 你现在忽略它们这不是一个好习惯。



如果那不给任何显而易见的答案然后在IDE中使用debug来确认doc不为null - 这可能表示它找不到文件名。
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.


这篇关于关于打开.doc flie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆