VB.NET and Office Open XML

J

James Cassidy

I have been working on a function that inserts a slide into a presentation. I
am having a problem grasping the steps to add a child node to the parent
document. in this case a slide to the presentation. There is code on the MSDN
site but it has several errors and when I fixed them it would not update the
slide title. It would actually corrupt the file. Here is what I have so far:

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

If anyone has any information on a document that does not just hold
information on the XML format but actually explains the VB.NET code (not C#)
then I would apreciate that. I have made several futile attpmts to convert
the C# code into VB.NET. I will get so far and then a class or one of its
members will not be available. This is with all references that are needed
being loaded into my project. Thanks in advance for any help provided.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top