通过 VBA 在 OneNote 中创建新的分区/页面

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

我正在尝试从 Excel VBA 在 OneNote 中创建一个新的分区/页面,但始终遇到困难。

我尝试了3种方法,都没有成功。

  1. 使用 OpenHierarchy 在已知笔记本中创建一个部分

这是我尝试过的:

' Declare variables
Dim oneNote As oneNote.Application      ' Object to hold the OneNote application object
Dim XDoc As Object                      ' Object to hold the MSXML2.DOMDocument object
Dim sXML As String                      ' XML of the existing OneNote structure/hierarchy
Dim msg As String                       ' Generic string
Dim nodes As Object                     ' Holds a list of nodes returned by the XPath
Dim node As Object                      ' For looping through the list of nodes
Dim numberReturned As Long              ' Number of nodes returned from the XPath
Dim path As String                      ' ID of section to add page to

' Get OneNote hierarchy
Set oneNote = New oneNote.Application
Call oneNote.GetHierarchy("", hsPages, sXML, xs2013)

' Put XML into XML object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False
XDoc.validateOnParse = False
XDoc.LoadXML (sXML)

' Get list of nodes to rename
Set nodes = XDoc.SelectNodes("//one:Notebook[@name='Projects']")
numberReturned = 0
For Each node In nodes
    numberReturned = numberReturned + 1
Next node
If numberReturned = 0 Or numberReturned > 1 Then

    ' Not 1 node returned
    msg = "ERROR. Couldn't find the notebook."
    MsgBox msg
    Exit Sub

End If

' Create new page
For Each node In nodes

    ' Get section ID
    path = node.GetAttribute("path")
    ' cftNone     0   Creates no new object.
    ' cftNotebook 1   Creates a notebook by using the specified name and location.
    ' cftFolder   2   Creates a section group by using the specified name and location.
    ' cftSection  3   Creates a section by using the specified name and location.
    Call oneNote.OpenHierarchy(path, "", "my new secion", 3)

Next node

当我运行 OpenHierarchy 时,这会出现自动化错误,因此不会创建新部分。

  1. 我尝试使用 UpdateHierarchy 创建部分/页面(现有部分/新页面和新部分/新页面的各种组合)
   ' Declare variables
    Dim oneNote As oneNote.Application      ' Object to hold the OneNote application object
    Dim sXML As String                      ' XML of the existing OneNote structure/hierarchy

    ' Get OneNote hierarchy
    Set oneNote = New oneNote.Application

    ' Set XML and update hierarchy
    sXML = "<one:Notebook name=""Projects""><one:Section name=""Set up PARA""><one:Page name=""Set up OneNote notebooks""/></one:Section></one:Notebook>"
    Call oneNote.UpdateHierarchy(sXML, xs2013)

这也会产生自动化错误。我真的希望如果指定的部分不存在,这会创建一个部分,或者只是将页面添加到现有的部分,但没有骰子!

  1. 最后的努力实际上是将页面添加到现有部分,但重命名页面的最后一部分根本没有执行任何操作
    ' Declare variables
    Dim oneNote As oneNote.Application      ' Object to hold the OneNote application object
    Dim XDoc As Object                      ' Object to hold the MSXML2.DOMDocument object
    Dim sXML As String                      ' XML of the existing OneNote structure/hierarchy
    Dim msg As String                       ' Generic string
    Dim nodes As Object                     ' Holds a list of nodes returned by the XPath
    Dim node As Object                      ' For looping through the list of nodes
    Dim obj As Object
    Dim numberReturned As Long              ' Number of nodes returned from the XPath
    Dim id As String                        ' ID of section to add page to
    Dim newId As String                     ' ID of new page

    ' Get OneNote hierarchy
    Set oneNote = New oneNote.Application
    Call oneNote.GetHierarchy("", hsPages, sXML, xs2013)

    ' Put XML into XML object
    Set XDoc = CreateObject("MSXML2.DOMDocument")
    XDoc.async = False
    XDoc.validateOnParse = False
    XDoc.LoadXML (sXML)

    ' Get list of nodes to rename
    Set nodes = XDoc.SelectNodes("//one:Section[@name='Set up PARA']")
    numberReturned = 0
    For Each node In nodes
        numberReturned = numberReturned + 1
    Next node
    If numberReturned = 0 Or numberReturned > 1 Then

        ' Not 1 node returned
        msg = "ERROR. Should only be one node returned."
        MsgBox msg
        Exit Sub

    End If

    ' Create new page
    For Each node In nodes

        ' Get section ID
        id = node.GetAttribute("ID")
        Call oneNote.CreateNewPage(id, newId, npsDefault)

    Next node

    ' Give the page a name
    Call oneNote.GetHierarchy(newId, hsPages, sXML, xs2013)
    XDoc.LoadXML (sXML)
    Set nodes = XDoc.SelectNodes("//one:Page")
    For Each node In nodes
        Call node.SetAttribute("name", "My new page yo")
        Call oneNote.UpdateHierarchy(node.XML, xs2013)
    Next node

我没有主意,希望我在做一些愚蠢的事情......请帮忙!

excel vba onenote
1个回答
0
投票

我已经编辑了上面的代码并注释掉了我更改的行。该代码对我有用。

Option Explicit

' https://stackoverflow.com/questions/79285713/creating-new-section-page-in-onenote-via-vba

' Tools > References
' Microsoft OneNote 15.0 Object Library
' Microsoft XML, v6.0

Sub sbSectionPage()
' Declare variables
Dim OneNote As OneNote.Application      ' Object to hold the OneNote application object
'Dim xDoc As Object                      ' Object to hold the MSXML2.DOMDocument object
Dim sXML As String                      ' XML of the existing OneNote structure/hierarchy
Dim msg As String                       ' Generic string
'Dim nodes As Object                     ' Holds a list of nodes returned by the XPath
'Dim node As Object                      ' For looping through the list of nodes
Dim numberReturned As Long              ' Number of nodes returned from the XPath

' Get OneNote hierarchy
Set OneNote = New OneNote.Application
Call OneNote.GetHierarchy("", hsPages, sXML, xs2013)

' Put XML into XML object
'Set xDoc = CreateObject("MSXML2.DOMDocument")
    Dim xDoc As MSXML2.DOMDocument60
    Set xDoc = New MSXML2.DOMDocument60

xDoc.async = False
xDoc.validateOnParse = False
xDoc.LoadXML (sXML)

' Get list of nodes to rename
'Set nodes = XDoc.SelectNodes("//one:Notebook[@name='Projects']")
    xDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
'Set nodes = xDoc.SelectNodes("//one:Notebook[@name='Davids Notebook']")
'    Set nodes = xDoc.SelectSingleNode("//one:Notebook")

    Dim nodes As MSXML2.IXMLDOMNodeList
    Dim node As MSXML2.IXMLDOMNode

    Set nodes = xDoc.DocumentElement.SelectNodes("//one:Notebook")

numberReturned = 0
For Each node In nodes
    numberReturned = numberReturned + 1
        MsgBox node.Attributes.getNamedItem("name").Text
Next node
If numberReturned = 0 Then ''Or numberReturned > 1 Then

    ' Not 1 node returned
    msg = "ERROR. Couldn't find the notebook."
    MsgBox msg
    Exit Sub

End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.