Answered Need to use a macros enabled template to get docx file as output using C# code

Deepak

New member
Joined
Feb 12, 2021
Messages
3
Programming Experience
1-3
I have a console application which gets json result from an API and need to use a macros enabled template(it has some custom shortcut keys not header or footer). I need to write the josn result into the macros template and save it as a docx file. When I tried below code, I got the docx file but the custom shortcut keys are missing. Can anyone check my code and help me do it right?

C#:
private static void PocUsingDotTemplate()
{
    SetAsposeLicense();
    var basePath=@"C:\tools\PoC\dot-template";
    var pathToTemplate=@"C:\tools\PoC\dot-template\template.dotm";
    var pathtoDocument=@"C:\tools\PoC\dot-template\197669-1502119-de.html";
    var opts1=new LoadOptions();
opts1. LoadFormat=LoadFormat.Dotm;
    var opts2=new LoadOptions();
opts1. LoadFormat=LoadFormat.Html;
   
    var template=new Document(pathToTemplate,opts1);
    var document=new Document(pathToDocument,opts2);
   
    DocumentBuilder dbuilder=new DocumentBuilder(template);
dbuilder. InsertDocument(document,ImportFormatMode.UseDestinationStyles);
   
    //next lines are to generate a document with macros activated in output,
    //Hence the format should be docm.
dbuilder. Document.Save($"{basepath}{DateTime.Now.GetHashCode()}.docm",SaveFormat.Docm);
   
    //next lines removes the macro from document builder and geneates properly
    //a docx document
dbuilder. Document.RemoveMacros();
dbuilder. Document.Save($"{basepath}{DateTime.Now.GetHashCode()}.docx",SaveFormat.Docx);
}
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,583
Location
Chesapeake, VA
Programming Experience
10+
Well, considering that the way custom shortcut keys work is by using macros, and on line 24 you are removing macros, why are you surprised with the result?
 

Deepak

New member
Joined
Feb 12, 2021
Messages
3
Programming Experience
1-3
Well, considering that the way custom shortcut keys work is by using macros, and on line 24 you are removing macros, why are you surprised with the result?
Hmm. I considered that. But I want the output as docx file with macros shortcuts enabled. Is there a way to do that? I am really in need for this.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,583
Location
Chesapeake, VA
Programming Experience
10+
A .docx file can still have macros in it. (It's the way some malware spreads itself.) Just comment out line 24.

Edit after: Serves me write for making a comment based on my experiences playing with alpha/beta versions from years ago instead of the actual production version. You are correct that the DOCX format shouldn't allow macros to be stored with them. So commenting out line 24 above would not help.

I think the key it determine where / how those shortcuts were established in the document. As I stated above, I believe that the keyboard shortcuts come in as macros. If macros are now allowed, then you'll have to give up the keyboard shortcuts as well.
 
Last edited:
Top Bottom