我在OneNote 2013中将图像添加到页面时遇到问题。我可以创建包含内容的部分,笔记本和页面,但是我在向页面添加图像时遇到了问题。
我调用UpdatePageContent并传入xml,但我收到一条无效的XML消息(hresult 0x80042001)。任何协助赞赏。这是我用来创建XML的完整代码,我正在尝试使用以下内容更新页面:
Sub CreateNewPage(ByVal pageName As String)
Dim OneNote As Microsoft.Office.Interop.OneNote.Application
OneNote = New Microsoft.Office.Interop.OneNote.Application
' Get all of the Notebook nodes.
Dim nodes As MSXML2.IXMLDOMNodeList
nodes = GetFirstOneNoteNotebookNodes(oneNote)
If Not nodes Is Nothing Then
' Get the first OneNote Notebook in the XML document.
Dim node As MSXML2.IXMLDOMNode
node = nodes(0)
Dim noteBookName As String = ""
noteBookName = node.attributes.getNamedItem("name").text
' Get the ID for the Notebook so the code can retrieve
' the list of sections.
Dim notebookID As String
notebookID = node.attributes.getNamedItem("ID").text
' Load the XML for the Sections for the Notebook requested.
Dim sectionsXml As String = ""
oneNote.GetHierarchy(notebookID, Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections, sectionsXml)
Dim secDoc As MSXML2.DOMDocument
secDoc = New MSXML2.DOMDocument
If secDoc.loadXML(sectionsXml) Then
' select the Section nodes
Dim secNodes As MSXML2.IXMLDOMNodeList
secNodes = secDoc.documentElement.selectNodes("//one:Section[@name='Balaji1']")
If Not secNodes Is Nothing Then
' Get the first section.
Dim secNode As MSXML2.IXMLDOMNode
secNode = secNodes(0)
Dim sectionName As String = ""
sectionName = secNode.attributes.getNamedItem("name").text
Dim sectionID As String
sectionID = secNode.attributes.getNamedItem("ID").text
Dim doc As MSXML2.DOMDocument
doc = New MSXML2.DOMDocument
'oneNote.GetHierarchy()
Dim sectionXML As String = ""
Dim newPageID As String = ""
sectionXML = ""
oneNote.GetHierarchy("", Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, sectionXML)
newPageID = GetPageIDByPageName(pageName, sectionXML)
' Create a new blank Page in the first Section
' using the default format.
If Len(newPageID) = 0 Then
oneNote.CreateNewPage(sectionID, newPageID, Microsoft.Office.Interop.OneNote.NewPageStyle.npsDefault)
End If
' Get the contents of the page.
Dim outXML As String = ""
oneNote.GetPageContent(newPageID, outXML, Microsoft.Office.Interop.OneNote.PageInfo.piAll)
' Load Page's XML into a MSXML2.DOMDocument object.
If doc.loadXML(outXML) Then
' Get Page Node.
Dim pageNode As MSXML2.IXMLDOMNode
pageNode = doc.selectSingleNode("//one:Page")
' Find the Title element.
Dim titleNode As MSXML2.IXMLDOMNode
titleNode = doc.selectSingleNode("//one:Page/one:Title/one:OE/one:T")
' Get the CDataSection where OneNote store's the Title's text.
Dim cdataChild As MSXML2.IXMLDOMNode
cdataChild = titleNode.selectSingleNode("text()")
'change the title will change the pageName
' Change the title in the local XML copy.
cdataChild.text = pageName
' Write the update to OneNote.
oneNote.UpdatePageContent(doc.xml)
Dim newElement As MSXML2.IXMLDOMElement
Dim newNode As MSXML2.IXMLDOMNode
' Create Outline node.
newElement = doc.createElement("one:Outline")
newNode = pageNode.appendChild(newElement)
' Create OEChildren.
newElement = doc.createElement("one:OEChildren")
newNode = newNode.appendChild(newElement)
' Create OE.
newElement = doc.createElement("one:OE")
newNode = newNode.appendChild(newElement)
' Create TE.
newElement = doc.createElement("one:T")
newNode = newNode.appendChild(newElement)
newElement = doc.createElement("one:Image")
newNode = newNode.appendChild(newElement)
' Add the text for the Page's content.
Dim cd As MSXML2.IXMLDOMCDATASection
cd = doc.createCDATASection("YOUR TEXT HERE")
newNode.appendChild(cd)
' Update OneNote with the new content.
oneNote.UpdatePageContent(doc.xml)
' Print out information about the update.
MsgBox("A new page was created in Section '" & sectionName & "' in Notebook '" & noteBookName & "'.")
Debug.Print(doc.xml)
End If
Else
MsgBox("OneNote 2010 Section nodes not found.")
End If
Else
MsgBox("OneNote 2010 Section XML Data failed to load.")
End If
Else
MsgBox("OneNote 2010 XML Data failed to load.")
End If
End Sub