Nasıl Yapılır: Excel'de Kısayol Menülerine Komut Ekleme
Bu örnekte, uygulama düzeyi eklentisi kullanarak Excel'deki kısayol menüsüne komutun nasıl ekleneceği gösterilir. Kısayol menüsü, çalışma sayfasını sağ tıklattığınızda görünür. Son Kullanıcı komuta tıkladığında, seçili tüm hücreleri içeren metin, metin dosyasına yazılır.
Uygulama alanı: Bu konudaki bilgiler Excel 2007 ve Excel 2010 uygulamalarının belge düzeyi projelerine ve uygulama düzeyi projelerine yöneliktir. Daha fazla bilgi için bkz. Office Uygulamalarında Kullanılabilir Özellikler ve Proje Türü.
Excel için uygulama düzeyi projesi içinde ThisAddIn sınıfına aşağıdaki kodu ekleyin.
Örnek
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";
}
Güçlü Programlama
Olay işleyicileri eklediğinizde, denetimlerinizin Tag özelliğini ayarlamalısınız. Office, belirli CommandBarControl'ü için olay işleyicilerinin kaydını tutmak amacıyla Tag özelliğini kullanır. Eğer Tag özelliği boşsa, olaylar düzgün olarak işlenmez.
Menü değişkenlerinizi, çağırıldıkları yöntemin içi yerine sınıf düzeyinde bildirin. Bu, uygulama çalıştığı sürece menü değişkenlerinizin kapsam içinde kalmasını sağlar. Aksi durumda, öğe atık toplama tarafından kaldırılır ve olay işleyici kodunuz çalışmaz.
Ayrıca bkz.
Görevler
Nasıl Yapılır: Office Araç Çubuğu Oluşturma
İzlenecek Yol: Yer İşaretleri İçin Kısayol Menüleri Oluşturma
Nasıl Yapılır: Word'de Kısayol Menülerine Komut Ekleme
Nasıl Yapılır: Özel menüler ve menü öğelerini Outlook'a ekleyin.
Nasıl Yapılır: Araç Kutusuna Özel Simge Ekleme ve Menü Öğeleri
Kavramlar
Office Çözümlerinde İsteğe Bağlı Parametreler