ToB企服应用市场:ToB评测及商务社交产业平台

标题: 用 (Excel) VBA 读取 OneNote! [打印本页]

作者: 冬雨财经    时间: 2024-10-22 08:11
标题: 用 (Excel) VBA 读取 OneNote!
本文记录,用 VBA 读取 OneNote 的方法!小白贡献,语失莫怪!

问题背景:

我在 OneNote 里有上百篇笔记,可 OneNote 自己,却无法导出全部的标题。于是我费尽心机,想要读取 OneNote 的文件,来获取标题和日志信息。尝试了各种方案,都没能读出 OneNote 的数据。最后过了好久好久,才找到了这种,基于 Excel VBA 的解决方式!那么接下来,我们就来看下它的代码!在片尾我也会讲下,尝试过的其他方式,大概哪位路过的大神,能解决掉其中的问题!

代码如下:

  1. 'Add the following references (adjust to your office version):
  2. '- Microsoft OneNote 12.0 Object Library
  3. '- Microsoft OneNote 15.0 Object Library
  4. '- Microsoft XML, v3.0
  5. Sub ListOneNotePages()
  6.     'OneNote will be started, if it's not running.
  7.     Dim OneNote As OneNote.Application
  8.     Set OneNote = New OneNote.Application
  9.     'Get the XML that represents the OneNote pages
  10.     Dim oneNotePagesXml As String
  11.     'Use note hierarchy, to get all page id.
  12.     OneNote.GetHierarchy "", OneNote12.HierarchyScope.hsPages, oneNotePagesXml
  13.     'Use the MSXML Library to parse the XML.
  14.     Dim doc As MSXML2.DOMDocument
  15.     Set doc = New MSXML2.DOMDocument
  16.     If doc.LoadXML(oneNotePagesXml) Then
  17.         'Find all the Page nodes in the one namespace.
  18.         Dim nodes As MSXML2.IXMLDOMNodeList
  19.         Set nodes = doc.DocumentElement.SelectNodes("//one:Page")
  20.         Dim node As MSXML2.IXMLDOMNode
  21.         Dim pageName As String
  22.         Dim sectionName As String
  23.         Dim pageContent As String
  24.         Dim temp As String
  25.         'Go through each page, and print out page content
  26.         For Each node In nodes
  27.             pageName = node.Attributes.getNamedItem("name").Text
  28.             Debug.Print "Page name: "; vbCrLf & " " & pageName
  29.             Call OneNote.GetPageContent(GetAttributeValueFromNode(node, "ID"), pageContent, piBasic)
  30.             Debug.Print " content: " & pageContent
  31.         Next
  32.     Else
  33.         MsgBox "OneNote 2010 XML Data failed to load."
  34.     End If
  35. End Sub
  36. Private Function GetAttributeValueFromNode(node As MSXML2.IXMLDOMNode, attributeName As String) As String
  37.     If node.Attributes.getNamedItem(attributeName) Is Nothing Then
  38.         GetAttributeValueFromNode = "Not found."
  39.     Else
  40.         GetAttributeValueFromNode = node.Attributes.getNamedItem(attributeName).Text
  41.     End If
  42. End Function
复制代码
Remote Procedure Call Failed 报错:



OneNote 无法启动问题:



其他解决方案:

其实在 Excel VBA 的解决方案之前,还尝试了很多很多其他的方案,那也大抵的说一下吧。

一些可用的 Python 代码:

至于下列代码里,为什么都是 OneNote.Application.12 我也不知道!但如果你的欠好使,也可以改成 OneNote.Application.14 试试!但在我的电脑里,只有 OneNote.Application.12 好使!
  1. # 输出页面结构信息
  2. import win32com.client
  3. oneapp = win32com.client.Dispatch('OneNote.Application.12')
  4. result = oneapp.GetHierarchy("",4)
  5. print(result)
  6. ## 输出所有页面的标题
  7. import win32com.client
  8. oneapp = win32com.client.Dispatch('OneNote.Application.12')
  9. result = oneapp.GetHierarchy("",4)
  10. from bs4 import BeautifulSoup
  11. soup = BeautifulSoup(result, 'xml')
  12. for tag in soup.find_all('one:Page'):
  13.     print(tag.attrs['name'])
  14. # 试图输出任何一个页面的内容 (报错暂时无解)
  15. # >>> Error occurred: (-2147023170, 'The remote procedure call failed)
  16. import win32com.client
  17. onapp = win32com.client.Dispatch('OneNote.Application.12')
  18. result = onapp.GetHierarchy("",4)
  19. print(result)
  20. pageID = "任何上面输出过的PageID"
  21. content = onapp.GetPageContent(pageID)
  22. print(content)
复制代码
后期的应用

被 VBA 读取出来了,是 OneNote 每个页面的全部内容!信息量很大,都是 XML 形式的!之后的处置惩罚就不再赘述了,怎么处置惩罚就取决于各自的需求了!好的就写到这里了,盼望能有资助,(^_^)b

Reference:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4