1,用窗体应用程序生成一个word
2,生成一个word的comAddIns
---------------------------------------------------------
新建一个项目
visual c#
windows
windows窗体应用程序
Word2010Demo
添加两个引用
Microsoft.Office.Core
Microsoft.Office.Interop.Word
添加两个定义
using MSO = Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word;
增加一个button
button text :“操作Word示例”
botton name :“btnWordApp”
事件:
Word.Application WordApp = new Word.Application(); WordApp.Visible = true; Word.Document WordDoc = WordApp.Documents.Add(); WordDoc.Content.Text = "我用C#调用word"; WordDoc.SaveAs2("c:\\1.doc"); ((Word._Document)WordDoc).Close(); ((Word._Application)WordApp).Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
编译
运行
自动在C生成一个内容为:我用C#调用word 的word
------------------------------------------------------------------------------
新建项目
其他项目类型
扩展性
共享的外接程序
WordDemo
引用和using同上
添加私有变量
private MSO.CommandBarButton popupButton;
OnDisconnection函数
添加
try { (applicationObject as Word.Application).CommandBars["Text"].Reset(); } catch (System.Exception ex) { Console.WriteLine(ex); } finally { }
OnStartupComplete函数添加
MSO.CommandBars commandbars = (applicationObject as Word.Application).CommandBars; MSO.CommandBar fontRightPopup = commandbars["Text"]; fontRightPopup.Reset(); popupButton = (MSO.CommandBarButton)fontRightPopup.Controls.Add(Before: 1); popupButton.Caption = "在此处kgs"; popupButton.Style = MSO.MsoButtonStyle.msoButtonCaption; popupButton.Tag = "k"; popupButton.Click += new MSO._CommandBarButtonEvents_ClickEventHandler(popupButton_Click);
通过 tab两次增加的_CommandBarButtonEvents_ClickEventHandler函数
void popupButton_Click(MSO.CommandBarButton Ctrl, ref bool CancelDefault) { (applicationObject as Word.Application).Selection.TypeText("kgs"); }
编译WordDemoSetup可以生成安装程序让别的机器也可以使用
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15681893/viewspace-750819/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15681893/viewspace-750819/