Hi,
we have a word document with macros. We want to replace the source code of one module with a custom code via Aspose, or add it as a new module if it does not exist yet.
However, after changing the macro source code and saving the document, the macro project is completly broken.
The document also has a built in custom button into the ribbon, which makes the macro open a dialog. This also is broken after adding the new module to the project.
We use the following code:
var lic = new License();
lic.SetLicense(@"S:\Aspose.Total.lic");
var doc = new Document(@"S:\tmp\in.docm"); // template document
var macroStr = @"Public Function GetFieldsStr() As String
GetFieldsStr = ""Dummy=Str"" + vbLf
End Function";
// Modul finden
var fieldsModule = doc.VbaProject.Modules.FirstOrDefault(module =>
module.Name.Equals("FieldsData", StringComparison.InvariantCultureIgnoreCase));
if (fieldsModule == null)
{
fieldsModule = new VbaModule();
fieldsModule.Name = "FieldsData";
fieldsModule.Type = VbaModuleType.ProceduralModule;
doc.VbaProject.Modules.Add(fieldsModule);
}
// Code setze
fieldsModule.SourceCode = macroStr;
doc.Save(@"S:\tmp\out.docm");
Is there something wrong how we try to edit / add the code?
Little off-topic question: As mentioned above we added a custom button into the ribbon with this tool: https://bettersolutions.com/vba/ribbon/custom-ui-editor.htm
Is such a manipulation also possible to be done via Aspose?
Kind Regards and thanks for your help,
Daniel
Edit: I added a wrong input document to the zip, here is the correct one:
documents_fixed.zip (25.6 KB)