Open XML for primary school goers

Ok ..I agree! I was not a simple-sober-studious-obedient-goodie-good student.

I do remember when I was a primary school goer, whenever me or any of my friends wanted a leave, we almost always used the same letter template, just modifying a few fields.

Yesterday, I was thinking about this. While working on an Open XML issue and thought of creating a sample which primary school goers can use to generate application templates.

This sample just takes the fields that you are finally going to modify and creates the application docx for you. Oh .. well it will also demonstrate how to modify CustomXML attached to a docx file.


Here is it. Have fun -



 Imports System.IO
Imports System.IO.Packaging
Imports System.Xml
Public Class AddFields

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim fieldscollection As New Collection
        fieldscollection.Add(AddressTo.Text, "AddressTo")
        fieldscollection.Add(Place.Text, "Place")
        fieldscollection.Add(Teacher.Text, "Teacher")
        fieldscollection.Add(Sickness.Text, "Sickness")
        fieldscollection.Add(TimeFrame.Text, "TimeFrame")
        fieldscollection.Add(Days.Text, "Days")
        fieldscollection.Add(StudentName.Text, "StudentName")
        fieldscollection.Add(Dated.Text, "Dated")

    End Sub

    Public Sub UpdateXML(ByRef fs As Collection)

        ' Given a file name, retrieve the officeDocument part.
        Dim fileName As String = "C:\Users\username\Desktop\test\application.docx"

        Const documentRelationshipType As String = ""
        Const customRelationshipType As String = ""

        ' Open the package with read/write access.
        Using myPackage As Package = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)
            ' Get the main document part (workbook.xml, document.xml, presentation.xml).
            For Each relationship As PackageRelationship In myPackage.GetRelationshipsByType(documentRelationshipType)
                ' There should only be one document part in the package. 
                Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), relationship.TargetUri)
                Dim documentPart As PackagePart = myPackage.GetPart(documentUri)
                Dim customPart As PackagePart = Nothing 'declare a custom part
                'Get the custom xml part
                For Each crelation As PackageRelationship In documentPart.GetRelationshipsByType(customRelationshipType)
                    'There should be only one customxml part in the package
                    Dim customUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), crelation.TargetUri)
                    customPart = myPackage.GetPart(customUri)
                    Exit For

                Dim customDoc As XmlDocument = New XmlDocument()
                'Update XML
                customDoc.SelectSingleNode("/root/addressto").InnerXml = fs.Item("AddressTo")
                customDoc.SelectSingleNode("/root/place").InnerXml = fs.Item("Place")
                customDoc.SelectSingleNode("/root/teacher").InnerXml = fs.Item("Teacher")
                customDoc.SelectSingleNode("/root/sickness").InnerXml = fs.Item("Sickness")
                customDoc.SelectSingleNode("/root/timeframe").InnerXml = fs.Item("TimeFrame")
                customDoc.SelectSingleNode("/root/days").InnerXml = fs.Item("Days")
                customDoc.SelectSingleNode("/root/name").InnerXml = fs.Item("StudentName")
                customDoc.SelectSingleNode("/root/date").InnerXml = fs.Item("Dated")

                ' Save the modified customxml back into its part.
                customDoc.Save(customPart.GetStream(FileMode.Create, FileAccess.ReadWrite))

                ' Only one customXML part, so get out now.
                Exit For
        End Using
    End Sub

End Class


docx that it modifies is also attached to the post. tags: Microsoft+Blogger, Pranav+Wagh, Open+XML, OpenXML, Office+2007, Office2007, Word+2007, Word2007

Technorati tags: Microsoft+Blogger, Pranav+Wagh, Open+XML, OpenXML, Office+2007, Office2007, Word+2007, Word2007


Not responsible for errors in content, meaning, tact, or judgment. Live and let live. Toes go in first. I didn't do it. Enjoy.