إدارة رموز VBA لمصنف العمل ذو الماكرو الممكن تمكينه.
إضافة وحدة VBA في C#
يُنشئ رمز العينة التالي مصنف عمل جديد ويضيف وحدة VBA جديدة وكود الماكرو الجديد ويحفظ الإخراج بتنسيق XLSM. بمجرد فتحك لملف الإكسيل الناتج XLSM والنقر على أوامر تطوير > الأساسيات المرئية، سترى وحدة تسمى “الوحدة الاختبارية” وبداخلها سترى كود الماكرو التالي.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
فيما يلي رمز العينة لإنشاء ملف إكسيل الناتج بتنسيق XLSM مع وحدة VBA وكود الماكرو.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create new workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Add VBA Module | |
int idx = workbook.VbaProject.Modules.Add(worksheet); | |
// Access the VBA Module, set its name and codes | |
Aspose.Cells.Vba.VbaModule module = workbook.VbaProject.Modules[idx]; | |
module.Name = "TestModule"; | |
module.Codes = "Sub ShowMessage()" + "\r\n" + | |
" MsgBox \"Welcome to Aspose!\"" + "\r\n" + | |
"End Sub"; | |
// Save the workbook | |
workbook.Save(dataDir + "output_out.xlsm", SaveFormat.Xlsm); |
تعديل VBA أو الماكرو في C#
يمكنك تعديل الكود الخاص ب VBA أو الماكرو باستخدام Aspose.Cells. لقد قامت Aspose.Cells بإضافة مساحة الاسم التالية والفئات لقراءة وتعديل مشروع VBA في ملف الإكسيل.
- Aspose.Cells.Vba
- VbaProject
- VbaModuleCollection
- VbaModule
سيعرض هذا المقال لك كيفية تغيير رمز VBA أو الماكرو داخل ملف Excel المصدر باستخدام Aspose.Cells.
يقوم الرمز الخاص المعروض أدناه بتحميل ملف Excel المصدر الذي يحتوي على رمز VBA أو ماكرو التالي داخله
Sub Button1_Click()
MsgBox "This is test message."
End Sub
بعد تنفيذ رمز عينات Aspose.Cells، سيتم تعديل رمز VBA أو الماكرو مثل هذا
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
يمكنك تنزيل ملف Excel المصدر وملف Excel الناتج من الروابط المعطاة.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsm"); | |
// Change the VBA Module Code | |
foreach (VbaModule module in workbook.VbaProject.Modules) | |
{ | |
string code = module.Codes; | |
// Replace the original message with the modified message | |
if (code.Contains("This is test message.")) | |
{ | |
code = code.Replace("This is test message.", "This is Aspose.Cells message."); | |
module.Codes = code; | |
} | |
} | |
// Save the output Excel file | |
workbook.Save(dataDir + "output_out.xlsm"); |
مواضيع متقدمة
- إضافة مرجع مكتبة إلى مشروع VBA في مصنف العمل
- تعيين الماكرو إلى عنصر تحكم النموذج
- التحقق مما إذا كان التوقيع الرقمي لرمز VBA صالحًا
- فحص ما إذا كان رمز VBA موقعًا
- التحقق مما إذا كان مشروع VBA في مصنف عمل موقعًا
- فحص ما إذا كان مشروع VBA محميًا ومقفلاً للعرض
- نسخ تصميم الاستوديو Form UserForm VBA Macro من القالب إلى دفتر العمل الهدف
- توقيع رقمي لمشروع رمز VBA باستخدام شهادة
- تصدير شهادة VBA إلى ملف أو تيار
- تصفية مشروع VBA أثناء تحميل مصنف عمل
- معرفة ما إذا كان مشروع VBA محميًا
- حماية كلمة المرور لمشروع VBA لمصنف عمل Excel