Excel Makro Etkin çalışma kitabının VBA kodlarını yönetin.
C#‘de bir VBA Modülü ekleyin
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, XLSM çıktı dosyasını Microsoft Excel’de açacaksınız veGeliştirici > Visual Basic menü komutları, “TestModule” adında bir modül göreceksiniz ve içinde aşağıdaki makro kodunu göreceksiniz.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
İşte XLSM çıktı dosyasını VBA Modülü ve Makro Kodu ile oluşturmak için örnek kod.
// 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#‘de VBA’yı veya Makroyu değiştirin
Aspose.Cells’i kullanarak VBA veya Makro Kodunu değiştirebilirsiniz. Aspose.Cells, Excel dosyasındaki VBA projesini okumak ve değiştirmek için aşağıdaki ad alanını ve sınıfları ekledi.
- Aspose.Cells.Vba
- Vba Projesi
- VbaModül Koleksiyonu
- Vba Modülü
Bu makale, Aspose.Cells kullanarak kaynak Excel dosyası içindeki VBA veya Makro Kodunu nasıl değiştireceğinizi gösterecektir.
Aşağıdaki örnek kod, içinde aşağıdaki 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ştirilecektir.
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
indirebilirsinizkaynak Excel dosyası veçıktı excel dosyası verilen linklerden
// 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"); |
ileri konular
- Çalışma kitabında VBA projesine bir kitaplık başvurusu ekleyin
- Form Denetimine Makro Ata
- VBA Kodunun Dijital İmzasının Geçerli olup olmadığını kontrol edin
- VBA Kodunun İmzalanıp İmzalanmadığını Kontrol Edin
- Çalışma Kitabındaki VBA projesinin İmzalı olup olmadığını kontrol edin
- VBA Projesinin Korumalı ve Görüntüleme için Kilitli olup olmadığını kontrol edin
- VBA Macro UserForm DesignerStorage’ı Şablondan Hedef Çalışma Kitabına Kopyalayın
- Sertifikalı Bir VBA Kodu Projesini Dijital Olarak İmzalayın
- VBA Sertifikasını Dosyaya veya Akışa Aktarın
- Çalışma kitabı yüklerken VBA Projesini filtreleme
- VBA Projesinin Korumalı olup olmadığını öğrenin
- Excel Çalışma Kitabının VBA Projesini Parolayla Koruyun