2014-12-04 101 views
8

我見過很多關於此的文章,但它們都不完整或不回答我的問題。使用C#和OneNote Interop,我想簡單地將文本寫入現有的OneNote 2013頁面。目前,我有一個OneNote筆記本,有標題爲"Sample_Section「和一個叫"MyPage"頁。如何使用C#和OneNote編寫OneNote 2013頁面Interop

我需要能夠使用C#代碼來寫文字此頁,但我無法弄清楚如何或發現任何資源我已經查看了網絡上的所有代碼示例,但沒有人回答這個簡單的問題,或者能夠做到這一點,而且許多代碼示例在試圖運行它們時已經過時並且破壞了。 Microsoft代碼示例顯示如何更改部分的名稱,但是我找不到任何代碼將文本寫入Page。沒有簡單的方法可以實現,我可以看到,我花了大量時間來研究此問題並查看不同的例子在線,但沒有人能夠提供幫助。

我已經在OneNoteInterop上看過MSDN文章。我隱約瞭解OneNoteInterop如何通過XML工作,但任何額外的幫助理解,也將不勝感激。最重要的是,我將非常感謝一個代碼示例,演示如何將文本寫入到2013年筆記本頁面。

我一直在使用這個堆棧溢出的答案嘗試: Creating new One Note 2010 page from C#

不過,也有關於這個解決方案,不回答我的問題,兩件事情:

1)顯着的解決方案展示瞭如何創建一個新的頁面,而不是如何寫入文本或如何用任何信息填充頁面。

2)當我嘗試運行被標記爲溶液中的代碼,我得到一個錯誤在下面的行:

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

原因是,的「節點」的值爲空,任何幫助將不勝感激。

回答

8

我在MSDN論壇上提出了同樣的問題,並給出了這個很好的答案。下面是如何使用C#和OneNote互操作書寫入OneNote的一個很好的例子。我希望這可以幫助未來的人們。

static Application onenoteApp = new Application(); 
    static XNamespace ns = null; 

    static void Main(string[] args) 
    { 
     GetNamespace(); 
     string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyNotebook"); 
     string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Sample_Section"); 
     string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "MyPage"); 
     GetPageContent(firstPageId); 
     Console.Read(); 
    } 
    static void GetNamespace() 
    { 
     string xml; 

     onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml); 
     var doc = XDocument.Parse(xml); 
     ns = doc.Root.Name.Namespace; 
    } 

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

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

     switch (scope) 
     { 
      case (OneNote.HierarchyScope.hsNotebooks): nodeName = "Notebook"; break; 
      case (OneNote.HierarchyScope.hsPages): nodeName = "Page"; break; 
      case (OneNote.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 GetPageContent(string pageId) 
    { 
     string xml; 
     onenoteApp.GetPageContent(pageId, out xml, OneNote.PageInfo.piAll); 
     var doc = XDocument.Parse(xml); 
     var outLine = doc.Descendants(ns + "Outline").First(); 
     var content = outLine.Descendants(ns + "T").First(); 
     string contentVal = content.Value; 
     content.Value = "modified"; 
     onenoteApp.UpdatePageContent(doc.ToString()); 
     return null; 
    } 
0

如果您使用的是C#,請查看http://dev.onenote.com上更新的OneNote REST API。它已經支持創建一個新頁面,並有一個測試版API來修補和添加內容到現有頁面。

+1

謝謝你,不過我已閱讀,然後再發布上述文章,甚至嘗試了一些代碼示例。而且我仍然無法找到使用Interop將文本/內容添加到OneNote文檔的代碼。我並不試圖開發一個應用程序,例如將不斷與OneNote或OneNote API通信的Windows應用商店/ Windows Phone應用程序。我最好只是設計一個控制檯應用程序來創建和編輯OneNote文檔。類似於使用Interop for Word和Excel可以完成的工作 – B17 2014-12-05 02:36:31

+0

請參閱http://stackoverflow.com/questions/14875760/onenote-inserting-elements-with-updatepagecontent help。 – DipakBoyed 2014-12-05 19:24:56

+1

這不僅適用於在線OneNote文檔嗎? – johnny 2017-01-09 22:21:46

2

這正是我已經從網絡上閱讀的例子收集(當然,您已經閱讀所有這些)和偷看使用ONOMspy OneNote中存儲其在XML數據的方式( http://blogs.msdn.com/b/johnguin/archive/2011/07/28/onenote-spy-omspy-for-onenote-2010.aspx)。

如果您想使用OneNote內容,則需要對XML有基本的瞭解。將文本寫入OneNote頁面涉及創建一個大綱元素,其內容將包含在OEChildren元素中。在OEChildren元素中,可以有許多其他代表大綱內容的子元素。如果我正確讀取模式,這些可以是OEHTMLBlock類型。就個人而言,我只使用OE,在這種情況下,您將有一個OE元素包含一個T(文本)元素。下面的代碼將創建大綱的XElement和文本添加到它:

// Get info from OneNote 
string xml; 
onApp.GetHierarchy(null, OneNote.HierarchyScope.hsSections, out xml); 
XDocument doc = XDocument.Parse(xml); 
XNamespace ns = doc.Root.Name.Namespace; 

// Assuming you have a notebook called "Test" 
XElement notebook = doc.Root.Elements(ns + "Notebook").Where(x => x.Attribute("name").Value == "Test").FirstOrDefault(); 
if (notebook == null) 
{ 
    Console.WriteLine("Did not find notebook titled 'Test'. Aborting."); 
    return; 
} 

// If there is a section, just use the first one we encounter 
XElement section; 
if (notebook.Elements(ns + "Section").Any()) 
{ 
    section = notebook.Elements(ns + "Section").FirstOrDefault(); 
} 
else 
{ 
    Console.WriteLine("No sections found. Aborting"); 
    return; 
} 

// Create a page 
string newPageID; 
onApp.CreateNewPage(section.Attribute("ID").Value, out newPageID); 

// Create the page element using the ID of the new page OneNote just created 
XElement newPage = new XElement(ns + "Page"); 
newPage.SetAttributeValue("ID", newPageID); 

// Add a title just for grins 
newPage.Add(new XElement(ns + "Title", 
    new XElement(ns + "OE", 
     new XElement(ns + "T", 
      new XCData("Test Page"))))); 

// Add an outline and text content 
newPage.Add(new XElement(ns + "Outline", 
    new XElement(ns + "OEChildren", 
     new XElement(ns + "OE", 
      new XElement(ns + "T", 
       new XCData("Here is some new sample text.")))))); 

// Now update the page content 
onApp.UpdatePageContent(newPage.ToString()); 

這裏就是你發送到OneNote實際的XML看起來像:

<Page ID="{20A13151-AD1C-4944-A3D3-772025BB8084}{1}{A1954187212743991351891701718491104445838501}" xmlns="http://schemas.microsoft.com/office/onenote/2013/onenote"> 
    <Title> 
    <OE> 
     <T><![CDATA[Test Page]]></T> 
    </OE> 
    </Title> 
    <Outline> 
    <OEChildren> 
     <OE> 
     <T><![CDATA[Here is some new sample text.]]></T> 
     </OE> 
    </OEChildren> 
    </Outline> 
</Page> 

希望幫助讓你開始!

2

給定的任務可以使用Aspose.Note輕鬆解決。下面的代碼創建一個新文檔,將文本添加到它的標題:

var doc = new Document(); 
var page = new Page(doc); 
page.Title = new Title(doc) 
{ 
    TitleText = new RichText(doc) 
        { 
         Text = "Title text.", 
         DefaultStyle = TextStyle.DefaultMsOneNoteTitleTextStyle 
        }, 
    TitleDate = new RichText(doc) 
        { 
         Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), 
         DefaultStyle = TextStyle.DefaultMsOneNoteTitleDateStyle 
        }, 
    TitleTime = new RichText(doc) 
        { 
         Text = "12:34", 
         DefaultStyle = TextStyle.DefaultMsOneNoteTitleTimeStyle 
        } 
}; 
page.AppendChild(outline); 
doc.AppendChild(page); 
doc.Save("output.one")