Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel OneNote2013对未声明名称空间前缀的引用:';一个';错误_Excel_Vba_Onenote - Fatal编程技术网

Excel OneNote2013对未声明名称空间前缀的引用:';一个';错误

Excel OneNote2013对未声明名称空间前缀的引用:';一个';错误,excel,vba,onenote,Excel,Vba,Onenote,今天开始使用OneNote vba,所以我们在网上试用了示例程序。与OneNote 2013一样,对以下内容进行了基本更改(这将在OneNote中创建新页面)。但我尝试运行的每个程序都会返回错误- 引用未声明的命名空间前缀:“一”错误(对于粗体标记的行)。 谁能告诉我我做错了什么。我需要完成一项作业,通过它我可以运行打印屏幕的OCR并将数据返回excel,但首先,我认为正确掌握基本知识是一件好事。这花了整整一天的时间,我仍然无法让任何事情顺利进行。 顺便说一句,已对Onenote 15.0对象库

今天开始使用OneNote vba,所以我们在网上试用了示例程序。与OneNote 2013一样,对以下内容进行了基本更改(这将在OneNote中创建新页面)。但我尝试运行的每个程序都会返回错误- 引用未声明的命名空间前缀:“一”错误(对于粗体标记的行)。 谁能告诉我我做错了什么。我需要完成一项作业,通过它我可以运行打印屏幕的OCR并将数据返回excel,但首先,我认为正确掌握基本知识是一件好事。这花了整整一天的时间,我仍然无法让任何事情顺利进行。 顺便说一句,已对Onenote 15.0对象库和xml v6.0进行了引用。我是VBA的初学者,非常感谢您的帮助

Sub CreateNewPage()
    ' Connect to OneNote 2013.
    ' To see the results of the code,
    ' you'll want to ensure the OneNote 2013 user
    ' interface is visible.
    Dim OneNote As OneNote.Application
    Set 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
    If node.Attributes.getNamedItem(attributeName) Is Nothing Then
        GetAttributeValueFromNode = "Not found."
    Else
        GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
    End If
End Function

Private Function GetFirstOneNoteNotebookNodes(OneNote As OneNote.Application) As MSXML2.IXMLDOMNodeList

  ' Get the XML that represents the OneNote notebooks available.
    Dim notebookXml As String
    ' OneNote fills notebookXml with an XML document providing information
    ' about what OneNote notebooks are available.
    ' You want all the data and thus are providing an empty string
    ' for the bstrStartNodeID parameter.
    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
End Function

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

您的XML文档应该有类似xmlns:one=“”的内容

在顶端

名称空间是我在上面用粗体显示的内容。

查看MSXML API文档,了解如何在执行查询时指定命名空间。

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

您的XML文档应该有类似xmlns:one=“”的内容

在顶端

名称空间是我在上面用粗体显示的内容。
查看MSXML API文档,了解如何在执行查询时指定命名空间。

使用

doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
在调用SelectNodes方法之前,请先指定名称空间。

使用

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

在调用SelectNodes方法之前,请指定名称空间。

是的,我读了很多,感谢你指出了正确的方向,现在我对这个ps有了更好的理解。很抱歉,回复太晚了。是的,我读了很多,感谢你指出了正确的方向,现在我对这个ps有了更好的理解,很抱歉回复得太晚了