Quantcast
Channel: General Office Development forum
Viewing all articles
Browse latest Browse all 2257

Export Excel Chart to OneNote

$
0
0

I am stumped on a piece of code that involves Excel and OneNote.  I am not an experienced programmer although I know Excel VBA fairly well.  This is my first time ever posting anything to a community forum so bear with me.  Any input here is appreciated even if it doesn't solve my entire problem.  

I have a chart in Excel that has a drop down control (with a list of 88 items to choose from) so that I can change the chart easily.  Because I usually have to wait about 20 seconds for the macro to run each time I change the item, I would have to wait 30 minutes to view each item in the list.  My goal is to copy the chart for each option into a separate page on OneNote so that I can quickly navigate through each item's chart.  I could do this strictly in Excel, but I would like to utilize OneNote to take notes about each chart and my Excel file is already fairly large.  I need the charts to update about once per week, so I would like to find some way to do this automatically.

I have tried the following:

  • Use Excel VBA to copy the chart and paste it into OneNote. - I couldn't find an option in OneNote's API to paste from the clipboard.
  • Use Excel VBA to export the chart as an image (I was able to do this).  Then insert each image manually into OneNote.  My thought was that I could overwrite the image using Excel VBA and somehow have this refresh in OneNote automatically. - OneNote does not appear to be equipped to do this as images appear to be static when they are brought into OneNote.
  • The option that seems the most promising is to use Excel to export the chart as an image (which I have done) and then use OneNote's API to delete the old image and then insert a new image from where it was saved by Excel.  I haven't even tried deleting the old image because I am stuck on inserting a new image.  I have familiarized myself with OneNote's API and know how to retrieve the IDs and names for the Notebook, Section, and Pages of interest, but I am getting stuck when having to use XML to insert page content.  I think I am close, but I think I need to find some way to import the image as base64Binary so that it can be inserted as Data.

I currently have the following code (I am not showing the code for navigating to the correct section because I have figured that out).  I would add code to loop through each item.  I need this to work from Excel VBA (I have seen solutions in C# but they don't work with VBA).  The Object libraries that I have added are Microsoft Office 15.0, Microsoft Excel 15.0, Microsoft OneNote 15.0, Microsoft XML v6.0, System.Drawing.dll, System, and mscorlib.dll.

                                                                                                                                                          

'This first section is just to come up with the XML document for the pages and seems to work fine.  The sectionID comes from previous code.

               ' Load the XML for the Pages for the Section requested.
                Dim pagesXml As String
                oneNote.GetHierarchy sectionID, hsPages, pagesXml, xs2013

Dim pagesDoc As MSXML2.DOMDocument60
                Set pagesDoc = New MSXML2.DOMDocument60

                pagesDoc.setProperty "SelectionLanguage", "XPath"
                pagesDoc.setProperty "SelectionNamespaces", "xmlns:one='http://schemas.microsoft.com/office/onenote/2013/onenote'"

                ' Load Page's XML into a MSXML2.DOMDocument60 object.
                If pagesDoc.LoadXML(pagesXml) Then
                    Dim pagesNodes As MSXML2.IXMLDOMNodeList
                    Set pagesNodes = pagesDoc.DocumentElement.SelectNodes("//one:Page")

                    If Not pagesNodes Is Nothing Then
                        Dim pageNode As MSXML2.IXMLDOMNode
                        For mycount = 0 To pagesNodes.Length - 1
                            Set pageNode = pagesNodes(mycount)
                            If pageNode.Attributes.getNamedItem("name").Text = "I Will Have Additional Code for the Name" Then
                                Exit For
                            End If
                        Next mycount
                        If pageNode Is Nothing Then
                            MsgBox "Error finding page."
                            Exit Sub
                        End If

                        Dim pageName As String
                        pageName = pageNode.Attributes.getNamedItem("name").Text
                        Dim pageID As String
                        pageID = pageNode.Attributes.getNamedItem("ID").Text

'This code just finds the name of the item from the drop down and associated list        

Dim ChartNum As Integer
Dim ChartName As String
ChartNum = Sheets("ChartOutput").DropDowns("Drop Down 4")
ChartName = Sheets("Options").Cells(ChartNum + 1, 2)

'This is the main point where I am having a problem.  This is meant to read the image and convert to Base64Binary.  It is giving an error when trying to save the image string.  I am looking for a way to do this in VBA because most of this code came from C# or VB.Net.

            Dim sd As Bitmap
            sd = "C:\Users\MyUserName\SkyDrive\Charts\" & ChartName & ".jpg"
            Dim imgStream As MemoryStream
            imgStream = New MemoryStream
            sd.Save imgStream, Imaging.ImageFormat.jpg

            imgStream.Close
           Dim byteArray As Byte
            byteArray = imgStream.ToArray()
            imgStream.Dispose

            'Convert the byte[] to base64 string for use to upload.
            Dim final As String
            final = Convert.ToBase64String(byteArray)

'I actually think this would work if I could get the image figured out, but I am not sure.  It errors on the last line when trying to upload page content.

                        Dim newElement As MSXML2.IXMLDOMElement
                        Dim newNode As MSXML2.IXMLDOMNode

                        ' Create Outline node.
                        Set newElement = pagesDoc.createElement("one:Outline")
                        Set newNode = pageNode.appendChild(newElement)
                        ' Create OEChildren.
                        Set newElement = pagesDoc.createElement("one:OEChildren")
                        Set newNode = newNode.appendChild(newElement)
                        ' Create OE.
                        Set newElement = pagesDoc.createElement("one:OE")
                        Set newNode = newNode.appendChild(newElement)
                        ' Create Image.
                        Set newElement = pagesDoc.createElement("one:Image")
                        newElement.setAttribute "isPrintOut", "True"
                        newElement.setAttribute "ImageFormat", "jpg"
                        Set newNode = newNode.appendChild(newElement)
                        Set newElement = pagesDoc.createElement("one:File")
                        newElement.setAttribute "Path", "C:\Users\MyUserName\SkyDrive\Charts\" & ChartName & ".jpg"
                        Set newNode = newNode.appendChild(newElement)
                        Set newElement = pagesDoc.createElement("one:Data")
                        Set newNode = newNode.appendChild(newElement)

                        ' Add the image for the Page's content.
                        Dim cd As MSXML2.IXMLDOMCDATASection
                        Set cd = pagesDoc.createCDATASection(final)
                        newNode.appendChild cd
                        'Update OneNote with the new content.
                        oneNote.UpdatePageContent pagesDoc.XML

Thank you in advance!


Viewing all articles
Browse latest Browse all 2257

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>