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

C# - OneNote 2016 - Create New Page

$
0
0

Hi friends,

 

I am testing a onenote C# program but i have come into difficulties

 

It keeps giving me an error

 

And not creating a new page :(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Office.Interop.OneNote;


namespace ConsolTest
{
    class Program
    {
        static Microsoft.Office.Interop.OneNote.Application onenoteApp = new Microsoft.Office.Interop.OneNote.Application();
        static XNamespace ns = null;

        static void Main(string[] args)
        {
            GetNamespace();
            string notebookId = GetObjectId(null, HierarchyScope.hsNotebooks, "TestSpace");
            string sectionId = GetObjectId(notebookId, HierarchyScope.hsSections, "WS1");
            string pageId = CreatePage(sectionId, "Hello");
        }

        static void GetNamespace()
        {
            string xml;
            onenoteApp.GetHierarchy(null, HierarchyScope.hsNotebooks, out xml);

            var doc = XDocument.Parse(xml);
            ns = doc.Root.Name.Namespace;
        }

        static string GetObjectId(string parentId, HierarchyScope scope, string objectName)
        {
            string xml;
            onenoteApp.GetHierarchy(parentId, scope, out xml);

            var doc = XDocument.Parse(xml);
            var nodeName = "";

            switch (scope)
            {
                case (HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
                case (HierarchyScope.hsPages): nodeName = "Page"; break;
                case (HierarchyScope.hsSections): nodeName = "Section"; break;
                default:
                    return null;
            }

            var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();

            return node.Attribute("ID").Value;
        }

        static string CreatePage(string sectionId, string pageName)
        {
            // Create the new page
            string pageId;
            onenoteApp.CreateNewPage(sectionId, out pageId, NewPageStyle.npsBlankPageWithTitle);

            // Get the title and set it to our page name
            string xml;
            onenoteApp.GetPageContent(pageId, out xml, PageInfo.piAll);
            var doc = XDocument.Parse(xml);
            var title = doc.Descendants(ns + "T").First();
            title.Value = pageName;

            // Update the page
            onenoteApp.UpdatePageContent(doc.ToString());

            return pageId;
        }
    }
}

and the error

 

Unhandled Exception: System.TypeInitializationException: The type initializer for 'ConsolTest.Program' threw an exception. ---> System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID }

 

failed due to the following error: 80080005 Server execution failed

 

 

 (Exception from HRESULT: 0x80080005

 

 

 

(CO_E_SERVER_EXEC_FAILURE)).

   at ConsolTest.Program..cctor() in C:\Users\Dan\Documents\Visual Studio 2017\Projects\ConsolTest\ConsolTest\Program.cs:line 14

   --- End of inner exception stack trace ---

   at ConsolTest.Program.Main(String[] args)

Press any key to continue . . .

Line 14

static Microsoft.Office.Interop.OneNote.Application onenoteApp = new Microsoft.Office.Interop.OneNote.Application

please can some one offer some advice.

How can i add a new page to a notebook using c#?

is there some working code availible for me to test and learn from.

All the information i found on the internet is old and outdated.

I want to make a C# program for desktop OneNote  -  not cloud based

thank you for your help

 


Cheers Dan :)



Viewing all articles
Browse latest Browse all 2257

Latest Images

Trending Articles



Latest Images

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