Microsoft.Office.Core
Microsoft.Office.Interop.Word
添加两个定义
using MSO = Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word;
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 的wordprivate 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博客 ” ,链接:https://blog.itpub.net/15681893/viewspace-750819/ ,如需转载,请注明出处,否则将追究法律责任。