2
votes

I'd like to export text from my application to MS OneNote and support Office versions: 2007, 2010, 2013 and 2016. I'm using the Microsoft.Office.Interop.OneNote.dll Version:12.0.0 as it's supplied with all Office versions.

The problem is when I try to create a new OneNote page on Office 2013 using "CreateNewPage" I'm getting the exception:

hrSectionReadOnly 0x8004200b

  • The section is read-only.

I can see an attribute readOnly="true" I've tried to change it and it's not helping. Still getting the same exception.

If I reference Microsoft.Office.Interop.OneNote.dll Version:15.0.0 then everything works fine.

Any ideas why it's read only and how can I overcome the problem?

Here is code example:

string strPath;
string sectionId;
string xml;
// points directly on OneNote's Unfiled section
_app.GetSpecialLocation(SpecialLocation.slUnfiledNotesSection, out strPath);

_app.OpenHierarchy(strPath, "", out sectionId, CreateFileType.cftSection);
_app.CreateNewPage(sectionId, out _sectionId, NewPageStyle.npsDefault);

var strImportXml = @"<?xml version='1.0' ?> 
    <one:Page xmlns:one='" + _oneNoteNamespace + "' ID='" + _sectionId + @"'>
        <one:Title>
               <one:OE>
                      <one:T>
                             <![CDATA[ 
                             ]]> 
                      </one:T>
               </one:OE>
        </one:Title>
        <one:Outline>
               <one:Meta name='Rumba' content='" + new Random().Next() + @"' /> 
                      <one:OEChildren>
                             <one:HTMLBlock>
                                    <one:Data>
                                           <![CDATA[My sample page]]> 
                                    </one:Data>
                              </one:HTMLBlock>
                      </one:OEChildren>
        </one:Outline>
    </one:Page>";

_app.UpdatePageContent(strImportXml, DateTime.MinValue);
_app.NavigateTo(_sectionId, String.Empty, true);
1

1 Answers

0
votes
 private void ExportDataSetToOther(System.Data.DataTable dataTable, bool isActive)
 {
     string path = string.Empty;
     var lines = new List<string>();
     if (isActive )
     {
         path = savingFileName.Remove(savingFileName.LastIndexOf('\\') + 1);
         savingFileName = path + transactionName + DetailsOrSummary + (page + 1) +"."+ extention;// extention means .txt,.xlx,.one,.xlxs etc
         isActive = false;
         string[] columnNames = dataTable.Columns.Cast<DataColumn>().
                                     Select(column => column.ColumnName).
                                     ToArray();

         var header = string.Join(",", columnNames);
         lines.Add(header);

         var valueLines = dataTable.AsEnumerable()
                            .Select(row => string.Join(",", row.ItemArray));
         lines.AddRange(valueLines);

         File.WriteAllLines(savingFileName, lines);
     }
     else
     {
        var valueLines1 = dataTable.AsEnumerable()
                           .Select(row => string.Join(",", row.ItemArray));
        lines.AddRange(valueLines1);

        File.AppendAllLines(savingFileName, lines);

     }

 }

check the file is active or not if not active you can do with else part so the current data will append with exiting values