1.c#,winForm嵌入word并能操作文档
在 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 方法的末尾,添加以下代码以处理 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 文档服务器名称的消息框。
2.winform读取word文件
在Form里加一个button按钮和一个richtextbox,给按钮加一个Click事件private void button1_Click(object sender, EventArgs e) { object filepath= "D:\\Debugtest.doc"; openWord(filepath); }//把Word文档内容取出来放到richtextBox里 private void openWord(object SPath) { object file = SPath; object nullobj = System.Reflection.Missing.Value; Word.Document doc = myWordApp.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,ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString(); doc.Close(ref nullobj, ref nullobj, ref nullobj);}。
3.C# winform 打开word报错
无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此操作失败的原因是对 IID 为“{00020970-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 找不到 。 (异常来自 HRESULT:0x80030002 (STG_E_FILENOTFOUND))。
上次是 HRESULT:0x80004002(E_NOINTERFACE),修复了一下office变成如上所示。
出现原因:可能是安装office时安装过不同版本造成的。
本人安装的是office 2003,并安装过office2007的兼容包,也安装过office visio 2007
解决办法:
在注册表中找到:HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.6 删除之就可以了。
因为我用的是office 2003对应的是1.5,所以删除1.6
office 2007对应的是1.6,所以要根据情况判断。(我的是安装了2007之后有安装了2010,10可以,07不可以,我后来卸载掉10,用07版测试发现该注册表项下1.7还在,所以删除掉1.7,07版就正常了)
后记:
一般来说,一个COM对象在提供的时候,通常还会提供一个类型库,在其中定义了COM对象的所有方法名称、参数名称、属性名称等等信息。我们要做的就是从类型库中取出这些信息。当然,某些只供C++程序员使用的COM对象没有类型库,而代之以C++的头文件和/或idl文件,对这种情况,一般没有办法在程序中枚举出对象的方法属性:毕竟去找C++头文件不太现实,何况在非开发环境下,根本就没有头文件的说。
在一般情况下,COM对象的TypeLib信息存储在注册表中:在HK_CLASSROOT\CLSID\{ClassID}\的注册表项下,有一个名为TypeLib的子项,其中定义了这个COM对象类型库的ID;而在HK_CLASSROOT\TypeLib 注册表项下,列举了系统中所有TypeLib。
4.如何在C#中读取Word文档
1.新建一个winform工程,添加两个button控件和一个textbox控件。
2.添加引用com文件Microsoft word 11.0 Object Library
并在代码中添加命名空间using Word //注意首字母大写
3.定义全局变量
//创建word
_Application app=new Word.Application();
//创建word文档
_Document doc=null;
注意word和word文档是不同的
4.通过文件打开对话框获取word文件所在位置,代码如下
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "word文件|*.doc";
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
}
打开word,代码如下
object unknow = Type.Missing;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
5.读取word文档
string temp = doc.Paragraphs[i].Range.Text.Trim();//变量i为第i段
textBox1.Text = temp; //将第i段内容复制到textbox中
5.请问用 winform 打开远程的word 怎么玩 谁玩过啊
不知道,满哥。
利用中文Word提供的“插入”功能直接在文档中插入一个超级链接是创建超级链接最基本的方法,其具体的操作步骤为:
将光标移至希望插入超级链接的位置,单击“插入”菜单栏中的“超级链接”命令,打开“插入超级链接”对话框,在“链接到”列表框中选择适当的链接范围(如“本文档中的位置”、“新建文档”、“Web页”等),这里我们选择“原有文件或Web页”选项。 在“要显示的文字”栏中输入超级链接的名称(如“首都在线”)。在“请键入文件名或Web页名称”栏中输入超级链接的网址(这里应输入首都在线的网址 )。单击“屏幕提示”按钮,打开“设置超级链接屏幕提示”窗口。输入系统对该超级链接的屏幕提示(如“263”)。单击“确定”按钮,关闭所有对话框 这样,首都在线的网址就会作为超级链接插入到用户正在编辑的文档中,它在默认情况下会采用蓝色、并加下划线方式显示“首都在线”几个字样,我们只需将鼠标放到该超级链接上,系统就会显示出用户设置的屏幕提示“263”,而单击该链接则可启动系统默认的浏览器打开首都在线的网址(链接其他文档或网址的效果与此完全一样),使用十分方便。
。。。PS:来凑人数的。。
6.如何C#操作word模板生成word文档并打开
codeproject上有很多操作word的C#代码:顺便给你几个链接:/KB/office/Word_Automation.aspx/KB/office/Word2007Automation.aspx/KB/office/csautomateword.aspxprivate void met_word_automation(){ try { // Declaring the object variables we will need later object varFileName = "c:\temp\doc.docx"; object varFalseValue = false; object varTrueValue = true; object varMissing = Type.Missing; string varText; // Create a reference to Microsoft Word application Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application(); // Creates a reference to a Word document Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); // Activate the document varDoc.Activate(); // Change the 1st sentence of the document varText = "Altered sentence nr. 1"; varDoc.Sentences[0].Text = varText; // Change the 3rd sentence of the document varText = "Altered sentence nr. 3"; varDoc.Sentences[2].Text = varText; // Save the document varDoc.Save(); // Show the Microsoft Word window with our document on it varWord.Visible = true; // Call the print dialog in Word Microsoft.Office.Interop.Word.Dialog varDlg = varWord.Application.Dialogs[ Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint]; varDlg.Show(ref varMissing); // Print the document varDoc.PrintOut(ref varTrueValue, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing); // Send mail with this document as an attachment varDoc.SendMail(); } catch (Exception varE) { MessageBox.Show("Error:\n" + varE.Message, "Error message"); }}You need to create references to this .NET namespace in order to use the above code: Microsoft.Office.Interop.Word。
转载请注明出处51数据库 » winform打开word