首页 > 解决方案 > VBA 和 OneNote。在另一个部分中移动页面:OneNote.UpdateContentPage 方法出错

问题描述

我编写了一个子例程来将页面移动到与原始部分相比不同的部分,但它不起作用!有人可以帮助我吗?

Private Sub CreateInNewSection(onote As OneNote.Application, pageXML As String, newSecId As String, title As String)

    Dim pDoc As MSXML2.DOMDocument60
    Set pDoc = New MSXML2.DOMDocument60
    If pDoc.LoadXML(pageXML) Then
        Dim cNodes As MSXML2.IXMLDOMNodeList
        Dim fNodes As MSXML2.IXMLDOMNodeList
        Dim iNodes As MSXML2.IXMLDOMNodeList
        Dim cNode As MSXML2.IXMLDOMNode
        Dim fNode As MSXML2.IXMLDOMNode
        Dim iNode As MSXML2.IXMLDOMNode
        soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        pDoc.setProperty "SelectionNamespaces", soapNS
        Set cNodes = pDoc.DocumentElement.SelectNodes("//one:T")
        Set fNodes = pDoc.DocumentElement.SelectNodes("//one:InsertedFile")
        Set iNodes = pDoc.DocumentElement.SelectNodes("//one:Image")
        Dim nPageID As String
        onote.CreateNewPage newSecId, nPageID, npsDefault
        Dim oXML As String
        onote.GetPageContent nPageID, oXML, piAll, xs2013

        'oXML = pageXML
        Dim nDoc As MSXML2.DOMDocument60
        Set nDoc = New MSXML2.DOMDocument60
        If nDoc.LoadXML(oXML) Then
            Dim npNode As MSXML2.IXMLDOMNode
            soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
            nDoc.setProperty "SelectionNamespaces", soapNS
            Set npNode = nDoc.SelectSingleNode("//one:Page")

            ' Find the Title element.
            Dim tNode As MSXML2.IXMLDOMNode
            Set tNode = nDoc.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
            Set cdataChild = tNode.SelectSingleNode("text()")

            ' Change the title in the local XML copy.
            cdataChild.Text = title
            ' Write the update to OneNote.
            'oneNote.UpdatePageContent doc.XML

            '---------- For Text Nodes -----------

            For Each cNode In cNodes
                If cNode.Text <> "" Then
                    Dim newTextNodeElement As MSXML2.IXMLDOMElement
                    Dim newTextNode As MSXML2.IXMLDOMNode
                    ' Create Outline node.
                    Set newTextNodeElement = nDoc.createElement("one:Outline")
                    Set newTextNode = npNode.appendChild(newTextNodeElement)
                    ' Create OEChildren.
                    Set newTextNodeElement = nDoc.createElement("one:OEChildren")
                    Set newTextNode = newTextNode.appendChild(newTextNodeElement)
                    ' Create OE.
                    Set newTextNodeElement = nDoc.createElement("one:OE")
                    Set newTextNode = newTextNode.appendChild(newTextNodeElement)
                    ' Create TE.
                    Set newTextNodeElement = nDoc.createElement("one:T")
                    Set newTextNode = newTextNode.appendChild(newTextNodeElement)

                    ' Add the text for the Page's content.
                    Dim newcd As MSXML2.IXMLDOMCDATASection
                    Set newcd = nDoc.createCDATASection(cNode.Text)
                    newTextNode.appendChild newcd
                End If
            Next

                    ---------- For File Nodes -----------

            For Each fNode In fNodes

                'Set newFileNode = fNode
                Set npNode = npNode.appendChild(fNode)

            Next

            onote.UpdatePageContent nDoc.XML, DateTime.Now, xs2013

        End If
    End If

End Sub

onenote.UpdatePageContent 继续失败,出现运行时错误 -2147213296 (80042010)。如果我只考虑文本节点,代码就可以工作,而如果我为文件节点添加代码,它就不再工作了。

我尝试使用以下代码更改文件节点的代码:

For Each fNode In fNodes            

    Dim newFileNodeElement As MSXML2.IXMLDOMElement
    Dim newFileNode As MSXML2.IXMLDOMNode

    'Set newFileNode = fNode
    'Set npNode = npNode.appendChild(fNode)

    Set newFileNodeElement = nDoc.createElement("one:InsertedFile")
    Set newFileNode = npNode.appendChild(newFileNodeElement)

    For i = 0 To fNode.Attributes.Length - 1
        Dim attrName As String
        Dim attrValue As String
        Dim attr As MSXML2.IXMLDOMAttribute
        Dim namedNodeMap As MSXML2.IXMLDOMNamedNodeMap

        attrName = fNode.Attributes(i).nodeName
        attrValue = fNode.Attributes(i).NodeValue
        Set attr = nDoc.createNode(2, attrName, "")
        attr.Value = attrValue

        Set namedNodeMap = nDoc.DocumentElement.LastChild.Attributes
        Set newFileNode = namedNodeMap.setNamedItem(attr)


    Next i      
Next

但结果是一样的。

标签: excelxmlvbaonenote

解决方案


我使用不同的方法解决了这个问题:我使用 Publish 方法来创建一个带有我需要复制的页面名称的新部分,并使用 MergeSections 方法来复制我希望的部分中的页面。这是代码:

Private Sub CreateInNewSection(onote As onenote.Application, pageXML As String, 
newSecId As String, title As String, nbID As String, path As String)

Dim pDoc As MSXML2.DOMDocument60
Set pDoc = New MSXML2.DOMDocument60
If pDoc.LoadXML(pageXML) Then
    Dim pNode As MSXML2.IXMLDOMNode
    soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
    pDoc.setProperty "SelectionNamespaces", soapNS
    Set pNode = pDoc.SelectSingleNode("//one:Page")
    Dim oldPageId As String
    Dim oldPageName As String
    oldPageId = pNode.Attributes.getNamedItem("ID").Text
    oldPageName = pNode.Attributes.getNamedItem("name").Text
    path = path + "\" + oldPageName + ".one"
    Debug.Print path

    onote.Publish oldPageId, path, pfOneNote, ""

    Dim sXml As String
    onote.GetHierarchy nbID, hsSections, sXml, xs2013
    Dim sDoc As MSXML2.DOMDocument60
    Set sDoc = New MSXML2.DOMDocument60
    Dim nowSecId As String

    If sDoc.LoadXML(sXml) Then
        ' select the Section nodes
        Dim sNodes As MSXML2.IXMLDOMNodeList
        soapNS = "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        sDoc.setProperty "SelectionNamespaces", soapNS
        Set sNodes = sDoc.DocumentElement.SelectNodes("//one:Section")

        Dim j As Integer
        If Not sNodes Is Nothing Then
            ' Get the first section.
            Dim sNode As MSXML2.IXMLDOMNode
            For j = 0 To (sNodes.Length - 1)
                If sNodes(j).Attributes.getNamedItem("name").Text = oldPageName Then
                    nowSecId = sNodes(j).Attributes.getNamedItem("ID").Text
                    Exit For
                End If
            Next j
        End If
        onote.MergeSections nowSecId, newSecId
        onote.DeleteHierarchy nowSecId
        onote.DeleteHierarchy oldPageId
    End If
End If

End Sub

推荐阅读