Add to Google Add to My Yahoo!

Live Chat Here


Articles on ASP.NET

Tuesday, May 8, 2007

Reading WORD DOCUMENTS FOR ASP.net


using

Word;

using

System.Reflection;

public

partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

DocToHtml(

@"c:\a.doc", @"c:\a.html");

}

void DocToHtml(string docPath, string htmlPath)

{

Application app = new Application ();

app.Visible =

false;

Object o = Missing.Value;

object docFile = docPath;

_Document doc = app.Documents.Open(ref docFile, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);

object fileName = htmlPath;

object format = 8;//Html

doc.SaveAs(

ref fileName, ref format, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);

object t = true;

app.Quit(

ref t, ref o, ref o);

}

}


--
Regards,
Munish Kalra

http://kalraonaspnet.blogspot.com
http://munishmca.co.nr

HOW to read word document in .net(untested yet)


1.The First Step is that you'll need to add a COM reference to your project by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 9.0 Object Library. Click Select and OK.

In Form_Load write this code:-
2.private void Form1_Load(object sender, System.EventArgs e)
{
Word.Application app = new Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
object file = @"C:\Suneetha\Ques.doc";
Word.Document doc = app.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,
ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string text = data.GetData(DataFormats.Text).ToString();
Console.WriteLine(text);
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);


}

--
Regards,
Munish Kalra

http://kalraonaspnet.blogspot.com
http://munishmca.co.nr