如何使用 Powershell 搜索 OneNote 文件中的文本
How to search text in OneNote file with Powershell
我正在尝试使用 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")
最后一个参数应该是一个整数,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 打开 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")
最后一个参数应该是一个整数,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
}
请注意,某些部分可以有部分组,在这种情况下,您将不得不使用一些递归。