Hello. I use the following code to combine multiple documents. This works fine.
I am wondering how this works with documents that contain macros. What I notice now is that if the first document is docm, those macros are kept in the combined file. If the first is docx, and a docm is added, the result is a document without macros.
I want to be clear to the user about this.So I have the folowing questions:
Is it even possible to combine two or more docm files, or should I not allow that?
Is it advisable to combine docm and docx files?
You can join/combine DOCX and DOCM using Aspose.Words. Could you please ZIP and attach your input DOCX and DOCM along with expected output document? We will investigate the issue and provide you more information on it.
Please note that Document.AppendDocument does not copy the macros from source document to destination document. However, we have logged this feature request as WORDSNET-19556 in our issue tracking system. You will be notified via this forum thread once this feature is available.
However, you can read the macro from one document and copy it into another document. Please read the following article about working with macros. Working with VBA Macros
So when a docM is appended to a docX file, I could in theory do the following:
create a new vba project in de target doc (because it does not have macros yet)
add modules from the append docM to the new vba project
I am not too familiar with macros. Would it be advisable to do this? I would only want to support this for my users if it is a safe operation. I have users that combine sometimes 8 documents.
We will add ability to clone VbaProject and VbaModule for your requirement as shown in following code examples. Please share if you are satisfied with the proposed solution. We will then add the described functionality in upcoming release.
Document sourceDoc = new Document(MyDir + "source.docm");
Document destDoc = new Document();
// Clone the whole project.
destDoc.VbaProject = sourceDoc.VbaProject.Clone();
destDoc.Save("output.docm");
Document sourceDoc = new Document(MyDir + "source.docm");
Document destDoc = new Document();
destDoc.VbaProject = new VbaProject();
// Clone a single module.
VbaModule copyModule = sourceDoc.VbaProject.Modules["Module1"].Clone();
destDoc.VbaProject.Modules.Add(copyModule);
destDoc.Save("output.docm");
Excuse me for my late response. For now, I have settled for now with the situation that macros are only retained when they are in the first source document. So I have not implemented this yet.