wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Military Paperless Initiative

Last post 05-27-2009, 10:11 PM by DanielM. 1 replies.
Sort Posts: Previous Next
  •  04-24-2009, 10:21 AM 4549

    Military Paperless Initiative

    I am attempting to develope classes that are dedicated to each office document. With this class I will have it passed information from the database of stored procedure names and format information. When the user presses a button the document information for the report they want will be sent to this class creating a document with up to date information. Only one problem. I am for some reason not able to figure out how to add children nodes to a document. Here is a function that I have been working on.

    Public Function PPTInsertNewSlide(ByVal fileName As String, ByVal position As Integer, ByVal title As String) As Boolean

    Dim returnValue As Boolean = False

    Dim documentPart As PackagePart = Nothing

    Using pptPackage As Package = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)

    ' Get the main document part (presentation.xml).

    For Each relationship As PackageRelationship In pptPackage.GetRelationshipsByType(documentRelationshipType)

    Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), relationship.TargetUri)

    documentPart = pptPackage.GetPart(documentUri)

    ' There is only one document.

    Exit For

    Next

    ' Iterate through the slides and extract the title string from each.

    Dim slidePart As PackagePart = Nothing

    Dim slideUri As Uri = Nothing

    Dim DocElement As OpenXmlElement = Nothing

    'DocElement.SetAttributes(documentPart)

    ' Manage namespaces to perform Xml XPath queries.

    Dim nt As New NameTable()

    Dim nsManager As New XmlNamespaceManager(nt)

    nsManager.AddNamespace("p", presentationmlNamespace)

    nsManager.AddNamespace("a", drawingmlNamespace)

    ' Select each slide document part (slides/slideX.xml)

    ' via relationship with document part.

    For Each relation As PackageRelationship In documentPart.GetRelationshipsByType(slideRelationshipType)

    slideUri = PackUriHelper.ResolvePartUri(documentPart.Uri, relation.TargetUri)

    slidePart = pptPackage.GetPart(slideUri)

    ' Get the slide part from the package.

    Dim doc As XmlDocument = New XmlDocument(nt)

    doc.Load(slidePart.GetStream())

    'Insert the new slide and name it.

    Dim newSlide As Slide =

    newSlide.InnerText.Insert(0, title)

    newSlide.InsertAt(DocElement, position)

    ' Locate the slide title using XPath.

    ' Note: This code assumes that the first text found is the title.

    ' Also note that if the title contains more than one font,

    ' or is in any way anything other than plain text, PowerPoint

    ' breaks it up into multiple elements. This code won't find a match

    ' in that case.

    Dim xNode As XmlNode = doc.SelectSingleNode("//a:t", nsManager)

    If xNode IsNot Nothing Then

    ' Perform a case-insensitive comparison.

    'If String.Compare(xNode.InnerText, positionAfterTitle, True) = 0 Then

    'Insert the new slide and name it.

    Dim newSlide As Slide = xNode.

    newSlide.InnerText.Insert(0, title)

    newSlide.InsertAt(DocElement, position)

    'End If

    End If

    Next

    End Using

    Return returnValue

    End Function

    Any help would be apreciated and would definately save the government in paper costs.

    Thanks

    James


    James Cassidy
  •  05-27-2009, 10:11 PM 4715 in reply to 4549

    Re: Military Paperless Initiative

    Hi James,

    Are you referring to inserting slides into a Powerpoint presentation, or generic nodes into a Word document (as indicated in your question)? In the code example you've given, you're selecting the title text (a DrawingML object contained within the slide) and trying to treat it as a slide. Have a look at this MSDN guide on how to do this (using the API - which encapsulates these elements in respective classes): http://msdn.microsoft.com/en-us/library/cc850834(office.14).aspx

    There are also some handy references:
    The OOXML primer (which explains the basic concepts) here;
    The OOXML fundamentals (i.e. more conceptual details) here; and
    The OOXML reference (which goes into greater detail of the XML - best to navigate with the bookmarks) here

    Hope this helps.

    -Daniel
View as RSS news feed in XML