مشاركة عبر


كيفية القيام بما يلي: إضافة أوامر إلى قوائم مختصرة في Word

ينطبق على

تنطبق المعلومات الموجودة في هذا الموضوع فقط على أنواع المشاريع وإصدارات Microsoft Office التالية: لمزيد من المعلومات، راجع الميزات المتوفرة بواسطة تطبيقات Office و نوع المشروع.

نوع المشروع

  • مشروعات على مستوى المستند

  • مشروعات على مستوى التطبيق

إصدار Microsoft Office

  • Word 2007

  • Word 2010

يستخدم ‏‫Visual Studio الاندماج، تحميل تجميع.NET Framework، إلى ذاكرة التخزين المؤقتة للتجميعات قبل تحميل حلول المكتب. قد تحدث المشاكل التالية أثناء الذي تقوم بتطوير حلول باستخدام أدوات تطوير المكتب في ‏‫Visual Studio.

للحصول على مثال يوضح كيفية إنشاء قائمة مختصرة في Excel، راجع كيفية القيام بما يلي: إضافة أوامر إلى القوائم المختصرة في Excel.

قم بإضافة التعليمات البرمجية التالية إلى الفئة ThisAddIn في مشروع على مستوى التطبيق لـ Word.

لا يمكن تاريخ الإنشاء مشروعMyCustomTemplate.dotx

مثال

Private MyApplication As Word.Application
Private WithEvents myControl As Office.CommandBarButton
Private customTemplate As Word.Template

Private Sub ThisAddIn_Startup _
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    MyApplication = Me.Application

    GetCustomTemplate()
    RemoveExistingMenuItem()
    AddMenuItem()

End Sub

Private Sub GetCustomTemplate()
    Dim TemplatePath As String = Environment.GetFolderPath _
        (Environment.SpecialFolder.MyDocuments) + "\MyCustomTemplate.dotx"
    Dim install As Boolean = True

    For Each installedTemplate As Word.Template In MyApplication.Templates
        If installedTemplate.FullName = DirectCast(TemplatePath, String) Then
            install = False
        End If
    Next

    If install = True Then
        MyApplication.AddIns.Add(TemplatePath.ToString(), True)
    End If
    customTemplate = MyApplication.Templates.Item(TemplatePath)
End Sub

Private Sub RemoveExistingMenuItem()

    Dim contextMenu As Office.CommandBar = _
    MyApplication.CommandBars("Text")

    MyApplication.CustomizationContext = customTemplate

    Dim control As Office.CommandBarButton = contextMenu.FindControl _
        (Office.MsoControlType.msoControlButton, System.Type.Missing, _
         "MyMenuItem", True, True)

    If Not (control Is Nothing) Then
        control.Delete(True)
    End If

End Sub

Private Sub AddMenuItem()

    MyApplication.CustomizationContext = customTemplate

    Dim menuItem As Office.MsoControlType = _
        Office.MsoControlType.msoControlButton

    myControl = CType(MyApplication.CommandBars("Text").Controls.Add _
       (menuItem, 1, True), Office.CommandBarButton)

    myControl.Style = Office.MsoButtonStyle.msoButtonCaption
    myControl.Caption = "My Menu Item"
    myControl.Tag = "MyMenuItem"
    customTemplate.Saved = True

    GC.Collect()

End Sub


Sub myControl_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, _
                    ByRef CancelDefault As Boolean) Handles myControl.Click

    System.Windows.Forms.MessageBox.Show("My Menu Item clicked")

End Sub
private Word.Application myApplication;
private Office.CommandBarButton myControl;
private Word.Template customTemplate; 

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    myApplication = this.Application;

    GetCustomTemplate();
    RemoveExistingMenuItem();
    AddMenuItem(); 
}

private void GetCustomTemplate()
{
    object TemplatePath = Environment.GetFolderPath
        (Environment.SpecialFolder.MyDocuments) + 
        "\\MyCustomTemplate.dotx";
    object install = true;

    foreach (Word.Template installedTemplate in myApplication.Templates)
    {
        if (installedTemplate.FullName == (string)TemplatePath)
        {
            install = false;
        }
    }
    if ((bool)install)
    {
        myApplication.AddIns.Add(TemplatePath.ToString(), ref install);
    }
    customTemplate = myApplication.Templates.get_Item(ref TemplatePath);

}

private void RemoveExistingMenuItem()
{
    Office.CommandBar contextMenu = myApplication.CommandBars["Text"];
    myApplication.CustomizationContext = customTemplate;

    Office.CommandBarButton control = 
        (Office.CommandBarButton)contextMenu.FindControl
        (Office.MsoControlType.msoControlButton, missing,
        "MyMenuItem", true, true);

    if ((control != null))
    {
        control.Delete(true);
    }

}

private void AddMenuItem()
{
    myApplication.CustomizationContext = customTemplate;
    Office.MsoControlType menuItem = 
        Office.MsoControlType.msoControlButton;

    myControl = 
        (Office.CommandBarButton)myApplication.CommandBars["Text"].Controls.Add
        (menuItem,missing, missing, 1, true);

    myControl.Style = Office.MsoButtonStyle.msoButtonCaption;
    myControl.Caption = "My Menu Item";
    myControl.Tag = "MyMenuItem";

    myControl.Click += 
        new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler
            (myControl_Click);

    customTemplate.Saved = true;

    GC.Collect();

}


void myControl_Click(Microsoft.Office.Core.CommandBarButton Ctrl, 
    ref bool CancelDefault)
{
    System.Windows.Forms.MessageBox.Show("My Menu Item clicked");
}

برمجة نشطة

يجب عليك تعيين خاصيه Tag لعناصر التحكم عند إضافة معالجات الحدث. يستخدم Office الخاصية Tag لمتابعة تعقب معالجات الأحداث لـ CommandBarControl خاص. إذا كانت خاصية Tag فارغة، لايتم معالجة الأحداث بشكل صحيح.

قم بالتصريح بمتغيرات القائمة على مستوى الفئة بدلاً من داخل الأسلوب حيث يتم استدعاؤهم . هذا يضمن أن متغيرات القامة ستظل في النطاق طالما يتم تشغيل التطبيق. وإلا، فستتم إزالة العنصر بواسطة مجمع المهملات ثم تتوقف التعليمات البرمجية الخاصة بمعالج الحدث عن العمل.

قم بتعيين خاصية CustomizationContext للكائن Application إلى نفس المستند أو القالب كل مرة تقوم بإضافة أو إزالة أمر.

راجع أيضًا:

المهام

كيفية القيام بما يلي: إنشاء أشرطة أدوات Office

الإرشادات التفصيلية: إنشاء قوائم مختصرة للإشارات المرجعية

كيفية القيام بما يلي: إضافة أوامر إلى القوائم المختصرة في Excel

كيفية القيام بما يلي: اضافة القوائم المخصصة و عناصر القائمة إلى Outlook

كيفية القيام بما يلي: إضافة أيقونات مخصصة لشريط الأدوات و عناصر القائمة

المبادئ

المعلمات الاختيارية في حلول Office

موارد أخرى

تخصيص واجهة Office