I want code to copy word text content into texbox using c# code in web application

right now i have code for windows application.

using Microsoft.Office.Interop.Word;

private void readFileContent(string path)
    {
        Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
        object file = path;
        object nullobj = System.Reflection.Missing.Value;

        Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
            ref file, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj,
            ref nullobj, ref nullobj, ref nullobj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
       textbox1.Text = data.GetData(DataFormats.Text).ToString();
        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);

    }

This code is working in windows application....Clipbord is not there in web application....
so how to do in web application

Recommended Answers

All 3 Replies

Don't use Clipboard object. Read content of doc using,

string txt=doc.Content.Text;

Do you want to this operation in client side or server side?

Since it is a web application, you can't use the above C# code at client side. It can be done using JavaScript only.

You can try to use object.execCommand javascript method to paste the text from clipboard to the current object such as TextBox.

For example,

function PasteTextFromClipboard()
        {
            document.getElementById('textArea1').focus();
            var strText = document.getElementById('textArea1').createTextRange();
            strText.execCommand("Paste");
        }

In case if you want to do this operation in server side, check the following article. The C# code uploads a word document file and stores it into a string and from that it is placed that string into a textbox.

Read any document (like .doc, .rtf , txt ) in ASP.Net , C#

Don't use Clipboard object. Read content of doc using,

string txt=doc.Content.Text;

Thanks it helped me...U r right

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.