Question

How can you create a new section in a OneNote 2010 notebook with c#? According to the API there is no method to do so. But there is a CreateNewPage Method so I wondering if there is something similiar for sections? If not, how can this be achieved except for manipulating the XML files (which is a task i'd like to avoid since I'm not experienced in it)?

Was it helpful?

Solution

Here is code snippet from my add on:

public bool AddNewSection(string SectionTitle, out string newSectionId)
        {
            try
            {
                string CurrParentId;
                string CurrParentName;
                string strPath;
                CurrParentId = FindCurrentlyViewedSectionGroup(out CurrParentName);
                if (string.IsNullOrWhiteSpace(CurrParentId) || string.IsNullOrWhiteSpace(CurrParentName))
                {
                    CurrParentId = FindCurrentlyViewedNotebook(out CurrParentName);
                    if (string.IsNullOrWhiteSpace(CurrParentId) || string.IsNullOrWhiteSpace(CurrParentName))
                    {
                        newSectionId = string.Empty;
                        return false;
                    }
                    strPath = FindCurrentlyViewedItemPath("Notebook");
                }
                else
                    strPath = FindCurrentlyViewedItemPath("SectionGroup");

                if (string.IsNullOrWhiteSpace(strPath))
                {
                    newSectionId = string.Empty;
                    return false;
                }

                SectionTitle = SectionTitle.Replace(':', '\\');
                SectionTitle = SectionTitle.Trim('\\');
                strPath += "\\" + SectionTitle + ".one";
                onApp.OpenHierarchy(strPath, null, out newSectionId, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
                onApp.NavigateTo(newSectionId, "", false);
            }
            catch
            {
                newSectionId = string.Empty;
                return false;
            }
            return true;
        }

Basically what I am doing here is to get the path of currently viewing Section Group or Notebook and then adding new section name to that path and then calling OpenHierarchy method. OpenHierarchy creates a new section with title provided and returns it's id.

Following is where I create a new section and Navigate to it:

onApp.OpenHierarchy(strPath, null, out newSectionId, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);
onApp.NavigateTo(newSectionId, "", false);

So can write something like:

static void CreateNewSectionMeetingsInWorkNotebook()
    {
        String strID;
        OneNote.Application onApplication = new OneNote.Application();
        onApplication.OpenHierarchy("C:\\Documents and Settings\\user\\My Documents\\OneNote Notebooks\\Work\\Meetings.one", 
        System.String.Empty, out strID, OneNote.CreateFileType.cftSection);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top