Add Style Part using Open XML SDK

How can I add a style part to a document created using the

'How to create an Office Open XML Package by using the Open XML Object Model'

[155 byte] By [MDLo] at [2008-2-7]
# 1

Problem Solved !!

Once I figured that a Style Part is of type StyleDefinitionPart and not StylePart as per ThemePart I weas able to produce the following helper function which creates a Word Document named Filename using DocumentXML as the main document part 'Document.xml' , 'Styles.xml' as the Style File and Them1.xml as the Theme file

Public Function CreateDocument(ByRef FileName As String, ByVal DocumentXML As XDocument) As Boolean

Dim Dir = My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\"

Dim StyleFile = Dir + "styles.xml"

Dim ThemeFile = Dir + "Theme1.xml"

FileName = Dir + FileName

Try

' Create the package

Dim wordDoc = WordprocessingDocument.Create(FileName, _

WordprocessingDocumentType.Document)

Using (wordDoc)

Dim mainPart = wordDoc.AddMainDocumentPart

Dim stream = mainPart.GetStream

Dim utf8encoder1 = New UTF8Encoding()

Dim buf() = utf8encoder1.GetBytes(DocumentXML.ToString)

stream.Write(buf, 0, buf.Length)

Dim themepart = mainPart.AddNewPart(Of ThemePart)()

stream = New FileStream(ThemeFile, FileMode.Open)

themepart.FeedData(stream)

Dim stylepart = mainPart.AddNewPart(Of StyleDefinitionsPart)()

stream = New FileStream(StyleFile, FileMode.Open)

stylepart.FeedData(stream)

End Using

Catch ex As Exception

Return 0

End Try

Return -1

End Function

MDLohan at 2007-9-28 > top of Msdn Tech,Office Live Development,Microsoft SDK for Open XML Formats...