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

Free Support Forum - aspose.com

.dotm macros are being lost when saving a new document

We are having an issue when creating and saving a new document from a .dotm template using Aspose.Words.

When we create a new document from the template manually, using Word, the macros are all contained in the new document.

When we create new document programmatically, using Aspose.Words, the macros are not in the new document.

The code we are using to save the new document, if it is based on a macro-enabled template, is:

Doc.Save(docName, SaveFormat.Docm)

Is there something else we need to do to ensure the macros are available in the newly created document? This is a high-priority issue for us.

Thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please attach your template for testing? I will investigate the issue and provide you more information.

Best regards.

Here is one. I added a .txt extension so that I could upload the file.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think you should just attach the template to your document. See the following link for more information.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.document.attachedtemplate.html

Hope this helps.

Best regards.

Ok, I’ll try that. What is the difference between creating a new document based on a template, and creating a new blank document and attaching a template? Will all of the merge fields and bookmarks defined in the template be applied to the new blank document?

Well, unfortunately that didn’t work at all, unless I did something wrong.

This is the original way I was creating my documents:
doc = New Document(Path.Combine(_templateBasePath, Template.FileName))

I changed it to this, based on your suggestion:
doc = New Document()
doc.AttachedTemplate = Path.Combine(_templateBasePath, Template.FileName)

Not only did the macros from the template not show up in the new document, none of the content from the template was in the new document either. The new document was completely blank.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following code:

doc = New Document(Path.Combine(_templateBasePath, Template.FileName))

doc.AttachedTemplate = Path.Combine(_templateBasePath, Template.FileName)

Hope this helps.

Best regards.

Thank you. That is getting us a little closer. The document contains all the proper text and merge fields, and the macro modules are being loaded.

However, when I create a new document based on this template manually using word, we get a couple of buttons on our Add-Ins tab that launch the two macros.

When I create the document using Aspose.Words, the buttons do not show up on the Add-Ins tab.

When I look at the macros using the VBA editor, I see two template projects - the new document, and the attached template. the macros are in the second project, but the new document is in the first.

I have attached screen shots to show what I am talking about.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following code:

Document doc = new Document(@"C:\Temp\RC44.dotm");

doc.AttachedTemplate = @"C:\Temp\RC44.dotm";

doc.IsTemplate = false;

doc.Save(@"Test069\out.docm");

Hope this helps.

Best regards.

Thanks. That seems to get the macros into the TemplateProject in the VB code window, but the custom buttons are still not showing up on the Add-ins toolbar, nor are they visible in the Macros dialog. When the document is created manually from the template, the buttons are there and the macros are visible in the macros dialog.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Add-ins is shown only if I open your Template. If I save created document on disk and open it again Add-ins tab is not shown at all. Please try using DOCM document as a template for Aspose.Words. Something like the following:

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

doc.Save(@"Test069\out.dotm", SaveFormat.Docm);

Best regards.

Hey, that worked! I never would have thought of using a .DOCM instead of a .DOTM. Thanks!