------------------------------------------------------打开文档-------------------------------------------------------------------
string strmubanPath = @"d:/muban.doc";//得到模板位置
string strkongbaiPath = strPath + @"/kongbai.doc";//得到要编辑的文档位置
Object refmissing = System.Reflection.Missing.Value;
object oDocument = null;
webBrowser2.Navigate(strmubanPath);//, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
webBrowser1.Navigate(strkongbaiPath);
-------------------------------------------------------保存文档---------------------------------------------------------------------------
需要添加引用 Interop.Word.dll
object axWebBrowser; //定义web浏览器
object oApplication;//
axWebBrowser = this.webBrowser1.ActiveXInstance;
oApplication = axWebBrowser.GetType().InvokeMember("Document", System.Reflection.BindingFlags.GetProperty, null, axWebBrowser, null);//通过反射,获得word应用程序实例
Word.Document doc = (Word.Document)oApplication;
Word.ApplicationClass word1;
word1 = doc.Application as Word.ApplicationClass;
word1.ActiveDocument.Save();
PS:如果webBrowser1不在当前选中的窗体中,或者不在当前选项卡中,好像保存无效。
----------------------------------------------------------清空文档---------------------------------------------------------------
//该方法好像并没有真正的释放Word进程,文件依然被占用,目前还没有找到好的解决办法。
webBrowser1.Navigate("about:blank");
webBrowser2.Navigate("about:blank");
----------------------------------------------------------在office 2007中打开---------------------------------------------------------------
//如果装的是office 2007的话,用webBrowser打开Word文件时,不会在webBrowser中打开,而是直接打开了Word程序,解决办法是修改注册表选项,将以下内容保存到记事本中,存为reg格式的文件运行即可
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Word.Document.8] "BrowserFlags"=dword:80000024
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Word.RTF.8] "BrowserFlags"=dword:80000024
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Word.Document.12] "BrowserFlags"=dword:80000024
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Word.DocumentMacroEnabled.12] "BrowserFlags"=dword:80000024
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Excel.Sheet.8] "BrowserFlags"=dword:80000A00
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Excel.Sheet.12] "BrowserFlags"=dword:80000A00
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Excel.SheetMacroEnabled.12] "BrowserFlags"=dword:80000A00
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Excel.SheetBinaryMacroEnabled.12] "BrowserFlags"=dword:80000A00
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.Show.8] "BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.Show.12] "BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.ShowMacroEnabled.12] "BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.SlideShow.8] "BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.SlideShow.12] "BrowserFlags"=dword:800000A0
[HKEY_LOCAL_MACHINE/SOFTWARE/Classes/PowerPoint.SlideShowMacroEnabled.12] "BrowserFlags"=dword:800000A0