有没有办法通过代码与OneNote对话?

问题描述 投票:0回答:3

我喜欢使用 OneNote,但我希望更好地控制笔记的位置以及笔记的生成方式。 我非常精通 VBA,也可以使用 Python(这些是我可以在我的机器上编写代码的唯一语言),但是我还没有找到通过代码与 OneNote 交互的好方法。 我使用的是 OneNote 2003,它看起来不像 2007 那样具有 xml 导出功能。 有没有人找到一种简单的方法来使用 Python 或 VBA 读写 OneNote 文件?

xml vba ms-office onenote
3个回答
4
投票

OneNote 2007 有更多选项,但 OneNote 2003 仅支持 CSimpleImporterClass API 以编程方式将数据导入 OneNote。 CSimpleImporterClass 将允许您使用 VB 将图像、墨迹和 HTML 引入 OneNote。

读取 OneNote 2003 文件可能有点困难。我不知道有什么资源可以让您轻松使用 Python 或 VBA 准备 OneNote 2003 文件。


0
投票

对于 OneNote 笔记本、分区或页面上的基本 CRUD(创建、读取、更新、删除)操作,您还可以使用 Python 中的 REST API


0
投票

这里是来自这个 Microsoft 示例的工作代码。该代码需要进行一些更正才能使其与 Office 365 保持同步,这些更正显示在注释中、注释掉的原始行及其下面的新行。

Option Explicit

' https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/hh377183(v=office.14)

Sub sbCreateNewPage()
    ' Connect to OneNote 2010.
    ' To see the results of the code,
    ' you'll want to ensure the OneNote 2010 user
    ' interface is visible.
'    Dim oneNote As oneNote14.Application
    Dim oneNote As oneNote.Application
'    Set oneNote = New oneNote14.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, xs2010
        oneNote.GetHierarchy notebookID, hsSections, sectionsXml, xs2013

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

        If secDoc.LoadXML(sectionsXml) Then
            ' select the Section nodes
            Dim secNodes As MSXML2.IXMLDOMNodeList
            
            secDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
            ' change from the original to define the one: abbreviation
            
            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, xs2010
                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
                    
                    doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
                    ' change from the original to define the one: abbreviation
                    
                    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" & Format(Now(), " - YYYYMMDD HH:MM")
                    ' 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." & Format(Now(), " - YYYYMMDD HH:MM"))

                    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 2010 Section nodes not found. error 01"
            End If
        Else
            MsgBox "OneNote 2010 Section XML Data failed to load. error 02"
        End If
    Else
        MsgBox "OneNote 2010 XML Data failed to load. error 03"
    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. error 04"
    Else
        GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
    End If
End Function

'Private Function GetFirstOneNoteNotebookNodes(oneNote As oneNote14.Application) As MSXML2.IXMLDOMNodeList
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, xs2010
    oneNote.GetHierarchy "", hsNotebooks, notebookXml, xs2013

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

    If doc.LoadXML(notebookXml) Then
                    
        doc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
        ' change from the original to define the one: abbreviation
                    
        Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook")
    Else
        Set GetFirstOneNoteNotebookNodes = Nothing
    End If
End Function
© www.soinside.com 2019 - 2024. All rights reserved.