如何在asp.net的文本框中打开Word文件 [英] How to open a word file in a text box in asp.net

查看:78
本文介绍了如何在asp.net的文本框中打开Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在文本框中打开一个Word文件.我正在使用此代码,但它给了我以下错误:

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:

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.

推荐答案




我试图重现您的错误,但没有成功.
您可以看一下我的源代码.我和你的之间有一些区别,但是可以.


我已经对其进行测试的Microsoft.Office.Interop.Word库的版本为14.0.0.0,并且已在Office 2010中创建了一个文档并将其另存为97-2003 word文档. />

我教过,如果您的文档中只有一个图像,则可能会导致一个空异常,但不会.它给您一个空字符串.


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.


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.


您好,

请点击下面的链接,对此已经进行了讨论!
http://www.daniweb.com/web-development/aspnet/threads/265281 [ ^ ]
Hi,

Follow the link below already having conversation on this!
http://www.daniweb.com/web-development/aspnet/threads/265281[^]


请勿尝试使用剪贴板.文本框只能显示纯文本.而不是颜色和文本格式之类的图形格式.这样您就可以从Word文件中读取文本并将其显示在文本框中
稍微更改代码以从Word文件中获取所有文本

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

txtshowfile.Text = docs.Contents.Text;


这篇关于如何在asp.net的文本框中打开Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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