C++ ile Excel Makro aktif Çalışma Kitabının VBA Kodlarını Yönetin
C++ ile 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 formatında kaydeder. Çıktı XLSM dosyasını Microsoft Excel’de açtıktan sonra Geliştirici > Görsel Temel menüsüne tıklarsanız, “TestModule” adında bir modül görürsünüz 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
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create new workbook
Workbook workbook;
// Access first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Add VBA Module
int32_t idx = workbook.GetVbaProject().GetModules().Add(worksheet);
// Access the VBA Module, set its name and codes
VbaModule module = workbook.GetVbaProject().GetModules().Get(idx);
module.SetName(u"TestModule");
U16String codes = u"Sub ShowMessage()\r\n"
u" MsgBox \"Welcome to Aspose!\"\r\n"
u"End Sub";
module.SetCodes(codes);
// Save the workbook
U16String outputPath = outDir + u"output_out.xlsm";
workbook.Save(outputPath, SaveFormat::Xlsm);
std::cout << "VBA module added and workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
C++ ile VBA veya Makroyu Düzenle
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çindeki 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 çalıştırılmasından sonra, VBA veya Makro kodu şöyle değişecektir:
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.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
U16String inputFilePath = srcDir + u"sample.xlsm";
U16String outputFilePath = outDir + u"output_out.xlsm";
Workbook workbook(inputFilePath);
VbaProject vbaProject = workbook.GetVbaProject();
VbaModuleCollection modules = vbaProject.GetModules();
for (int i = 0; i < modules.GetCount(); ++i)
{
VbaModule module = modules.Get(i);
U16String code = module.GetCodes();
U16String searchStr = u"This is test message.";
U16String replaceStr = u"This is Aspose.Cells message.";
if (code.IndexOf(searchStr) != -1)
{
U16String newCode;
const char16_t* codeData = code.GetData();
const char16_t* searchData = searchStr.GetData();
int codeLen = code.GetLength();
int searchLen = searchStr.GetLength();
int pos = 0;
int searchPos;
while ((searchPos = code.IndexOf(searchStr)) != -1)
{
for (int j = pos; j < searchPos; j++)
{
newCode += codeData[j];
}
newCode += replaceStr;
pos = searchPos + searchLen;
}
for (int j = pos; j < codeLen; j++)
{
newCode += codeData[j];
}
module.SetCodes(newCode);
}
}
workbook.Save(outputFilePath);
std::cout << "VBA module codes updated successfully." << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
Gelişmiş Konular
- Çalışma kitabında VBA Projesine 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
- Bir çalışma kitabındaki VBA Projesinin İmzalanıp İmzalanmadığı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 Filtrele
- VBA Projesinin Korunup Korunmadığını Bulma
- Excel Çalışma Kitabının VBA Projesini Parolayla Koruma