当我尝试通过 VBA 更新 OneNote 中新创建的页面的内容时,出现“无效 XML”错误。我的 XML 很简单,并且通过了在线验证器,所以我没有想法。 XML 字符串:
<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote" ID="SOME ID">
<one:Title>
<one:Text>Updated Page with Complex Content</one:Text>
</one:Title>
<one:Outline>
<one:OEChildren>
<one:OE>
<one:Text>
<one:ParagraphStyle>Body</one:ParagraphStyle>
<one:Text>Dummy Text</one:Text>
</one:Text>
</one:OE>
</one:OEChildren>
</one:Outline>
</one:Page>
简单的答案是,这是对错误消息的误解
' https://learn.microsoft.com/en-us/office/client-developer/onenote/error-codes-onenote
' 0x80042000 XML 格式不正确。
' 0x80042001 XML 无效。
XML 格式不正确意味着 XML 定义中存在语法错误,例如缺少“<"
XML 无效意味着 XML 相对于 XSD 为命名空间定义的 XML 词汇表无效 -
在本例中为“http://schemas.microsoft.com/office/onenote/2013/onenote”
请注意,如果您将 Notepad++ 与 XML 插件一起使用,则 XML 需要线性化 或者例程“OneNote.UpdatePageContent”给出“格式不正确”错误,因为有 是文件中额外的 CR 和 LF 字符,以使其易于阅读。
这是 OneNote 中空白页面的 XML -
<?xml version="1.0"?>
<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote"
ID="{00}"
name="Blank page 0857-01"
dateTime="2024-10-15T07:57:51.000Z"
lastModifiedTime="2024-10-15T08:44:27.000Z"
pageLevel="1"
isCurrentlyViewed="true">
<one:QuickStyleDef index="0"
name="PageTitle"
fontColor="automatic"
highlightColor="automatic"
font="Calibri Light"
fontSize="20.0"
spaceBefore="0.0"
spaceAfter="0.0"/>
<one:PageSettings RTL="false"
color="automatic">
<one:PageSize>
<one:Automatic/>
</one:PageSize>
<one:RuleLines visible="false"/>
</one:PageSettings>
<one:Title lang="en-GB">
<one:OE author="David Tallett"
authorInitials="DT"
lastModifiedBy="David Tallett"
lastModifiedByInitials="DT"
creationTime="2024-10-15T08:04:39.000Z"
lastModifiedTime="2024-10-15T08:44:20.000Z"
objectID="{5C75DD49-BD75-49BB-96D6-F81794548E33}{15}{B0}"
alignment="left"
quickStyleIndex="0">
<one:T><![CDATA[Blank page 0857-01]]></one:T>
</one:OE>
</one:Title>
</one:Page>
正如你所见,词汇并不简单。此文本是使用 Notepad ++ 和 XML 工具插件的“漂亮打印 - 缩进属性”。 该文本将无法正确加载,必须线性化才能加载。
这是用于加载此页面的代码。
Option Explicit
Sub sbUpdatePageXML()
Dim OneNote As New OneNote.Application
Dim sPageXML As String
Dim sPageID As String
' replace the ID in each .XML file with {00} so that it can be replaced with the actual ID of the page
' sPageXML = fnRead2("C:\Users\david\Desktop\SOME ID.xml")
sPageXML = fnRead2("C:\Users\david\Desktop\OneNote - Blank page 0857-01.xml")
' make sure the page name below is unique
sPageID = fnGetPageID("Page2")
MsgBox sPageID
If sPageID = "" Then
MsgBox "page not found"
Exit Sub
End If
sPageXML = Replace(sPageXML, "{00}", sPageID)
MsgBox sPageXML
OneNote.UpdatePageContent sPageXML, , xs2013, False
End Sub
' https://learn.microsoft.com/en-us/office/client-developer/onenote/error-codes-onenote
' 0x80042000 The XML is not well-formed.
' 0x80042001 The XML is invalid.
' the XML needed to be linearised!!!
Public Function fnRead2(sFile As String) As String
Dim sText As String
Dim i As Integer
Open sFile For Input As #1
sText = ""
For i = 1 To 1000
If Not EOF(1) Then sText = sText & Input(1, #1)
Next i
Close #1
fnRead2 = sText
End Function
Function fnGetPageID(sPageName As String) As String
Dim OneNote As New OneNote.Application
Dim sPagesXML As String
OneNote.GetHierarchy "", hsPages, sPagesXML, xs2013
Dim xDoc As New MSXML2.DOMDocument60
xDoc.LoadXML (sPagesXML)
Dim nodes As MSXML2.IXMLDOMNodeList
Dim node As MSXML2.IXMLDOMNode
xDoc.SetProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'" ' set this property before using SelectNodes
Set nodes = xDoc.DocumentElement.SelectNodes("//one:Page")
For Each node In nodes
If sPageName = node.Attributes.getNamedItem("name").Text Then
fnGetPageID = node.Attributes.getNamedItem("ID").Text
Exit Function
End If
Next
' if the name given for the page does not exist, return the first page found
Dim node0 As MSXML2.IXMLDOMNode
Set node0 = nodes(0)
fnGetPageID = node0.Attributes.getNamedItem("ID").Text
End Function
使用以下 VBA 代码段查看 OneNote 笔记本中所有页面的 XML。
Option Explicit
Sub sbGetPagesXML()
Dim OneNote As New OneNote.Application
Dim sPagesXML As String
OneNote.GetHierarchy "", hsPages, sPagesXML, xs2013
MsgBox sPagesXML
sbPrint (sPagesXML)
End Sub
Sub sbPrint(sText As String): Open "C:\tmp\z-" & Format(Now(), "HHMMSS") & ".txt" For Output As #1: Print #1, sText: Close #1: End Sub
软件版本
记事本++ v8.7(64位)
XML 工具插件版本 3.1.1.13 unicode 64 位
Microsoft® OneNote® for Microsoft 365 MSO(版本 2409 内部版本 16.0.18025.20160)64 位
适用于 Microsoft 365 MSO 的 Microsoft® Excel®(版本 2409 内部版本 16.0.18025.20160)64 位 版本
Windows 11 家庭版 23H2 操作系统内部版本 22631.4317