You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Excel VBA在OneNote中创建新分区/页面遇问题求助

Excel VBA操作OneNote创建分区/页面失败问题解决

方法1:OpenHierarchy创建分区的错误修正

你用node.GetAttribute("path")获取的是笔记本的文件系统路径,但OpenHierarchy创建分区时,第一个参数需要传入笔记本的ID,而不是路径。另外代码里存在拼写错误:"my new secion"应为"my new section",同时XML查询未指定命名空间可能导致节点匹配失败。

修正后的代码:

' 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 numberReturned As Long              ' Number of nodes returned from the XPath
Dim notebookId As String                ' ID of the target notebook

' Get OneNote hierarchy (only notebook level for efficiency)
Set oneNote = New oneNote.Application
Call oneNote.GetHierarchy("", hsNotebooks, sXML, xs2013)

' Put XML into XML object with namespace declaration
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False
XDoc.validateOnParse = False
XDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
XDoc.LoadXML (sXML)

' Get target notebook node
Set nodes = XDoc.SelectNodes("//one:Notebook[@name='Projects']")
numberReturned = nodes.Length ' Directly get node count without looping
If numberReturned = 0 Or numberReturned > 1 Then
    msg = "ERROR. Couldn't find the notebook."
    MsgBox msg
    Exit Sub
End If

' Get notebook ID instead of file path
notebookId = nodes(0).GetAttribute("ID")
' Create new section
Call oneNote.OpenHierarchy(notebookId, "", "my new section", cftSection)

方法2:UpdateHierarchy创建分区/页面的错误修正

UpdateHierarchy无法仅靠名称匹配父对象,必须指定父对象的ID,且XML必须包含正确的OneNote命名空间声明。你构造的XML缺少命名空间,导致API无法解析结构。

修正后的代码(需先获取Projects笔记本的ID,参考方法1逻辑):

Dim oneNote As oneNote.Application
Dim sXML As String
Dim notebookId As String ' Pre-fetched ID of the "Projects" notebook

Set oneNote = New oneNote.Application
' Construct valid XML with namespace and parent ID
sXML = "<one:Notebook ID='" & notebookId & "' xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'>" & _
       "  <one:Section name='Set up PARA'>" & _
       "    <one:Page name='Set up OneNote notebooks' />" & _
       "  </one:Section>" & _
       "</one:Notebook>"
Call oneNote.UpdateHierarchy(sXML, xs2013)

方法3:页面重命名无效的错误修正

创建页面后需确保获取到完整的页面XML,更新时要传递包含命名空间的完整节点内容,同时需确认节点匹配成功再执行更新操作。

修正后的代码:

' Declare variables
Dim oneNote As oneNote.Application
Dim XDoc As Object
Dim sXML As String
Dim msg As String
Dim nodes As Object
Dim numberReturned As Long
Dim sectionId As String
Dim newPageId As String

' Get OneNote hierarchy (only section level for efficiency)
Set oneNote = New oneNote.Application
Call oneNote.GetHierarchy("", hsSections, sXML, xs2013)

' Put XML into XML object with namespace declaration
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False
XDoc.validateOnParse = False
XDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"
XDoc.LoadXML (sXML)

' Get target section
Set nodes = XDoc.SelectNodes("//one:Section[@name='Set up PARA']")
numberReturned = nodes.Length
If numberReturned = 0 Or numberReturned > 1 Then
    msg = "ERROR. Should only be one node returned."
    MsgBox msg
    Exit Sub
End If

' Create new page
sectionId = nodes(0).GetAttribute("ID")
Call oneNote.CreateNewPage(sectionId, newPageId, npsDefault)

' Rename the new page
Call oneNote.GetHierarchy(newPageId, hsPages, sXML, xs2013)
XDoc.LoadXML (sXML)
Set nodes = XDoc.SelectNodes("//one:Page")
If nodes.Length > 0 Then
    nodes(0).SetAttribute "name", "My new page yo"
    ' Pass complete page XML with namespace to update
    Call oneNote.UpdateHierarchy(nodes(0).XML, xs2013)
End If

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 12:00:54