powershell - 如何使用 Powershell 在 OneNote 文件中搜索文本

标签 powershell onenote

我正在尝试使用 Powershell 打开 OneNote 文件(可能是网络共享上的文件,不一定是我自己的 OneNote 文件)。
我收到一个错误

Exception calling "OpenHierarchy" with "4" argument(s): "Exception from HRESULT: 0x80042018"



在调用 OpenHierarchy 时,我找不到解决此问题的方法。
任何人都可以帮忙吗?这是我的代码:
$OneNote = New-Object -ComObject OneNote.Application

$OneNoteFilePath = "someNetworkPath"
[ref]$xml = ""
[xml]$Hierarchy = ""
$OneNote.OpenHierarchy($OneNoteFilePath, "", $xml, "cftNotebook")
$OneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy)

ForEach($notebook in $Hierarchy.Notebooks.Notebook)
{
    [ref]$PageXML = ''
    $OneNote.GetPageContent($notebook.ID, [ref]$PageXML, [Microsoft.Office.Interop.OneNote.PageInfo]::piAll)

    If ($PageXML | Where-Object { $_ -match "\b$word\b" })
    {
        ## Do something
    }

    ForEach($section in $notebook.Section)
    {
        [ref]$PageXML = ''
        $OneNote.GetPageContent($notebook.ID, [ref]$PageXML, [Microsoft.Office.Interop.OneNote.PageInfo]::piAll)

        If ($PageXML | Where-Object { $_ -match "\b$word\b" })
        {
            ## Do something
        }
    }
}

最佳答案

线路有问题:
$OneNote.OpenHierarchy($OneNoteFilePath, "", $xml, "cftNotebook")
最后一个参数应该是 int,3,而不是字符串“cftNotebook”。

或者您可以使用正确的枚举来代替 3:[Microsoft.Office.Interop.OneNote.CreateFileType]::cftSection
https://docs.microsoft.com/en-us/office/client-developer/onenote/application-interface-onenote
https://docs.microsoft.com/en-us/office/client-developer/onenote/enumerations-onenote-developer-reference#odc_CreateFileType

如果您正在寻找一个工作片段,这里有一些可以帮助您入门的东西:

$OneNote = New-Object -ComObject OneNote.Application

$OneNoteFilePath = "c:\path\to\onenote.one"
[ref]$oneNoteID = ""
[xml]$Hierarchy = ""
$OneNote.OpenHierarchy($OneNoteFilePath, "", $oneNoteID, [Microsoft.Office.Interop.OneNote.CreateFileType]::cftSection)
$OneNote.GetHierarchy($oneNoteID.Value, [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy)

#At this point, check $Heirarchy for what properties it has, it could be Notebooks, or SectionGroup, or Section

#If your $Hierarchy contains sections, you can do this
ForEach($page in $Hierarchy.Section.Page)
{
    [ref]$PageXML = ''
    $OneNote.GetPageContent($page.ID, [ref]$PageXML, [Microsoft.Office.Interop.OneNote.PageInfo]::piAll)
    Write-Output $PageXML.Value
}

请注意,某些部分可以有部分组,在这种情况下,您必须使用一些递归。

关于powershell - 如何使用 Powershell 在 OneNote 文件中搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57397334/

相关文章:

javascript - 在网站上使用 Surface Pro 触控笔

string - 具有根UNC目录的拆分路径

powershell - 检查 AD 对象是否存在;如何避免丑陋的错误消息?

asp.net - 卡在ASP.NET 5/VS2015中的DNX引用问题上

windows - 如何测试Windows Server是否完全更新? (尝试创建更新/重启循环脚本)

javascript - 有没有办法克服 OneNote 客户端和 API 结果之间的变化之间的滞后?

sql-server - 从 Powershell 调用的存储过程不通过 SMO 对象执行

azure - microsoft graph api和Azure v2.0的测试环境

java - OneNote 解析 - 如何获取文档中的文本 Blob?

python - 有没有办法使用 Python 脚本读取 .one(OneNote 文件)?