110,499
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using System.Windows.Forms;
namespace WordMyPic
{
public partial class ThisAddIn
{
private Office.CommandBarButton myControl;
private Office.CommandBarButton myControl2;
private Office.CommandBarButton myControl3;
private Office.CommandBarButton myControl4;
private Office.CommandBarButton myControl5;
private Office.CommandBarButton myControl6;
private Word.Application myApplication;
private void AddMenuItem()
{
Office.MsoControlType menuItem = Office.MsoControlType.msoControlButton;
Office.MsoControlType menuItem2 = Office.MsoControlType.msoControlButton;
Office.MsoControlType menuItem3 = Office.MsoControlType.msoControlButton;
Office.MsoControlType menuItem4 = Office.MsoControlType.msoControlButton;
Office.MsoControlType menuItem5 = Office.MsoControlType.msoControlButton;
Office.MsoControlType menuItem6 = Office.MsoControlType.msoControlButton;
string poo="";
for(int pp=1;pp<= myApplication.CommandBars.Count;pp++)
{
poo= myApplication.CommandBars[pp].Name +"\r\n"+ poo;
}
myControl =
(Office.CommandBarButton)myApplication.CommandBars["Text"].Controls.Add
(menuItem, missing, missing, 1, true);
myControl.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
myControl.Caption = "手绘签图.";
myControl.Tag = "MyMenuItem_WMP_Text";
myControl.Click += new Office._CommandBarButtonEvents_ClickEventHandler(myControl_Click);
myControl2 =
(Office.CommandBarButton)myApplication.CommandBars["Table Text"].Controls.Add
(menuItem2, missing, missing, 1, true);
myControl2.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
myControl2.Caption = "手绘签图,";
myControl2.Tag = "MyMenuItem_WMP_Table";
myControl2.Click += new Office._CommandBarButtonEvents_ClickEventHandler(myControl_Click);
myControl3 =
(Office.CommandBarButton)myApplication.CommandBars["Linked Text"].Controls.Add
(menuItem3, missing, missing, 1, true);
myControl3.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
myControl3.Caption = "手绘签图...";
myControl3.Tag = "MyMenuItem_WMP_Link";
myControl3.Click += new Office._CommandBarButtonEvents_ClickEventHandler(myControl_Click);
myControl4 =
(Office.CommandBarButton)myApplication.CommandBars["Header and Footer"].Controls.Add
(menuItem4, missing, missing, 1, true);
myControl4.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
myControl4.Caption = "手绘签图....";
myControl4.Tag = "MyMenuItem_WMP_HAF";
myControl4.Click += new Office._CommandBarButtonEvents_ClickEventHandler(myControl_Click);
GC.Collect();
}
void myControl_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
IDataObject PID = Clipboard.GetDataObject();
System.Windows.Forms.MessageBox.Show("aa");
MyText MT = new MyText();
if ((myApplication.Selection.End - myApplication.Selection.Start) > 0)
{
myApplication.Selection.Range.CopyAsPicture();
MT.HasCopyPic = true;
}
// MT.PXZ = zzz;
if (System.Windows.Forms.DialogResult.OK == MT.ShowDialog())
{
try
{
if (MT.TransBG)
{
myApplication.Selection.InlineShapes.AddPicture(MT.TextBody, missing, missing, missing);
}
else
{
myApplication.Selection.Range.Paste();
}
}catch( Exception zzze)
{
System.Windows.Forms.MessageBox.Show(zzze.ToString());
}
}
MT.Dispose();
}
private bool RemoveMenuItem()
{
Office.CommandBar contextMenu = myApplication.CommandBars["Text"];
Office.CommandBar contextMenu2 = myApplication.CommandBars["Table Text"];
Office.CommandBar contextMenu3 = myApplication.CommandBars["Linked Text"];
Office.CommandBar contextMenu4 = myApplication.CommandBars["Header and Footer"];
Office.CommandBar contextMenu5 = myApplication.CommandBars["Floating Picture"];
Office.CommandBar contextMenu6 = myApplication.CommandBars["Inline Picture"];
bool zz = false;
Office.CommandBarButton control = (Office.CommandBarButton)contextMenu.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_Text", true, true);
Office.CommandBarButton control2 = (Office.CommandBarButton)contextMenu2.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_Table", true, true);
Office.CommandBarButton control3 = (Office.CommandBarButton)contextMenu3.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_Link", true, true);
Office.CommandBarButton control4 = (Office.CommandBarButton)contextMenu4.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_HAF", true, true);
Office.CommandBarButton control5 = (Office.CommandBarButton)contextMenu5.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_Picture", true, true);
Office.CommandBarButton control6 = (Office.CommandBarButton)contextMenu6.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem_WMP_ILPicture", true, true);
if ((control != null))
{
control.Delete(true); zz = true;
}
if ((control2 != null))
{
control2.Delete(true); zz = true;
}
if ((control3 != null))
{
control3.Delete(true); zz = true;
}
if ((control4 != null))
{
control4.Delete(true); zz = true;
}
if ((control5 != null))
{
control5.Delete(true); zz = true;
}
if ((control6 != null))
{
control6.Delete(true); zz = true;
}
Office.CommandBarButton controlttt = (Office.CommandBarButton)contextMenu.FindControl(Office.MsoControlType.msoControlButton, missing, "MyMenuItem", true, true);
if ((controlttt != null))
{
controlttt.Delete(true);
}
return zz;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myApplication = Application;
for (int z = 0; z < 100; z++)
{
if (RemoveMenuItem() == false)
{
break;
}
}
AddMenuItem();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}