我做了一个给WORD添加手绘图片的插件。
调试的时候一切正常。
但是正常执行的时候就出现问题。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
}
}以上代码 调试的时候正常。
我对WORD里的多种对象添加按钮。
最主要的是 TEXT 和 TABLE TEXT 两个
就是WORD光标处在文本位置 或者 表格里的时候 右键菜单
会出现 手绘签图字样。 TEXT的时候后边跟着一个英文的句点。 TABLE TEXT 里右键菜单 出现的是 手绘签图,后跟一个逗号。 表示这两个菜单的不同。调试的时候任何位置都可以成功调用 对话框去绘图。但是不在调试状态, 直接启动OFFICE WORD的时候 虽然在文本和 表格里 都能正常出现菜单。
但是只在文本处出现的菜单可以正常显示对话框。 而在表格里右键菜单 点击 后毫无反应。
我加了一个 MESSAGEBOX 表格里的菜单动作都无效,连MESSAGEBOX都不显示。不知何解。求教大侠。
调试的时候一切正常。
但是正常执行的时候就出现问题。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
}
}以上代码 调试的时候正常。
我对WORD里的多种对象添加按钮。
最主要的是 TEXT 和 TABLE TEXT 两个
就是WORD光标处在文本位置 或者 表格里的时候 右键菜单
会出现 手绘签图字样。 TEXT的时候后边跟着一个英文的句点。 TABLE TEXT 里右键菜单 出现的是 手绘签图,后跟一个逗号。 表示这两个菜单的不同。调试的时候任何位置都可以成功调用 对话框去绘图。但是不在调试状态, 直接启动OFFICE WORD的时候 虽然在文本和 表格里 都能正常出现菜单。
但是只在文本处出现的菜单可以正常显示对话框。 而在表格里右键菜单 点击 后毫无反应。
我加了一个 MESSAGEBOX 表格里的菜单动作都无效,连MESSAGEBOX都不显示。不知何解。求教大侠。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货