Macros broken after altering source code

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: VBA Ribbon - Custom UI Editor
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)

@Serraniel

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22560. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

With Aspose.Words, you can create, edit and read VBA code. Aspose.Words does not provides APIs to create custom button.

The issues you have found earlier (filed as WORDSNET-22560) have been fixed in this Aspose.Words for .NET 21.9 update.

Hi,

the original reported bug has been fixed. Thanks for your fast response. We now have another issue: The original document has a reference to the Microsoft Forms 2.0 Object Library:
image.png (10.9 KB)

After changing the source code of the other module and saving the document, the reference is not active any more:
image.png (8.7 KB)

This then prevents the macro to be working, cause a used type cannot be found any more.

I assume this is another bug then, reproducable with following code:

var doc = new Document(@"S:\tmp\example.docm");

var fieldsModule = doc.VbaProject.Modules.FirstOrDefault(module =>
    module.Name.Equals("fieldsData", StringComparison.InvariantCultureIgnoreCase));


var macroStr = $@"Public Function GetFieldsStr() As String
                                Dim s As String
                               s = s + ""Test.My.First.Field=#1234"" + vbCrLf
                               s = s + ""Test.My.Second.Field=#177013"" + vbCrLf
                                GetFieldsStr = s
                            End Function";

// replace code
fieldsModule.SourceCode = macroStr;

doc.Save(@"S:\tmp\out.docm");

example.zip (47.1 KB)

@Serraniel

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22807. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-22807) have been fixed in this Aspose.Words for .NET 24.8 update also available on NuGet.