We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Modify VBA source code in Word doc

Good morning,

I have a problem with a docm document I’m generating with Aspose.word librairy in Java.
I have a template in a .dotm format with existing VBA source code.
I’m reading and modifying VBA source code and create a new document in docm format.
When I open the generated docm document, my VBA isn’t working till I click on a button and save the document. When I reopen the document everything is working (the size of my document is greater after saving the document too).
Could you please advise on this problem ?

This is my code to modify the VBA source code :

VbaProject project = builder.getDocument().getVbaProject();
        if(project != null) {
            if(project.getModules().get("ParticipantAspose") != null) {
                String addedSourceCode = "Public Function InitParticipantsByOrdreDuJourList()\n" +
                        "   'BEGIN ASPOSE\n" +
                        "   Dim cParticipantsList As New Collection\n" +
                        "   Dim pParticipant As New Participant\n" +
                        "   Dim cParticipantsByOrdreDuJour As New Collection\n" +
                        "   Set cParticipantsByOrdreDuJour = New Collection\n" +
                        "   Set cParticipantsList = New Collection\n";

// SOME ADDED CODE...
      
                addedSourceCode += "     'END ASPOSE\n" + "End Function";
                project.getModules().get("ParticipantAspose").setSourceCode(addedSourceCode);
            }
        }

I can provide a document as an example if necessary.

Best Regards,

Muyldermans Aurore

@amuyldermans I cannot reproduce the problem with a simple document and the following code:

Document doc = new Document("C:\\Temp\\in.docm");

VbaProject project = doc.getVbaProject();
if (project != null)
{
    VbaModule module = project.getModules().get("NewMacros");
    if (module != null)
    {
        String addedSourceCode = "Sub ShowMessage() \n" +
                " MsgBox \"This macros was edited by Aspose.Words.\"  \n" +
                "End Sub";

        module.setSourceCode(addedSourceCode);
    }
}

doc.save("C:\\Temp\\out.docm");

docs.zip (24.2 KB)