none
Office 2013: Failed to update OneNote page content in VBA RRS feed

  • Question

  • Hello,

    I was trying to use UpdatePageContent method of OneNote application in Excel 2013 VBA to change the content of a OneNote 2013 page. I copied the C# sample code in this page https://msdn.microsoft.com/en-us/library/office/jj680120.aspx ("in the UpdatePageContent Method" section) and translated to VB. However, the page content was still not changed even after I run the VBA.

    Here is my code: (pageID is the ID of the page that I have successfully obtained)

    Dim strImportXML As String
        strImportXML = "<?xml version=""1.0""?>" & _
                "<one:Page xmlns:one=""http://schemas.microsoft.com/office/onenote/12/2004/onenote"" ID=""" & pageID & """>" & _
                "    <one:PageSettings RTL=""false"" color=""automatic"">" & _
                "        <one:PageSize>" & _
                "            <one:Automatic/>" & _
                "        </one:PageSize>" & _
                "        <one:RuleLines visible=""false""/>" & _
                "    </one:PageSettings>" & _
                "    <one:Title style=""font-family:Calibri; font-size:17.0pt"" lang=""zh-CN"">" & _
                "        <one:OE alignment=""left"">" & _
                "            <one:T>" & _
                "                <![CDATA[My Sample Page]]>" & _
                "            </one:T>" & _
                "        </one:OE>" & _
                "    </one:Title>" & _
                "    <one:Outline >" & _
                "        <one:Position x=""120"" y=""160""/>" & _
                "        <one:Size width=""120"" height=""15""/>" & _
                "        <one:OEChildren>" & _
                "            <one:OE alignment=""left"">" & _
                "                <one:T>" & _
                "                    <![CDATA[Sample Text]]>" & _
                "                </one:T>"
        strImportXML = strImportXML & _
                "            </one:OE>" & _
                "        </one:OEChildren>" & _
                "    </one:Outline>" & _
                "</one:Page>"
        oneNote.UpdatePageContent strImportXML, DateTime.Now, xs2013, True

    Upon running this code, I get a runtime error 80042010. I opened OneNote 2013 manually and the old content of the page still shows.

    I have tried to use various codes online about changing page contents such as adding images (which some users claim to be working, such as this one: https://social.msdn.microsoft.com/Forums/office/en-US/f772b9b2-f0aa-4180-b2ad-c936dbbe2abd/programmatically-inserting-image-into-onenote-page-using-vba-from-access-2010?forum=officegeneral), but they still do not work on my computer.

    I wonder is there any way to fix this problem? Or is it that the old codes for OneNote 2010 no longer work on OneNote 2013?

    Thanks in advance.

    Regards,

    Lyu Liang



    • Edited by array88 Tuesday, March 15, 2016 4:15 AM
    Tuesday, March 15, 2016 2:19 AM

Answers

  • Hi Lyu Liang,

    Based on the schema in the code, you were update the OneNote with old version of page content. To update the page content in OneNote 2013, we can get the original page content and modify the content as we wanted. Then update the page content with UpdatePageContent method.

    In addition, please note the dateExpectedLastModified parameter. 

    The date and time that you think the page you want to update was last modified. If you pass a non-zero value for this parameter, OneNote proceeds with the update only if the value you pass matches the actual date and time the page was last modified. Passing a value for this parameter helps prevent accidentally overwriting edits users made since the last time the page was modified.

    Here is an example for your reference:

    Function getPageContent(pageID As String) Dim app As New OneNote.Application Dim output As String app.getPageContent pageID, output Debug.Print output getPageContent = output End Function Sub updatePage() Dim pageID As String pageID = "{C0B82F3E-443C-07B6-2585-DAA4F3B48F01}{1}{E1946755770495296337861982248692703830668011}" strImportXML = Replace(getPageContent(pageID), "Rose", "Jack") Dim app As New OneNote.Application Dim output As String app.UpdatePageContent strImportXML, , xs2013 End Sub
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 16, 2016 8:31 AM
    Moderator

All replies

  • Hi Lyu Liang,

    Based on the schema in the code, you were update the OneNote with old version of page content. To update the page content in OneNote 2013, we can get the original page content and modify the content as we wanted. Then update the page content with UpdatePageContent method.

    In addition, please note the dateExpectedLastModified parameter. 

    The date and time that you think the page you want to update was last modified. If you pass a non-zero value for this parameter, OneNote proceeds with the update only if the value you pass matches the actual date and time the page was last modified. Passing a value for this parameter helps prevent accidentally overwriting edits users made since the last time the page was modified.

    Here is an example for your reference:

    Function getPageContent(pageID As String) Dim app As New OneNote.Application Dim output As String app.getPageContent pageID, output Debug.Print output getPageContent = output End Function Sub updatePage() Dim pageID As String pageID = "{C0B82F3E-443C-07B6-2585-DAA4F3B48F01}{1}{E1946755770495296337861982248692703830668011}" strImportXML = Replace(getPageContent(pageID), "Rose", "Jack") Dim app As New OneNote.Application Dim output As String app.UpdatePageContent strImportXML, , xs2013 End Sub
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 16, 2016 8:31 AM
    Moderator
  • Thanks! Tried this method and it worked perfectly.
    Thursday, March 17, 2016 5:06 AM