Etkinleştirilmiş Çalışma Kitabının VBA Kodlarını Yönet
C# İçinde VBA Modülü Ekle
Aşağıdaki örnek kod yeni bir çalışma kitabı oluşturur ve yeni bir VBA Modülü ve Makro Kodu ekler ve çıktıyı XLSM biçiminde kaydeder. Bir kez çıktı XLSM dosyasını Microsoft Excel’de açarsanız ve Geliştirici > Görsel Temel menü komutlarına tıklayarak “TestModülü” adlı bir modül göreceksiniz ve içinde aşağıdaki makro kodunu göreceksiniz.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
Aşağıdaki örnek kod, VBA Modülü ve Makro Kodu içeren kaynak Excel dosyasını yükler
// 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); |
C# İçinde VBA veya Makro Değiştir
VBA veya Makro Kodunu, Aspose.Cells kullanarak değiştirebilirsiniz. Aspose.Cells, Excel dosyasındaki VBA projeyi okumak ve değiştirmek için aşağıdaki ad alanını ve sınıfları eklemiştir.
- Aspose.Cells.Vba
- VbaProject
- VbaModuleCollection
- VbaModule
Bu makale, Aspose.Cells kullanarak kaynak Excel dosyasındaki VBA veya Makro Kodunu değiştirmeyi gösterecektir.
Aşağıdaki örnek kod, içinde belirtilen VBA veya Makro kodu bulunan kaynak Excel dosyasını yükler
Sub Button1_Click()
MsgBox "This is test message."
End Sub
Aspose.Cells örnek kodunun yürütülmesinden sonra, VBA veya Makro kodu bu şekilde değiştirilmiş olacaktır
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
Verilen bağlantılardan kaynak Excel dosyasını ve çıktı Excel dosyasını indirebilirsiniz.
// 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"); |
Gelişmiş Konular
- Çalışma Kitabındaki VBA projeye bir kütüphane referansı ekle
- Makroyu Form Kontrole Ata
- VBA Kodunun Dijital İmzasının Geçerli Olup Olmadığını Kontrol Et
- VBA Kodunun İmzalı Olup Olmadığını Kontrol Et
- Çalışma Kitabındaki VBA projesinin imzalı olup olmadığını kontrol edin
- VBA Projesinin Korunup Görüntülemeye Kilitli Olup Olmadığını Kontrol Edin
- Taslak ve Hedef Çalışbook Arasında VBA Makro Kullanıcı Formu Tasarımcı Depolama Alanını Kopyalama
- Sertifika ile Bir VBA Kod Projesini Dijital Olarak İmzalama
- VBA Sertifikasını Dosyaya veya Akışa Aktarma
- Bir çalışma kitabı yüklenirken VBA Projesini Filtreleme
- VBA Projesinin Korunup Korunmadığını Bulma
- Excel Çalışma Kitabının VBA Projesini Parolayla Koruma