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.
This post introduces handling OneNote data with PowerShell, and procedure to export a file to Markdown.
- Precaution
- Get hierarchy structure
- Get ID and output them as CSV file
- Export OneNote page and convert to Markdown
- Export from OneNote
Precaution
Different editions of PowerShell
Now, PowerShell, including provided in Windows Terminal is a part of one function provided by Windows. There are different editions as*1
Desktop | Ver 5.1 and below | run on .NET Framework |
Core | Ver 6.0 and later | run on .NET Core |
Scripts in this post can be run on Desktop, but not on Core. In using PowerShell from Windows Terminal, open new tab with one of either is recommended.
- Windows PowerShell
- Developer PowerShell for VS 2022
Different editions of OneNote
OneNote is also provided in different editions.*2
- Product
- Office family as Microsoft Office 2013
- Windows feature as OneNote for Windows 10
- Distribution
- 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.
Calling Execute GetHierarchy method*5, pointer to XML is set to the parameter $Hierarchy.
$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.
Retrieve all IDs and output them to CSV file with the next code (onenote-makecsv.ps1).
"depth" in the code and CSV file means hierarchy structure. In case of capturing from root node, each number corresponds following nodes
1 | notebook |
2 | section group section |
3~ | page |
Export OneNote page and convert to Markdown
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
Probably, this is the biggest catch in OneNote handling with PowerShell. No sites including Microsoft introduces this fact.
- Run OneNote in advance of running script, especially calling Publish method*7
- Run Windown Terminal, PowerShell, script and OneNote by common user
Publish method of OneNote object depends on export feature of OneNote. When script is executed, especially Publish method is called, OneNote must be running in advance of script.
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.