2006 11 MSDN Server Side Word 2007 Docs

IFF code (snippets) also see: keyword;

'* Defining content types package
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns= "http://schemas.openxmlformats.org/package/2006/conten-types">
<Default Extension="rels" ContentType = "application/vnd.openxmlformats-pagage.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>

'* Creating a new package
Imports System
Imports System.IO
Imports System.IO.Packaging
Imports System.Xml
Class GenerateDOCX
Shared Sub Main()
'* Create a new package
Dim pack As Package = Package.Open("c:\Data\Hello.docx", FileMode.Create, FileAccess.ReadWrite)
'* Write code here to create parts and add content
'* Close package
pack.Close()
End Sub
End Class

'* XML document generating
'* Defining string variable for Open XML namespace for nsWP;
Dim nsWP As String = "http://schemas.openxmlformats.org" & "/wordprocessingml/2006/3/main"
'* Creating the start part, set up the nested structure
Dim xmlPart As XmlDocument = New XmlDocument()
Dim tagDocument As XmlElement
tagDocument = xmlPart.CreateElement("w:document", nsWP)
xmlPart.AppendChild(tagDocument)
Dim tagBody As XmlElement
tagBody = xmlPart.CreateElement("w:body", nsWP)
tagDocument.AppendChild(tagBody)
Dim tagParagraph As XmlElement
tagParagraph = xmlPart.CreateElement("w:p", nsWP)
tagBody.AppendChild(tagParagraph)
Dim tagRun As XmlElement
tagRun = xmlPart.CreateElement("w:r", nsWP)
tagParagraph.AppdendChild(tagRun)
Dim tagText As XmlElement
tagText = xmlPart.CreateElement("w:t", nsWP)
tagRun.AppendChild(tagText)
'* Insert text into part as a text node
Dim nodeText As XmlNode
nodeText = xmlPart.CreateNode(XmlNodeType.Text, "w:t", nsWP)
nodeText.Value = "Hello Word 2007"
tagText.AppendChild(nodeText)
'* Write XML to part and close stream
xmlPart.Save(streamPart)
'* Close stream and flush XML content into package
streamPart.Close()
pack.Flush()

'* Creating in memory stream as buffer
Dim bufferStream As New MemoryStream()
'* Create new package in memory stream
Dim pack As Package = Package.Open(bufferStream, FileMode.Create, FileAccess.ReadWrite)
'* This calls same code shown in Hello World example
WriteContentToPackage(pack)
'* Save/close package object leaving DOCX file in MemoryStream
pack.Close()
'* (1) SET UP HTTP HEADERS FOR RESPONSE
'* (2) WRITE PACKAGE CONTENT INTO RESPONSE BODY

'* Response.ClearContent
Response.ClearContent()
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.ContentType = "application/vnd.ms-word.document.12"
'* Write package to response stream
bufferStream.Position = 0
Dim writer As New BinaryWriter(Response.OutputStream)
Dim reader As New Bina NO RESPONSE YET | ryReader(bufferStream)
writer.Write(reader.ReadBytes(butterStream.Length))
reader.Close()
writer.Close()
butterStream.Close()
'* flush and close response object
Response.Flush()
Response.Close()

Up