excelvbaonenote

Rename a page in onenote with VBA, unable to apply changes, cannot save xml


I would like to copy a page from my template section and rename it. For now, I merge sections because I only have one page in the template section. So I need to rename the page in the new section but I m not able to make it works! It changes the name in the code but not able to get it applied to the document.

Here the code that I use with declaration followed the part just after merging those sections:

'Microsoft OneNote 15.0 Object Library
'Microsoft XML, 5.0 (comes from 6.0 originally but a lot of code not working on 6.0)

Private Sub CommandButton5_Click()

 Dim onenote As onenote.Application
 Set onenote = New onenote.Application
 Dim MypageName as string
 MypageName = "page title"

' Get the XML that represents the OneNote sections
    Dim oneNoteSectionsXml As String

    onenote.GetHierarchy "", 4, oneNoteSectionsXml

    Dim doc As MSXML2.DOMDocument
    Set doc = New MSXML2.DOMDocument

    If doc.LoadXML(oneNoteSectionsXml) Then
        Dim nodeNoteBooks As MSXML2.IXMLDOMNodeList
        Dim nodeSections As MSXML2.IXMLDOMNodeList
        Dim nodepages As MSXML2.IXMLDOMNodeList
        
        
        Set nodeNoteBooks = doc.DocumentElement.SelectNodes("//one:Notebook")
        Set nodeSections = doc.DocumentElement.SelectNodes("//one:Section")
        Set nodepages = doc.DocumentElement.SelectNodes("//one:Page")
         
        
        Dim nodeNoteBook As MSXML2.IXMLDOMNode
        Dim nodeSection As MSXML2.IXMLDOMNode
        Dim nodePage As MSXML2.IXMLDOMNode
`For Each nodePage In nodepages
   If nodePage.Attributes.getNamedItem("name").Text = nodeSections.item(2).ChildNodes(0).Attributes.getNamedItem("name").Text Then nodePage.Attributes.getNamedItem("name").Text = MypageName
       
       onenote.UpdateHierarchy doc.XML
       doc.Save doc.XML
 next

end if

end sub      `

UpdateHierarchy gives no error but no results and doc.save gives an error (wrong parameters).

Is it the right way to do it ?

I tried a lot of solutions all around but nothing works. The original code was copied and it was on microsoft xml 6.0 but a lot of error came out so I tried to adapt it for 5.0 and its working, exept for this part.

Thanks for help!


Solution

  • I finally get it working. I think that I must reload another xml with page content.

    Here the entire code if it could help someone! It merge the template section (ongletNameOrigine) (index of the start section) with the destination section (ongletNameDestination) (index of destination section), then it loop through the destination section to find the template page by its name TemplatePageName (I only have one page in template section) and rename it with the right name (MypageName). Its not optimized but you can use it to get started!

    Private Sub CommandButton5_Click()
    
    'Make sure to add the references to:
    'Microsoft OneNote 15.0 Object Library
    'Microsoft XML, 5.0
    
    'ref to: https://github.com/pierrms/Alphabetize-OneNote-Sections-VBA/blob/master/OneNote_Sort_VBA.bas
    
    Unload Me
        Dim notebookSortName As String
        Dim ongletNameOrigine As Integer
        Dim ongletNameDestination As Integer
        Dim MypageName As String
        Dim contrat As String
        Dim item As String
        Dim Mac As String
        Dim TemplatePageName As String
        Dim templatePageID As String
        
    'Templates = 2, Projets en cours =3, Projets en appro =4, Projets terminés =5
    
    'set section value
        contrat = Cells(ActiveCell.Row, 1).Value
        item = Cells(ActiveCell.Row, 2).Value
        notebookSortName = "11A-Famille Expert C1SCAN"
        ongletNameOrigine = 2: ongletNameDestination = 3
        Mac = Application.InputBox("numero machine (911-912-913-871)")
        MypageName = "PL-" + contrat + "-" + item + " (" + Mac + ")"
        TemplatePageName = "PL-Contrat-Item (871,911,912,913)"
    
    
        Dim onenote As onenote.Application
        Set onenote = New onenote.Application
    
    ' Get the XML that represents the OneNote sections
        Dim oneNoteSectionsXml As String
        
        onenote.GetHierarchy "", 4, oneNoteSectionsXml
    
    
        Dim doc As MSXML2.DOMDocument
        Set doc = New MSXML2.DOMDocument
    
        If doc.LoadXML(oneNoteSectionsXml) Then
            Dim nodeNoteBooks As MSXML2.IXMLDOMNodeList
            Dim nodeSections As MSXML2.IXMLDOMNodeList
            Dim nodepages As MSXML2.IXMLDOMNodeList
            
            Set nodeNoteBooks = doc.DocumentElement.SelectNodes("//one:Notebook")
            Set nodeSections = doc.DocumentElement.SelectNodes("//one:Section")
            Set nodepages = doc.DocumentElement.SelectNodes("//one:Page")
          
            Dim nodeNoteBook As MSXML2.IXMLDOMNode
            Dim nodeSection As MSXML2.IXMLDOMNode
            Dim nodepage As MSXML2.IXMLDOMNode
            Dim sectionIDXML As String
            Dim section2IDXML As String
                   
            Dim pageXML As String
            Dim updatedPageXML As String
            Dim pageID As String
            
          'VÉRIFICATION DES ONGLETS
              If nodeSections.item(ongletNameDestination).Attributes.getNamedItem("name").Text <> "Projets en cours" Then MsgBox "onglet de destination erroné, supposé être Projets en cours mais  " & nodeSections.item(ongletNameDestination).Attributes.getNamedItem("name").Text: Exit Sub
              If nodeSections.item(ongletNameOrigine).Attributes.getNamedItem("name").Text <> "Templates" Then MsgBox "onglet de destination erroné, supposé être Templates mais  " & nodeSections.item(ongletNameOrigine).Attributes.getNamedItem("name").Text: Exit Sub
           'vérif template page name
              If nodeSections.item(ongletNameOrigine).ChildNodes(0).Attributes.getNamedItem("name").Text <> TemplatePageName Then MsgBox "nom de page de template incorrect, devrait être  " + TemplatePageName + "  mais est: " + nodeSections.item(ongletNameOrigine).ChildNodes(0).Attributes.getNamedItem("name").Text: Exit Sub
           
           'check if good notebook
            If nodeSections.item(ongletNameDestination).ParentNode.Attributes.getNamedItem("name").Text <> notebookSortName Then MsgBox "mauvais notebook, supposé être  " & notebookSortName & "  mais " & nodeSections.item(ongletNameDestination).ParentNode.Attributes.getNamedItem("name").Text: Exit Sub
            If nodeSections.item(ongletNameOrigine).ParentNode.Attributes.getNamedItem("name").Text <> notebookSortName Then MsgBox "mauvais notebook, supposé être  " & notebookSortName & "  mais " & nodeSections.item(ongletNameOrigine).ParentNode.Attributes.getNamedItem("name").Text: Exit Sub
           
      
           sectionIDXML = nodeSections.item(ongletNameOrigine).Attributes.getNamedItem("ID").Text
           section2IDXML = nodeSections.item(ongletNameDestination).Attributes.getNamedItem("ID").Text
        
           onenote.MergeSections sectionIDXML, section2IDXML
        
          onenote.GetHierarchy "", 4, oneNoteSectionsXml
          Set doc = New MSXML2.DOMDocument
          If doc.LoadXML(oneNoteSectionsXml) Then Set nodeSections = doc.DocumentElement.SelectNodes("//one:Section")
    
         'onglet destination, rename template page
          If nodeSections.item(ongletNameDestination).ChildNodes.Length > 0 Then
             For x = 1 To nodeSections.item(ongletNameDestination).ChildNodes.Length
                  If nodeSections.item(ongletNameDestination).ChildNodes(x).Attributes.getNamedItem("name").Text = TemplatePageName Then
                 
                         pageID = nodeSections.item(ongletNameDestination).ChildNodes(x).Attributes.getNamedItem("ID").Text
                         onenote.GetPageContent pageID, pageXML, 0
                
                         doc.LoadXML pageXML
                         doc.SelectSingleNode("//one:Page/one:Title/one:OE/one:T").Text = MypageName
                         updatedPageXML = doc.XML
                         'objShell.Popup updatedPageXML
                         onenote.UpdatePageContent updatedPageXML
                         onenote.UpdateHierarchy updatedPageXML
                         Exit For
                   End If
             Next
          End If
    
                   
     End If
    
    End Sub