إدارة أكواد VBA لمصنف إكسل مشغل، مع ماكرو
Contents
[
Hide
]
إضافة وحدة VBA في جافا سكريبت عبر C++
تسمح Aspose.Cells بإضافة وحدة VBA جديدة وكود الماكرو باستخدام Aspose.Cells for JavaScript عبر C++. يرجى استخدام طريقة Workbook.add(Worksheet) لإضافة وحدة VBA الجديدة داخل دفتر العمل
إن الكود النموذجي التالي ينشئ مصنفًا جديدًا ويضيف وحدة VBA جديدة وكود ماكرو ويحفظ الناتج بصيغة XLSM. بمجرد فتح ملف XLSM الناتج في Microsoft Excel والنقر على أوامر قائمة المطور > Visual Basic، سترى وحدة باسم “TestModule” وداخلها سترى كود الماكرو التالي.
Sub ShowMessage() {
MsgBox "Welcome to Aspose!"
}
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add VBA Module</title>
</head>
<body>
<h1>Add VBA Module Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.xlsm,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
// Create new workbook
const workbook = new Workbook();
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Add VBA Module
const idx = workbook.vbaProject.modules.add(worksheet);
// Access the VBA Module, set its name and codes
const module = workbook.vbaProject.modules.get(idx);
module.name = "TestModule";
module.codes = "Sub ShowMessage()\r\n" +
" MsgBox \"Welcome to Aspose!\"\r\n" +
"End Sub";
// Save the workbook as XLSM and prepare download
const outputData = workbook.save(SaveFormat.Xlsm);
const blob = new Blob([outputData], { type: "application/vnd.ms-excel.sheet.macroEnabled.12" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">VBA module added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
تعديل VBA أو الماكرو في جافا سكريبت عبر C++
يمكنك تعديل كود VBA أو الماكرو باستخدام Aspose.Cells for JavaScript عبر C++. أضافت Aspose.Cells الوحدة والفئات التالية لقراءة وتعديل مشروع VBA في ملف إكسل.
- Aspose.Cells.Vba
- VbaProject
- VbaModuleCollection
- VbaModule
سيعرض هذا المقال لك كيفية تغيير رمز VBA أو الماكرو داخل ملف Excel المصدر باستخدام Aspose.Cells.
يحمّل الكود النموذجي التالي ملف Excel المصدر الذي يحتوي على كود VBA أو الماكرو التالي بداخله
Sub Button1_Click() {
MsgBox "This is test message."
}
بعد تنفيذ رمز عينات Aspose.Cells، سيتم تعديل رمز VBA أو الماكرو مثل هذا
Sub Button1_Click() {
MsgBox "This is Aspose.Cells message."
}
يمكنك تنزيل ملف Excel المصدر وملف Excel الناتج من الروابط المعطاة.
<!DOCTYPE html>
<html>
<head>
<title>Change VBA Module Code Example</title>
</head>
<body>
<h1>Change VBA Module Code Example</h1>
<input type="file" id="fileInput" accept=".xlsm,.xls,.xlsx" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file (.xlsm).</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access VBA project modules
const modules = workbook.vbaProject.modules;
const moduleCount = modules.count;
for (let i = 0; i < moduleCount; i++) {
const module = modules.get(i);
let code = module.codes;
if (code && code.includes("This is test message.")) {
code = code.replace("This is test message.", "This is Aspose.Cells message.");
module.codes = code;
}
}
const outputData = workbook.save(SaveFormat.Xlsm);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsm';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">VBA module code updated successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
مواضيع متقدمة
- إضافة مرجع مكتبة إلى مشروع VBA في مصنف العمل
- تعيين الماكرو إلى عنصر تحكم النموذج
- التحقق مما إذا كان التوقيع الرقمي لرمز VBA صالحًا
- فحص ما إذا كان رمز VBA موقعًا
- التحقق مما إذا كان مشروع VBA في مصنف عمل موقعًا
- فحص ما إذا كان مشروع VBA محميًا ومقفلاً للعرض
- نسخ تصميم الاستوديو Form UserForm VBA Macro من القالب إلى دفتر العمل الهدف
- توقيع رقمي لمشروع رمز VBA باستخدام شهادة
- تصدير شهادة VBA إلى ملف أو تيار
- تصفية مشروع VBA أثناء تحميل مصنف عمل
- معرفة ما إذا كان مشروع VBA محميًا
- حماية كلمة المرور لمشروع VBA لمصنف عمل Excel