將MS Word 嵌入C# Winform 中

來源:互聯網
上載者:User

從今天開始要進入我的C#學習生涯了,~~~~(>_<)~~~~

我導師讓我搞個小任務,哎!對於C#我什麼也不懂,於是就上網search ,好像網上就這一個版本,不過我可是搞了半天才把問題解決掉,先說說整個過程是怎麼樣的吧!

 

在 Visual C# .NET 中建立一個 Windows 應用程式項目。預設情況下會建立 Form1。
工具菜單上,單擊自訂工具箱以開啟自訂工具箱對話方塊。在 COM 組件選項卡上,添加一個對 Microsoft WebBrowser 的引用。單擊確定,將 WebBrowser 控制項添加到 Windows 表單工具箱。WebBrowser 控制項會顯示出來,並且在工具箱中帶有 Explorer(資源管理員)字樣。
使用該工具箱向 Form1 添加一個 WebBrowser 控制項、一個 OpenFileDialog 控制項和一個 CommandButton 按鈕。這就會向 Form1 類添加 AxWebBrowser1、OpenFileDialog1 和 Button1 成員變數。
在 Form1 上,雙擊 button1。這就會向 Form1 添加 Button1_Click 事件。
在 Form1 的代碼視窗中,向列表添加以下命名空間:using System.Reflection;

如下所示在 Form1 類中定義一個私人成員:private Object oDocument;
在 Form1 類的 InitializeComponent 方法的末尾【註:可以在VS裡直接雙擊這個方法進入定義的模組然後按下面做】,添加以下代碼以處理 Form1_Load、Form1_Closed 和 axWebBrowser1_NavigateComplete2 事件:this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);

將下面的代碼private void button1_Click(object sender, System.EventArgs e)
{
}

替換為: private void button1_Click(object sender, System.EventArgs e)
{

String strFileName;

//Find the Office document.
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
strFileName = openFileDialog1.FileName;

//If the user does not cancel, open the document.
if(strFileName.Length != 0)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing);
}
}

 

public void Form1_Load(object sender, System.EventArgs e)
{
button1.Text = "Browse";
openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
openFileDialog1.FilterIndex = 1;
}

public void Form1_Closed(object sender, System.EventArgs e)
{
oDocument = null;
}

public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{

//Note: You can use the reference to the document object to
// automate the document server.

Object o = e.pDisp;

oDocument = o.GetType().InvokeMember("Document",BindingFlags.GetProperty,null,o,null);

Object oApplication = o.GetType().InvokeMember("Application",BindingFlags.GetProperty,null,oDocument,null);

Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);

MessageBox.Show("File opened by: " + oName.ToString() );
}

按 F5 鍵運行該項目。單擊瀏覽後,會出現開啟對話方塊,您可以使用該對話方塊瀏覽到 Word 文檔、Excel 工作表或 PowerPoint 簡報。選擇任一檔案,然後單擊開啟。文檔在 WebBrowser 控制項內開啟,並出現一個顯示有 Office 文檔伺服器名稱的訊息框。

我搞了好久才得到正確的結果,我錯誤的原因就在於開始時沒有添加COM組件裡的WebBrowser,把控制項工具箱裡的那個WebBrowser誤認為直接放在表單裡就可以顯示了,所以運行不出來,希望大家細心點O(∩_∩)O,另外就是對這個表單的設計顯示不是很滿意,比如說如何設定這個控制項隨表單的大小而自動改變大小;還有就是大家可以看下啟動並執行結果,很不美觀,word被開啟時直接顯示的是批註格式,你必須單擊右鍵轉到常用格式才可以方便使用,而且很多功能也被限制,如果有人能有好的解決方案,希望能交流一下 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.