OneNote2013-对未声明的命名空间前缀的引用:'one'错误

时间:2016-10-25 07:49:17

标签: excel vba onenote

今天开始使用OneNote vba,所以通过网络获取示例程序试用。对OneNote 2013进行了以下基本更改(在OneNote中创建了新页面)。但是我尝试运行的每个程序都返回错误 - 引用未声明的命名空间前缀:'one'错误(对于以粗体标记的行)。 任何人都可以让我知道我在这里做错了什么。我需要完成一项任务,通过该任务,我可以为打印屏幕运行OCR并将数据恢复到excel但是从那开始我认为获得正确的基础是一件好事。这花了一整天,我仍然无法做任何事情。 顺便提一下,对Onenote 15.0对象库和xml v6.0进行了引用。我是VBA的初学者,感谢任何帮助。

Sub CreateNewPage()     '连接到OneNote 2013。     '要查看代码的结果,     '您需要确保OneNote 2013用户     '界面可见。     Dim OneNote作为OneNote.Application     设置OneNote = New OneNote.Application

' Get all of the Notebook nodes.
Dim nodes As MSXML2.IXMLDOMNodeList
Set nodes = GetFirstOneNoteNotebookNodes(OneNote)
If Not nodes Is Nothing Then
    ' Get the first OneNote Notebook in the XML document.
    Dim node As MSXML2.IXMLDOMNode
    Set 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, hsSections, sectionsXml, xs2013

    Dim secDoc As MSXML2.DOMDocument60
    Set secDoc = New MSXML2.DOMDocument60

    If secDoc.LoadXML(sectionsXml) Then
        ' select the Section nodes
        Dim secNodes As MSXML2.IXMLDOMNodeList
        Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section")

        If Not secNodes Is Nothing Then
            ' Get the first section.
            Dim secNode As MSXML2.IXMLDOMNode
            Set secNode = secNodes(0)

            Dim sectionName As String
            sectionName = secNode.Attributes.getNamedItem("name").Text
            Dim sectionID As String
            sectionID = secNode.Attributes.getNamedItem("ID").Text

            ' Create a new blank Page in the first Section
            ' using the default format.
            Dim newPageID As String
            OneNote.CreateNewPage sectionID, newPageID, npsDefault

            ' Get the contents of the page.
            Dim outXML As String
            OneNote.GetPageContent newPageID, outXML, piAll, xs2013

            Dim doc As MSXML2.DOMDocument60
            Set doc = New MSXML2.DOMDocument60
            ' Load Page's XML into a MSXML2.DOMDocument object.
            If doc.LoadXML(outXML) Then
                ' Get Page Node.
                Dim pageNode As MSXML2.IXMLDOMNode
                Set pageNode = doc.SelectSingleNode("//one:Page")

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

                ' Change the title in the local XML copy.
                cdataChild.Text = "A Page Created from VBA"
                ' Write the update to OneNote.
                OneNote.UpdatePageContent doc.XML

                Dim newElement As MSXML2.IXMLDOMElement
                Dim newNode As MSXML2.IXMLDOMNode

                ' Create Outline node.
                Set newElement = doc.createElement("one:Outline")
                Set newNode = pageNode.appendChild(newElement)
                ' Create OEChildren.
                Set newElement = doc.createElement("one:OEChildren")
                Set newNode = newNode.appendChild(newElement)
                ' Create OE.
                Set newElement = doc.createElement("one:OE")
                Set newNode = newNode.appendChild(newElement)
                ' Create TE.
                Set newElement = doc.createElement("one:T")
                Set newNode = newNode.appendChild(newElement)

                ' Add the text for the Page's content.
                Dim cd As MSXML2.IXMLDOMCDATASection
                Set cd = doc.createCDATASection("Text added to a new OneNote page via VBA.")

                newNode.appendChild cd


                ' Update OneNote with the new content.
                OneNote.UpdatePageContent doc.XML

                ' Print out information about the update.
                Debug.Print "A new page was created in "
                Debug.Print "Section " & sectionName & " in"
                Debug.Print "Notebook " & noteBookName & "."
                Debug.Print "Contents of new Page:"

                Debug.Print doc.XML
            End If
        Else
            MsgBox "OneNote 2013 Section nodes not found."
        End If
    Else
        MsgBox "OneNote 2013 Section XML Data failed to load."
    End If
Else
    MsgBox "OneNote 2013 XML Data failed to load."
End If

End Sub

Private Function GetAttributeValueFromNode(node as MSXML2.IXMLDOMNode,attributeName As String)As String     如果node.Attributes.getNamedItem(attributeName)是Nothing Then         GetAttributeValueFromNode =“未找到。”     其他         GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text     万一 结束功能

私有函数GetFirstOneNoteNotebookNodes(OneNote As OneNote.Application)作为MSXML2.IXMLDOMNodeList

'获取代表OneNote笔记本的XML。     Dim notebookXml As String     'OneNote使用提供信息的XML文档填充notebookXml     '关于OneNote笔记本电脑的可用性。     '你想要所有的数据,因此提供一个空字符串     '为bstrStartNodeID参数。     OneNote.GetHierarchy“”,hsNotebooks,notebookXml,xs2013

' Use the MSXML Library to parse the XML.
Dim doc As MSXML2.DOMDocument60
Set doc = New MSXML2.DOMDocument60

If doc.LoadXML(notebookXml) Then
    **Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")**
Else
    Set GetFirstOneNoteNotebookNodes = Nothing
End If

结束功能

2 个答案:

答案 0 :(得分:2)

使用

doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"

在调用 SelectNodes 方法之前,指定您的命名空间。

答案 1 :(得分:0)

您需要指定命名空间。 “one:”不是命名空间,它只是一个别名。

您的XML文档应该包含xmlns:one =“ http://schemas.microsoft.com/office/onenote/12/2004/onenote

在顶部。

命名空间是我上面加粗的 检查MSXML API文档以了解在执行查询时如何指定命名空间。