السلام عليكم...
لقد بحث في موقع ميكروسوفت و وجدت مثالاً حول إضافة زر إلى Excel 2007 و قد جربته على Excel 2010 و عمل بشكل سليم (طبعاً كتجربة حيث يتم عرض رسالة عند تحميل الـ add-in و عند إلغاء تحميله، و عند النقر على الزر).
* تجد الزر المضاف في تبويب "الوظائف الإضافية" (هذا ما ظهر لي في Excel 2010).
قمت بتعديل الكود قليلاً من أجل Word. و الخطوات كالتالي:
1. قم بتشغيل VB6 كمسؤول.
2. من القائمة File اختر الأمر New Project.
3. في نافذة New Project اختر Addin و انقر OK.
4. أزل النافذة frmAddin من المشروع.
5. انقر نقراً مزدوجاً على الـ Designer المسمى Connect لفتحه.
6. في الخانة Application اختر Microsoft Word.
7. في الخانة Application Version اختر نسخة Word.
8. في الخانة Intial Load Behavior اختر Startup.
9. افتح كود الـ Designer (المسمى Connect) و احذف كل الكود الموجود (الكود الافتراضي الموجود سابقاً مكتوب بشكل افتراضي لإنشاء add-in من أجل VB6 نفسها).
10. انسخ الكود التالي و ألصقه:
كود :
Option Explicit
Dim WordObj As Object
Dim WithEvents MyButton As Office.CommandBarButton
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error Resume Next
MsgBox "My Addin started in " & Application.Name
Set WordObj = Application
Set MyButton = WordObj.CommandBars("Standard").Controls.Add(1)
With MyButton
.Caption = "My Custom Button"
.Style = msoButtonCaption
' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when there is more than
' one application window visible. The property is required
' by some Office applications and should be provided.
.Tag = "My Custom Button"
' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, such that if
' the add-in is not loaded when a user presses the button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.
.OnAction = "!<" & AddInInst.ProgId & ">"
.Visible = True
End With
End Sub
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next
MsgBox "My Addin was disconnected by " & IIf(RemoveMode = ext_dm_HostShutdown, "Excel shutdown.", "end user.")
MyButton.Delete
Set MyButton = Nothing
Set WordObj = Nothing
End Sub
Private Sub MyButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
MsgBox "Our CommandBar button was pressed!"
End Sub
11. احفظ المشروع.
12. من القائمة File اختر الأمر Make لإنشاء الـ DLL و تسجيله في النظام.
13. شغل برنامج Microsoft Word. ستظهر لك رسالة عند بدء تشغيله تخبرك بأنه تحميل الـ add-in. افتح علامة التبويب "الوظائف الإضافية" و ستجد بها زراً بعنوان My Custom Button عند النقر عليه سيعرض رسالة. و عند إغلاق Word ستظهر أيضاً رسالة تخبرنا بإلغاء تحميل الـ add-in.
*** بالطبع يمكنك حذف رسائل البداية و النهاية من الكود، و تعديل الإجراء MyButton_Click (في الكود السابق) لتنفيذ ما تريده بدل عرض الرسالة.
*** و ربما ستحتاج إلى مراجعة وثائق و تعليمات MS Word لمعرفة كيفية التعامل مع Word من داخل كود الـ add-in.
نرجو الاستفادة و السلام.