Home > Blockchain >  powershell onenote page title change
powershell onenote page title change

Time:10-03

i would like to change the title of the onenote page, but i have a error.

Here is my code

$app = New-Object -ComObject OneNote.Application
$struct = [ref]""
$scope = [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsPages
$app.GetHierarchy($null, $scope, $struct)
$struct = [xml]$struct.Value


$notebook = $struct.Notebooks.Notebook | Where-Object { $_.name -match "MeinNotizbuch" }

$OneSection = $notebook | Select-Object -ExpandProperty section | Where-Object { $_.name -match "Vorlage" }
$id = [ref]""

$app.CreateNewPage($OneSection.id,$id)

$OnePage = [ref]""
$app.GetPageContent($id.value, $OnePage)
$OnePage = [xml]$OnePage.value

$frag = $OnePage.CreateDocumentFragment()
# modify the title of onenote page

$frag.InnerXml = @'
<one:Title xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote">
    <one:T>Test Title</one:T>
</one:Title>
<one:Outline xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote">
    
    <one:OEChildren>
        <one:OE>
            <one:T>
                <![CDATA[{0}]]>
            </one:T>
        </one:OE>
    </one:OEChildren>
</one:Outline>
'@ -f "This is a test"

$OnePage.Page.AppendChild($frag)


$app.UpdatePageContent($OnePage.OuterXml)

the error is:

PS C:\Users\Jerome> . "c:\Users\Jerome\Scripts\OneNote\OneNote_NewPage.ps1"

one T --- - http://schemas.microsoft.com/office/onenote/2013/onenote Test Title Ausnahme beim Aufrufen von "UpdatePageContent" mit 1 Argument(en): "Unbekannter Fehler Der Inhalt des Elements '{http://schemas.microsoft.com/office/onenote/2013/onenote}Title' ist gemäß dem Inhaltsmodell des übergeordneten Elements '{http://schemas.microsoft.com/office/onenote/2013/onenote}Page' nicht gültig. Erwartet: {http://schemas.microsoft.com/office/onenote/2013/onenote}Outline, {http://schemas.microsoft.com/office/onenote/2013/onenote}Image, {http://schemas.microsoft.com/office/onenote/2013/onenote}InkDrawing, {http://schemas.microsoft.com/office/onenote/2013/onenote}InsertedFile, {h" In C:\Users\Jerome\Scripts\OneNote\OneNote_NewPage.ps1:43 Zeichen:1

  • $app.UpdatePageContent($OnePage.OuterXml)
  •     CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        FullyQualifiedErrorId : COMException
    
    
    
    

PS C:\Users\Jerome

CodePudding user response:

$app = New-Object -ComObject OneNote.Application
$struct = [ref]""
$scope = [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsPages
$app.GetHierarchy($null, $scope, $struct)
$struct = [xml]$struct.Value


$notebook = $struct.Notebooks.Notebook | Where-Object { $_.name -match "MeinNotizbuch" }


#Eine Ebene
$OneSection = $notebook | Select-Object -ExpandProperty section | Where-Object { $_.name -match "Test Page" }


$id = [ref]""
$app.CreateNewPage($OneSection.id,$id)


[ref]$NewPageXML = ''
$OneNote.GetPageContent($id.Value,[ref]$NewPageXML,[Microsoft.Office.Interop.OneNote.PageInfo]::piAll)

Write-Output $id




$null = [Reflection.Assembly]::LoadWithPartialName('System.Xml.Linq')
$xDoc = [System.Xml.Linq.XDocument]::Parse($NewPageXML.Value)
$title = $xDoc.Descendants() | Where-Object -Property Name -Like -Value '*}T'

if (-not $title) {
  throw 'Fehler: kann Titel der neuen OneNote Seite nicht auslesen'
}
$date = Get-Date -Format 'dd.MM.yyyy HH:mm:ss'
$title.Value = "Meine erste Seite - "   "${date}"
$onenote.UpdatePageContent($xDoc.ToString())
  • Related