Verwalten Sie VBA Codes in einer Excel Arbeitsmappe mit Makros in C++
Ein VBA-Modul in C++ hinzufügen
Der folgende Beispielcode erstellt eine neue Arbeitsmappe, fügt ein neues VBA-Modul und Makro-Code hinzu und speichert die Ausgabe im XLSM-Format. Sobald Sie die XLSM-Datei in Microsoft Excel öffnen und die Menübefehle Entwickler > Visual Basic anklicken, sehen Sie ein Modul namens “TestModule” und innerhalb dieses Moduls den folgenden Makro-Code.
Sub ShowMessage()
MsgBox "Welcome to Aspose!"
End Sub
Hier ist der Beispielcode zum Erzeugen der Ausgabe-XLSM-Datei mit VBA-Modul und Makrocode.
#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;
}
VBA oder Makro in C++ bearbeiten
Sie können VBA- oder Makrocode mit Aspose.Cells modifizieren. Aspose.Cells hat den folgenden Namensraum und Klassen hinzugefügt, um das VBA-Projekt in der Excel-Datei zu lesen und zu modifizieren.
- Aspose::Cells::Vba
- VbaProject
- VbaModuleCollection
- VbaModule
Dieser Artikel zeigt Ihnen, wie Sie den VBA- oder Makrocode in der Quell-Excel-Datei mithilfe von Aspose.Cells ändern können.
Der folgende Beispielcode lädt die Quell-Excel-Datei, die den folgenden VBA- oder Makro-Code enthält:
Sub Button1_Click()
MsgBox "This is test message."
End Sub
Nach Ausführung des Aspose.Cells-Beispiels wird der VBA- oder Makro-Code wie folgt geändert:
Sub Button1_Click()
MsgBox "This is Aspose.Cells message."
End Sub
Sie können die Quell-Excel-Datei und die Ausgabe-Excel-Datei über die angegebenen Links herunterladen.
#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;
}
Fortgeschrittene Themen
- Eine Bibliotheksreferenz zum VBA-Projekt in der Arbeitsmappe hinzufügen
- Makro einem Formularsteuerelement zuweisen
- Überprüfen Sie, ob die digitale Signatur des VBA-Codes gültig ist
- Überprüfen Sie, ob der VBA-Code signiert ist
- Überprüfen, ob das VBA-Projekt in einer Arbeitsmappe signiert ist
- Überprüfen Sie, ob das VBA-Projekt geschützt und zum Anzeigen gesperrt ist
- Kopieren Sie den VBA-Makro UserForm-DesignerStorage von der Vorlage in die Zieldatei
- Digitales Signieren eines VBA-Codeprojekts mit Zertifikat
- Exportieren Sie das VBA-Zertifikat in eine Datei oder einen Stream
- VBA-Projekt beim Laden einer Arbeitsmappe filtern
- Herausfinden, ob das VBA-Projekt geschützt ist
- Passwortschutz des VBA-Projekts der Excel-Arbeitsmappe