Labels

Thursday, March 29, 2012

Converting docx file to stylesheet


From NAV version 5.0 here's the possibility to send forms to Word/Excel. This functionality uses progressive XSLT technology, which uses source XML file with data + XSLT file with view template.
There is special tool to make the process of using templates much easier. See http://www.mibuso.com/dlinfo.asp?FileID=869 for additional info. But unfortunately this don't works for me. 
I prefer to investigate the manual creation of XSLT from source DOCX file. It's very important to use Word 2007 - 2010 and DOCX extension, because it's represents by XML file.
1. Firstly we should do the "bookmarks" in source text, where we want to export some values, e.g. fldVIN in the location, where's VIN should be.
2. Save DOCX file. Change the extension to ZIP. Inside archive find the file document.xml and copy it to initial folder.
3. Open the document.xml in notepad editor and replace text at the start of file (from the beggining to the tag <w:body>):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

with text:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-
com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ns2="http://www.navisionformlist.com">
<xsl:output method="xml" encoding="UTF-8" standalone="yes"/>
<xsl:template match="/"><xsl:processing-instruction name="mso-
application"><xsl:text>progid="Word.Document"</xsl:text></xsl:processing-instruction>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-
microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:ns2="http://www.navisionformlist.com" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"
xml:space="preserve"><wx:sect>

At the end of file, replace: 
</w:document>
with
</wx:sect></w:wordDocument></xsl:template></xsl:stylesheet>

Replace "bookmarks" (e.g fldVIN), with XPath expression in the attribute "select", for example
<xsl:value-of select="//Object/Control[1]/Control[1]/Row[1]/Control[2]/@value"/>
This query should return the value of second control from the first tab.

4. Save the file with name document.xslt and open it in Internet Explorer to check it for valid.
5. Open form 690 Manage Style Sheets. Choose option Show stylesheet only for that form.Enter the number of form, which should export data to Word, e.g. Vehicle Card (5025401). Press Functions, Import and choose file document.xslt. Enter the name of template and choose the Integration Program Word.
6. In the form Vehicle Card we could now check the export.

For debugging it is possible to export data to XML file. For that change CU 403, trigger LaunchApp 
AddLocaleInfo(DataXML);
// PT <
DataXML.save('C:\debug.xml');
// PT >
TransformDocument(DataXML,StylesheetXML,ApplicationXML);

After exporting we should have source data file. It could be opened in MS Word with special option Open with transform and selecting our stylesheet.

If there is the requirement to export some fields, that are absent in the conrols of the form we should change the source code in CU 403, for example:
AddCustomerLetter(VAR DataXML : Automation "'Microsoft XML, v4.0'.DOMDocument40")
DocumentElement := DataXML.documentElement;
CustCode := GetAttributeValue(DataXML,'//Object/Control/Control/Row[1]/Control[2]','value');
AddCustomerInfo(DataXML,CustCode);
AddElement(DocumentElement,'WorkDate',FORMAT(WORKDATE),ChildNode);
AddElement(DocumentElement,'Salutation',FORMAT(Text003),ChildNode);
AddElement(DocumentElement,'LetterBody',FORMAT(Text004),ChildNode);
AddElement(DocumentElement,'ComplimentaryClose',FORMAT(Text005),ChildNode);
AddCompanyInfo(DataXML);

After that we could get the values from the stylesheet with XPath expression, like <xsl:value-of select="Object/Item/Data/Description"/>. 
So, now we are not influenced by controls and their locations on the form.

No comments: