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

Free Support Forum - aspose.com

Preserve the macros in the document when converted to HTML


We are using ASPOSE in our application to modify word document and convert document into HTML and vice versa.

We have a requirement to preserve the macros in the document when converted to HTML, is it possible to achieve this functionality through ASPOSE?

Thanks & Regards,
Ashish Gupta

This message was posted using Email2Forum by aske012.

Hi Ashish,

Thanks for your inquiry.

I’m afraid it’s nearly impossible to preserve a marco in the HTML format. A macro is a binary script nested deep within the Word document format and can’t really be exported to HTML in a simple way.

Instead, I can think of one work around that you might want to use. For example when you want to save a document to HTML but preserve macros:

  1. Convert the document to HTML.
  2. Clear all of the content from the AW document and then save the “empty” document again but to WordML (XML) format…
  3. This should now give you a near empty document shell and also your macro in XML format.
  4. Append this XML string to your HTML in some way so it’s not displayed.

Then when you need to import the document with the macro again:

  1. Parse the macro XML from the HTML document.
  2. Load this string into a new Document object.
  3. Load the HTML content from your file into a different Document object.
  4. Copy all of the sections from the HTML document to the macro document.
  5. You should now get the same document loaded from HTML with macros intact.

Please see a code example of this in action below.

Document doc = new Document("Document.doc");
// Save the actual document content in HTML format.
doc.Save(dataDir + "Document Out.html");
// Remove all document content but leaving macros and save the document in XML format. 
MemoryStream outStream = new MemoryStream();
doc.Save(outStream, SaveFormat.WordML);
// Get the output which contains the macro in the form of an XML string.
string macroHtml = Encoding.UTF8.GetString(outStream.ToArray());
// Append this to the HTML file output as something that won't be displayed so it can be transfered e.g a comment in the HTML
// Now when you need to restore the original content.
// Check for and parse the HTML for the macro data into macroHtml.
string macroHtml = "...";
// Load the base document containing the macro.
Document baseDoc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(macroHtml)));
// Load the HTML document back in as well.
Document htmlDoc = new Document(dataDir + "Document Out.html");
// Copy all content from the HTML doc to the base document with the macros.
foreach (Section section in htmlDoc.Sections)
    baseDoc.AppendChild(baseDoc.ImportNode(section, true));
// baseDoc should now be restored with the document content and macro ready for use.

If you have any futher queries, please feel free to ask.