Archive for the ‘Dot net’ Category
C#: Opens Office Documents in Web Browser Control
This post is related to/same as a previous post VB: Opens Office Documents in Web Browser Control
Use the following steps to create a C# application that opens Office documents:
- Start Visual Studio and create a new C# project. Form1 created by default.
- From the Components toolbox, Add an instance of the WebBrowser control, CommonDialog control, and a CommandButton to Form1.
- Next, add the following code into the Code window for Form1:
object oDocument; private void button1_Click(object sender, EventArgs e) { string sFileName; openFileDialog1.FileName = ""; openFileDialog1.ShowDialog(); sFileName = openFileDialog1.FileName; if (sFileName.Length!=0) { oDocument = null; webBrowser1.Navigate(sFileName); } } private void Form1_Load(object sender, EventArgs e) { openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt"; openFileDialog1.FilterIndex = 1; } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { oDocument = webBrowser1.Document; }
- Press F5 to run the project. When you select the Browse button, the Open dialog box appears allowing you to navigate to a Word, Excel or PowerPoint file.
- Choose Open and the document should open inside the WebBrowser control.

