OneNote 2016:无法使Publish方法起作用 [英] OneNote 2016: Cannot get the Publish method to work
问题描述
我正在尝试使用OneNote API的Publish方法将OneNote内容转换为Word。
我正在使用的VBA代码(从代码中找到的代码修改) 'net,派生自旧的MSDN示例)如下:
Private Function GetFirstOneNoteNotebookNodes(oneNote as oneNote.Application)As MSXML2.IXMLDOMNodeList
'Get表示可用的OneNote笔记本的XML。
Dim notebookXml As String
'OneNote用一个XML文档填充notebookXml,提供有关OneNote笔记本可用信息的
'信息。
'您想要所有数据,因此为bstrStartNodeID参数提供空字符串
'。
oneNote.GetHierarchy"",hsNotebooks,notebookXml',xs2010
'使用MSXML库解析XML。
Dim doc As MSXML2.DOMDocument60
设置doc = New MSXML2.DOMDocument60
doc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft的.com /办公室/ OneNote中/ 2013 / OneNote的"""
如果是doc.LoadXML(notebookXml)那么
设置GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes(" // one:Notebook")
Else
Set GetFirstOneNoteNotebookNodes =没有
结束如果
结束函数
私有函数GetAttributeValueFromNode(节点为MSXML2.IXMLDOMNode,attributeName为String)As String
如果是node.Attributes。 getNamedItem(attributeName)Is Nothing Then
GetAttributeValueFromNode =" Not found。"
Else
GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
End if
End Function
Sub PublishFirstPageOfFirstSectionOfFirstNotebookToWord()
'连接到OneNote 2016.
Dim oneNote作为oneNote.Application
设置oneNote = New oneNote.Application
'获取所有Notebook节点。
Dim nodes As MSXML2.IXMLDOMNodeList
Set nodes = GetFirstOneNoteNotebookNodes(oneNote)
If Not nodes Is Nothing Then
'获取XML文档中的第一个OneNote Notebook。
Dim node As MSXML2.IXMLDOMNode
Set node = nodes(0)
Dim noteBookName As String
noteBookName = node.Attributes.getNamedItem(" name")。Text
'获取Notebook的ID,以便代码可以检索
'部分列表。
Dim notebookID As String
notebookID = node.Attributes.getNamedItem(" ID")。Text
'为所请求的Notebook部分加载XML。
Dim sectionsXml As String
oneNote.GetHierarchy notebookID,hsSections,sectionsXml',xs2010
Dim secDoc As MSXML2.DOMDocument60
Set secDoc = New MSXML2.DOMDocument60
secDoc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft.com/office/onenote/2013/onenote"""
如果secDoc.LoadXML(sectionsXml)则
Dim secNodes As MSXML2.IXMLDOMNodeList
Set secNodes = secDoc.DocumentElement.SelectNodes(" // one:Section")
如果不是secNodes则没有那么
Dim secNode As MSXML2.IXMLDOMNode
Set secNode = secNodes(0)
Dim sectionID As String
For Each secNode in secNodes
sectionID = GetAttributeValueFromNode(secNode," ID")
'为请求的部分加载页面的XML。
Dim pagesXml As String
oneNote.GetHierarchy sectionID,hsPages,pagesXml',xs2010
Dim pagesDoc As MSXML2.DOMDocument60
Set pagesDoc = New MSXML2.DOMDocument60
pagesDoc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft.com/office/onenote/2013/onenote"""
如果pagesDoc.LoadXML(pagesXml)那么
Dim pageNodes As MSXML2.IXMLDOMNodeList
设置pageNodes = pagesDoc.DocumentElement.SelectNodes(" // one:Page")
如果不是pageNodes则没有那么
Dim pageNode As MSXML2.IXMLDOMNode
Set pageNode = pageNodes(0)
Dim pageName As String
Dim pageID As String
For each pageNode in pageNodes
On Error Resume Next
pageName = GetAttributeValueFromNode(pageNode," name")
pageID = GetAttributeValueFromNode(pageNode," ; ID")
'为输出创建文件夹路径section
Dim sectionName As String
sectionName = GetAttributeValueFromNode(secNode," name")
Dim sectionPath As String
sectionPath = sectionName& " \"
'获取用户指定的输出文件夹。
Dim outputFolder As String
outputFolder = FILE_PATH
'使用页面名称创建文件名。
Dim fileName As String
fileName = pageName& " .DOCX"
'将两个值合并为一个
'变量,这样它就更容易使用两次。
Dim publishContentTo As String
publishContentTo = outputFolder& sectionPath& fileName
publishContentTo = Replace(publishContentTo," /"," _")
'将用户指定的文件夹中的页面内容发布到PDF文件
'。
'除非你使用
'pfEmf枚举,否则不需要最后一个参数。
'注意如果文件已在指定位置存在
',则此方法将失败。
On Error GoTo 0
oneNote.Publish pageID,publishContentTo,pfWord
On Error Resume Next
Next
Else
MsgBox" OneNote 2016找不到页面节点。"
End if
Else
MsgBox" OneNote 2016 Pages XML数据无法加载。
End if
Next
Else
MsgBox" OneNote 2016 Section节点未找到。
End if
Else
MsgBox" OneNote 2016 Section XML数据无法加载。
End if
Else
MsgBox" OneNote 2016 XML数据无法加载。
End if
End Sub
" oneNote.Publish pageID,publishContentTo,pfWord
"声明失败如下:
任何线索?
< blockquote>
您好sba923,
您遇到了哪些错误?我用你的代码进行了测试,它在大多数条件下都能正常工作。当文件存在或者您要导出的文件名包含无效字符(例如":")时,我收到错误"自动化错误"。
我建议检查哪个页面导致此错误,并检查文件路径和文件名。您可以尝试使用相同的文件名手动导出页面,以检查是否可以正确导出到单词。
以下是测试代码:
Const FILE_PATH =" C:\ Users \Administrator \Desktop \"
私有函数GetFirstOneNoteNotebookNodes(oneNote as oneNote.Application)As MSXML2.IXMLDOMNodeList
'获取表示OneNote笔记本可用的XML。
Dim notebookXml As String
'OneNote用一个XML文档填充notebookXml,提供有关OneNote笔记本可用信息的
'信息。
'您想要所有数据,因此为bstrStartNodeID参数提供空字符串
'。
oneNote.GetHierarchy"",hsNotebooks,notebookXml',xs2010
'使用MSXML库解析XML。
Dim doc As MSXML2.DOMDocument60
设置doc = New MSXML2.DOMDocument60
doc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft的.com /办公室/ OneNote中/ 2013 / OneNote的"""
如果是doc.LoadXML(notebookXml)那么
设置GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes(" // one:Notebook")
Else
Set GetFirstOneNoteNotebookNodes =没有
结束如果
结束函数
私有函数GetAttributeValueFromNode(节点为MSXML2.IXMLDOMNode,attributeName为String)As String
如果是node.Attributes。 getNamedItem(attributeName)Is Nothing Then
GetAttributeValueFromNode =" Not found。"
Else
GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
End if
End Function
Sub PublishFirstPageOfFirstSectionOfFirstNotebookToWord()
'连接到OneNote 2016.
Dim oneNote作为oneNote.Application
设置oneNote = New oneNote.Application
'获取所有Notebook节点。
Dim nodes As MSXML2.IXMLDOMNodeList
Set nodes = GetFirstOneNoteNotebookNodes(oneNote)
If Not nodes Is Nothing Then
'获取XML文档中的第一个OneNote Notebook。
Dim node As MSXML2.IXMLDOMNode
Set node = nodes(0)
Dim noteBookName As String
noteBookName = node.Attributes.getNamedItem(" name")。Text
'获取Notebook的ID,以便代码可以检索
'部分列表。
Dim notebookID As String
notebookID = node.Attributes.getNamedItem(" ID")。Text
'为所请求的Notebook部分加载XML。
Dim sectionsXml As String
oneNote.GetHierarchy notebookID,hsSections,sectionsXml',xs2010
Dim secDoc As MSXML2.DOMDocument60
Set secDoc = New MSXML2.DOMDocument60
secDoc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft.com/office/onenote/2013/onenote"""
如果secDoc.LoadXML(sectionsXml)则
Dim secNodes As MSXML2.IXMLDOMNodeList
Set secNodes = secDoc.DocumentElement.SelectNodes(" // one:Section")
如果不是secNodes则没有那么
Dim secNode As MSXML2.IXMLDOMNode
Set secNode = secNodes(0)
Dim sectionID As String
For Each secNode in secNodes
sectionID = GetAttributeValueFromNode(secNode," ID")
'为请求的部分加载页面的XML。
Dim pagesXml As String
oneNote.GetHierarchy sectionID,hsPages,pagesXml',xs2010
Dim pagesDoc As MSXML2.DOMDocument60
Set pagesDoc = New MSXML2.DOMDocument60
pagesDoc.setProperty" SelectionNamespaces"," xmlns:one ="" http://schemas.microsoft.com/office/onenote/2013/onenote"""
如果pagesDoc.LoadXML(pagesXml)那么
Dim pageNodes As MSXML2.IXMLDOMNodeList
设置pageNodes = pagesDoc.DocumentElement.SelectNodes(" // one:Page")
如果不是pageNodes则没有那么
Dim pageNode As MSXML2.IXMLDOMNode
Set pageNode = pageNodes(0)
Dim pageName As String
Dim pageID As String
For each pageNode in pageNodes
On Error Resume Next
pageName = GetAttributeValueFromNode(pageNode," name")
pageID = GetAttributeValueFromNode(pageNode," ; ID")
'为输出创建文件夹路径section
Dim sectionName As String
sectionName = GetAttributeValueFromNode(secNode," name")
Dim sectionPath As String
sectionPath = sectionName& " \"
'获取用户指定的输出文件夹。
Dim outputFolder As String
outputFolder = FILE_PATH
'使用页面名称创建文件名。
Dim fileName As String
fileName = pageName& " .DOCX"
'将两个值合并为一个
'变量,这样它就更容易使用两次。
Dim publishContentTo As String
publishContentTo = outputFolder& sectionPath& fileName
publishContentTo = Replace(publishContentTo," /"," _")
'将用户指定的文件夹中的页面内容发布到PDF文件
'。
'除非你使用
'pfEmf枚举,否则不需要最后一个参数。
'注意如果文件已在指定位置存在
',则此方法将失败。
On Error GoTo 0
如果Dir(publishContentTo)=""然后
oneNote.Publish pageID,publishContentTo,pfWord
End if
On Error Resume Next
Next
Else
MsgBox" OneNote 2016找不到页面节点。"
End if
Else
MsgBox" OneNote 2016 Pages XML数据无法加载。
End if
Next
Else
MsgBox" OneNote 2016 Section节点未找到。
End if
Else
MsgBox" OneNote 2016 Section XML数据无法加载。
End if
Else
MsgBox" OneNote 2016 XML数据无法加载。
结束如果
结束次级
最好的问候,
Edward
I'm trying to convert OneNote content to Word using the Publish method of the OneNote API.
The VBA code I'm using (modified from code found on the 'net, derived from an old MSDN sample) is the following:
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 ' Use the MSXML Library to parse the XML. Dim doc As MSXML2.DOMDocument60 Set doc = New MSXML2.DOMDocument60 doc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If doc.LoadXML(notebookXml) Then Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook") Else Set GetFirstOneNoteNotebookNodes = Nothing End If End Function 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 Sub PublishFirstPageOfFirstSectionOfFirstNotebookToWord() ' Connect to OneNote 2016. 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 ', xs2010 Dim secDoc As MSXML2.DOMDocument60 Set secDoc = New MSXML2.DOMDocument60 secDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If secDoc.LoadXML(sectionsXml) Then Dim secNodes As MSXML2.IXMLDOMNodeList Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section") If Not secNodes Is Nothing Then Dim secNode As MSXML2.IXMLDOMNode Set secNode = secNodes(0) Dim sectionID As String For Each secNode In secNodes sectionID = GetAttributeValueFromNode(secNode, "ID") ' Load the XML for the Pages for the Section requested. Dim pagesXml As String oneNote.GetHierarchy sectionID, hsPages, pagesXml ', xs2010 Dim pagesDoc As MSXML2.DOMDocument60 Set pagesDoc = New MSXML2.DOMDocument60 pagesDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If pagesDoc.LoadXML(pagesXml) Then Dim pageNodes As MSXML2.IXMLDOMNodeList Set pageNodes = pagesDoc.DocumentElement.SelectNodes("//one:Page") If Not pageNodes Is Nothing Then Dim pageNode As MSXML2.IXMLDOMNode Set pageNode = pageNodes(0) Dim pageName As String Dim pageID As String For Each pageNode In pageNodes On Error Resume Next pageName = GetAttributeValueFromNode(pageNode, "name") pageID = GetAttributeValueFromNode(pageNode, "ID") ' Creating folder path for output for section Dim sectionName As String sectionName = GetAttributeValueFromNode(secNode, "name") Dim sectionPath As String sectionPath = sectionName & "\" ' Get the user's specified output folder. Dim outputFolder As String outputFolder = FILE_PATH ' Create a file name using the page's name. Dim fileName As String fileName = pageName & ".docx" ' Combine the two values into a single ' Variable so it's easier to use twice. Dim publishContentTo As String publishContentTo = outputFolder & sectionPath & fileName publishContentTo = Replace(publishContentTo, "/", "_") ' Publish the page content to a PDF file ' in the user specified folder. ' The last parameter is not necessary unless you use the ' pfEmf enum. ' Note this method will fail if the file already exists ' at the location specified. On Error GoTo 0 oneNote.Publish pageID, publishContentTo, pfWord On Error Resume Next Next Else MsgBox "OneNote 2016 Page nodes not found." End If Else MsgBox "OneNote 2016 Pages XML data failed to load." End If Next Else MsgBox "OneNote 2016 Section nodes not found." End If Else MsgBox "OneNote 2016 Section XML data failed to load." End If Else MsgBox "OneNote 2016 XML data failed to load." End If End Sub
The "oneNote.Publish pageID, publishContentTo, pfWord
" statement fails as follows:
Any clue?
Hi sba923,
Which error you got? I made a test with your code, and it worked correctly at most conditions. I got error "automation error" when the file exist or the file name you want to export contains invalid characters like ":".
I suggest you check which page caused this error, and check the file path and file name. You could try to export the page manually to with the same file name to check whether it could be export to word correctly.
Here is the test code:
Const FILE_PATH = "C:\Users\Administrator\Desktop\" 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 ' Use the MSXML Library to parse the XML. Dim doc As MSXML2.DOMDocument60 Set doc = New MSXML2.DOMDocument60 doc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If doc.LoadXML(notebookXml) Then Set GetFirstOneNoteNotebookNodes = doc.DocumentElement.SelectNodes("//one:Notebook") Else Set GetFirstOneNoteNotebookNodes = Nothing End If End Function 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 Sub PublishFirstPageOfFirstSectionOfFirstNotebookToWord() ' Connect to OneNote 2016. 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 ', xs2010 Dim secDoc As MSXML2.DOMDocument60 Set secDoc = New MSXML2.DOMDocument60 secDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If secDoc.LoadXML(sectionsXml) Then Dim secNodes As MSXML2.IXMLDOMNodeList Set secNodes = secDoc.DocumentElement.SelectNodes("//one:Section") If Not secNodes Is Nothing Then Dim secNode As MSXML2.IXMLDOMNode Set secNode = secNodes(0) Dim sectionID As String For Each secNode In secNodes sectionID = GetAttributeValueFromNode(secNode, "ID") ' Load the XML for the Pages for the Section requested. Dim pagesXml As String oneNote.GetHierarchy sectionID, hsPages, pagesXml ', xs2010 Dim pagesDoc As MSXML2.DOMDocument60 Set pagesDoc = New MSXML2.DOMDocument60 pagesDoc.setProperty "SelectionNamespaces", "xmlns:one=""http://schemas.microsoft.com/office/onenote/2013/onenote""" If pagesDoc.LoadXML(pagesXml) Then Dim pageNodes As MSXML2.IXMLDOMNodeList Set pageNodes = pagesDoc.DocumentElement.SelectNodes("//one:Page") If Not pageNodes Is Nothing Then Dim pageNode As MSXML2.IXMLDOMNode Set pageNode = pageNodes(0) Dim pageName As String Dim pageID As String For Each pageNode In pageNodes On Error Resume Next pageName = GetAttributeValueFromNode(pageNode, "name") pageID = GetAttributeValueFromNode(pageNode, "ID") ' Creating folder path for output for section Dim sectionName As String sectionName = GetAttributeValueFromNode(secNode, "name") Dim sectionPath As String sectionPath = sectionName & "\" ' Get the user's specified output folder. Dim outputFolder As String outputFolder = FILE_PATH ' Create a file name using the page's name. Dim fileName As String fileName = pageName & ".docx" ' Combine the two values into a single ' Variable so it's easier to use twice. Dim publishContentTo As String publishContentTo = outputFolder & sectionPath & fileName publishContentTo = Replace(publishContentTo, "/", "_") ' Publish the page content to a PDF file ' in the user specified folder. ' The last parameter is not necessary unless you use the ' pfEmf enum. ' Note this method will fail if the file already exists ' at the location specified. On Error GoTo 0 If Dir(publishContentTo) = "" Then oneNote.Publish pageID, publishContentTo, pfWord End If On Error Resume Next Next Else MsgBox "OneNote 2016 Page nodes not found." End If Else MsgBox "OneNote 2016 Pages XML data failed to load." End If Next Else MsgBox "OneNote 2016 Section nodes not found." End If Else MsgBox "OneNote 2016 Section XML data failed to load." End If Else MsgBox "OneNote 2016 XML data failed to load." End If End SubBest Regards,
Edward
这篇关于OneNote 2016:无法使Publish方法起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!