OneNote can export its notebooks, sections and pages as a document files. But it doesn't support as
- exporting different pages to separate document files
- exporting all pages under a notebook or a section to separate document files
And due to limited format support, exporting data in unsupported format, an exported file should be converted.
Any cases, an user may need support with programming to make them automatic and efficient.
- Get hierarchy structure
- Get ID and output them as CSV file
- Export OneNote page and convert to Markdown
- Export from OneNote
Different editions of PowerShell
|Desktop||Ver 5.1 and below||run on .NET Framework|
|Core||Ver 6.0 and later||run on .NET Core|
Different editions of OneNote
- Microsoft Store
- Executable install file.
Scripts in this post support OneNote in Office family distributed as install file. Others are not supported.
Get hierarchy structure
OneNote data is composed of a combination of folders and files, and each page is processed as a data in a file.*4 This hierarchy structure can be caught as XML with COM object. To operate OneNote data with program is reference and manipulation of this XML.
$OneNote = New-Object -ComObject OneNote.Application Add-Type -assembly Microsoft.Office.Interop.OneNote $OneNoteID = "" [xml]$Hierarchy = "" $OneNote.GetHierarchy( $OneNoteID, [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy, [Microsoft.Office.InterOp.OneNote.XMLSchema]::xsCurrent)
OneNote data from note, section to page has ID. This ID is described later. Specifying data ID in the parameter $OneNoteID, hierarchy structure under the specified XML node is caught, which means note, section and page in OneNote data can be accessed by specifying ID.
Setting Null as ID means specifying root node, entire hierarchy structure of all notebooks are caught. Tne next code (onenote-printall.ps1) outputs these infromation.
Get ID and output them as CSV file
Let say trying program to bulk processing to all pages under certain notebook or section, or set of pages regardless of hierarchy. Identifying ID of processed data in advance makes programming work and its logic simple and efficient.
"depth" in the code and CSV file means hierarchy structure. In case of capturing from root node, each number corresponds following nodes
Precaution: convert to Markdown
Export feature of OneNote supports limited document format. To export unsupported format as Markdown, once exporting supported document format and convert it to unsupported format later. Example, exporting in Word format (docx file) and convert it to Markdown with Pandoc*6.
Precaution: run by common user→run as Administrator
- Run OneNote in advance of running script, especially calling Publish method*7
- Run Windown Terminal, PowerShell, script and OneNote by common user
Even if operated by a single user account, for security reasons, system may be segregated and executed with different system user account. To ensure execution by a single common account, "run as Administrator" is one simple solution.
Not running OneNote in advance or inconsistency of execution user causes next error. Probably, it is almost impossible to reach root cause of error only with this message.
The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Export from OneNote
Call Publish method with specifying next information, OneNote data specified with ID is exported.
- ID of exported data
- absolute path of exported file
- document format of exported file
To export a single page of OneNote as a single file regardless of exporting single notebook or section, call Publish method with ID of page per a page and repeat it required times.
To export series of pages in a single notebook or section, call Publish method with ID of notebook or section.
CSV file output in advance can work as IT catalog.
The next code (onenote-exportmakecsv.ps1) export a single page as a single file, and convert it to Markdown file. Attached images show an outcome that single OneNote page is exported to Word file, and converted to Markdown.