Programmatically inserting image into Onenote page using VBA from Access 2010 RRS feed

  • Question

  • Hello,

    I am writing in VBA from Access 2010 to take PDF files and create a page for each, so building a searchable archive.  Creating pages is fine but when I try to add the PDF as an image or inserted page and update the XML I get a syntax error.

    The code segments I am am trying are:

                Set newElement = doc.createElement("one:Image")
                newElement.setAttribute "File", "c:\_ENtmp\" & sTitle
                newElement.setAttribute "Format", "pdf"
                newElement.setAttribute "isPrintOut", "True"
                Set newNode = newNode.appendChild(newElement)
                Debug.Print newElement.XML


                Set newElement = doc.createElement("one:InsertedFile")
                newElement.setAttribute "pathSource", "c:\_ENtmp\" & sTitle
                newElement.setAttribute "preferredName", sTitle
                Set newNode = newNode.appendChild(newElement)
                Debug.Print newElement.XML

    and in both case the code fails when I execute:

                oneNote.UpdatePageContent doc.XML, , , True  

    by returning a runtime error 80042001 - Invalid syntax. The XML looks ok and I don't know where to go next. I am using OneNote v14 and XML v6 libraries added under Tools, References

    Any ideas appreciated.

    Thanks, Nick

    Saturday, March 29, 2014 11:40 AM

All replies

  • Hello Nick,

    According to the error code - 0x80042001 you specified an invalid XML markup. Also I'd try to specify the DateTime value. Though it is optional parameter, you'll get an error if you don't specify it explicitly. Please refer to the UpdatePageContent doesn't work in onenote addin forum thread for more information.

    Saturday, March 29, 2014 3:47 PM
  • Hi Nick,

    According to Error Codes (OneNote 2010) which Eugene mentioned, it seems the XML is invalid.

    Please look at the article below:

    Application Interface (OneNote 2010) - Page Content Methods

    It introduces how to use UpdatePageContent Method and give us a C# sample.

    You should check whether the XML is similar with below:

    strImportXML = "<?xml version=\"1.0\"?>" +
                "<one:Page xmlns:one=\"\" 
                ID=\"{3428B7BB-EF39-4B9C-A167-3FAE20630C37}{1}{B0}\">" +
                "    <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=\"en-US\">" +
                "        <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>" +
                "            </one:OE>" +
                "        </one:OEChildren>" +
                "    </one:Outline>" +

    Hope this helps.

    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.

    Monday, March 31, 2014 10:08 AM
  • Hello and thank for the replies,

    Unfortunately I am no further forward. I added the DateTime value but no change, and if I just create the page with no image all is fine so the overall XML structure works.  I have found some examples but they all seem to store the image - typically jpeg - as a binary variable.

    When I add the code to insert an image (isprintout = true) or inserted file, I need to understand which attributes I need to specify and ideally have an example that actually works.  I am working from the latest OneNote scheme but it's not really much help.

    Just in case I am missing something obvious, what I actually want to do is to programatically (Access 2010 with VBA) create a OneNote 2010 page which contains a file (typically PDF) that I can search. I can scan the directory of files and create the page with title and text but just can't add the file as either an image with isprintout = true, or as insertedfile.

    As I have 300 to add I could have probably created them by hand, but now I really want to crack the code and share it.

    Thanks, Nick

    Monday, March 31, 2014 1:55 PM
  • Dear Nick

    do you have cracked the code already?

    i have the same problem and i do not know how i can fix it.

    i think that isn´t possible to insert images or pdf`s into a existing onenote book

    Thanks, Torben

    Wednesday, August 17, 2016 11:53 AM
  • I am so happy I figured it out before I gave up on OneNote objects.

    so here is the deal.

    oneNote.UpdatePageContent supports limited amount of image types.

    so if you set format = bmp, you get a 80042001

    if you set format = pdf, you get a 80042001

    if you leave the format empty, it loads image as png, but oneNote has a bug in reading png, if the background color changes, it fails to read the text in different background.

    so, to make it work

    set format = jpg and load a jpg file to the xml, bingo!

    Monday, May 8, 2017 6:57 PM