Opening a word document in C# is actually not too difficult, and there are many methods.
1. How to open a word document in C#
//Add a reference to the Microsoft Word 11.0 object library in the project reference
private void button1_Click(object sender, System.EventArgs e)
{
//Call the open file dialog box Get the WORD file, RTF file, text file path name of the file to be opened
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = "c:\\";
opd.Filter = "Word document (*.doc)| *.doc|Text document (*.txt)|*.txt|RTF document (*.rtf)|*.rtf|All documents (*.*)|*.*";
opd.FilterIndex = 1;
if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
// Create an instance of the Word class, disadvantages: cannot correctly read the display of tables, pictures, etc.
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = opd.FileName;
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();
//Clip from Clip board to get data
IDataObject data=Clipboard.GetDataObject();
this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
}
{
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;[Page]
}
But, what if we use javascript to open it? Actually, it's not difficult.
2. Open the word document www.2cto.com in javascript
We create a new html file and write a FileUpLoad and button control.
flUpload
Then, Writing a javascript OpenFile method.
function OpenFile()
jExcel = new ActiveXObject(" Excel.Application");
objExcel.Visible = true; Id("flUpload").value. toUpperCase().indexOf(".DOC") != -1)
objDoc.Documents.Open(document .getElementById("flUpload").value);
.
OK. Then in IE, you can first select a doc document, then click open to open it.
Hope it helps you.