如何:向 Excel 中的快捷菜单添加命令
此示例演示如何使用应用程序级外接程序向 Excel 中的快捷菜单添加命令。 此快捷菜单在您右击工作簿单元格时显示。 当最终用户单击该命令时,会将所有选定单元格中包含的文本写入一个文本文件中。
**适用于:**本主题中的信息适用于 Excel 2007 和 Excel 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能。
将下列代码添加到 Excel 应用程序级外接程序项目中的 ThisAddIn 类中。
示例
Private WithEvents writeToText As Office.CommandBarButton
Private selectedCells As Excel.Range
Private Sub ThisAddIn_Startup(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles Me.Startup
DefineShortcutMenu()
End Sub
Private Sub DefineShortcutMenu()
Dim menuItem As Office.MsoControlType = Office.MsoControlType.msoControlButton
writeToText = Application.CommandBars("Cell").Controls.Add(Type:=menuItem, _
Before:=1, Temporary:=True)
writeToText.Style = Office.MsoButtonStyle.msoButtonCaption
writeToText.Caption = "Write to a Text File"
writeToText.Tag = "0"
End Sub
Private Sub Application_SheetBeforeRightClick(ByVal Sh _
As Object, ByVal Target As Excel.Range, _
ByRef Cancel As Boolean) Handles Application.SheetBeforeRightClick
selectedCells = Target
End Sub
Private Sub writeToText_Click(ByVal Ctrl As Office.CommandBarButton, _
ByRef CancelDefault As Boolean) Handles writeToText.Click
Try
Dim currentDateTime As System.DateTime = _
System.DateTime.Now
Dim dateStamp As String = _
currentDateTime.ToString("dMMMMyyyy_hh.mm.ss")
Dim fileName As String = System.Environment.GetFolderPath( _
Environment.SpecialFolder.MyDocuments) & "\\" & _
dateStamp & ".txt"
Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter(fileName)
For Each cell As Excel.Range In selectedCells.Cells
If cell.Value2 IsNot Nothing Then
sw.WriteLine(cell.Value2.ToString())
End If
Next
sw.Close()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
private Office.CommandBarButton writeToText;
private Excel.Range selectedCells;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
DefineShortcutMenu();
Application.SheetBeforeRightClick +=
new Excel.AppEvents_SheetBeforeRightClickEventHandler
(Application_SheetBeforeRightClick);
writeToText.Click +=
new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
(writeToText_Click);
}
void writeToText_Click(Office.CommandBarButton Ctrl,
ref bool CancelDefault)
{
try
{
System.DateTime currentDateTime = System.DateTime.Now;
string dateStamp = currentDateTime.ToString("dMMMMyyyy_hh.mm.ss");
string fileName =
System.Environment.GetFolderPath
(Environment.SpecialFolder.MyDocuments) + "\\\\" + dateStamp + ".txt";
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName);
foreach (Excel.Range cell in selectedCells.Cells)
{
if (cell.Value2 != null)
{
sw.WriteLine(cell.Value2.ToString());
}
}
sw.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
void Application_SheetBeforeRightClick(object Sh,
Excel.Range Target, ref bool Cancel)
{
selectedCells = Target;
}
private void DefineShortcutMenu()
{
Office.MsoControlType menuItem = Office.MsoControlType.msoControlButton;
writeToText = (Office.CommandBarButton)Application.CommandBars["Cell"].
Controls.Add(menuItem, missing, missing, 1, true);
writeToText.Style = Office.MsoButtonStyle.msoButtonCaption;
writeToText.Caption = "Write to a Text File";
writeToText.Tag = "0";
}
可靠编程
添加事件处理程序时,必须在控件上设置 Tag 属性。 Office 使用 Tag 属性跟踪特定 CommandBarControl 的事件处理程序。 如果 Tag 属性为空,则没有正确地处理事件。
在类级别声明菜单变量,而不要在调用这些变量的方法内进行声明。 这样可以确保只要应用程序在运行,这些菜单变量就会保持在范围内。 否则,此项便会被垃圾回收移除,而事件处理程序代码也将停止工作。