我在代码中遇到此错误。我试图获取word文件的内容并将它们添加到现有的一个注释文件中。以下是我到目前为止的情况:
namespace WordAddIn1
{
public partial class ThisAddIn
{
//creates a new OneNote Application object
static OneNote.Application onenoteApp;
static XNamespace ns = null;
static string resultWordText = "";
static Microsoft.Office.Interop.Word.Application word_app;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
onenoteApp = new OneNote.Application();
getWordText();
writeToOneNote();
}
public static void getWordText()
{
try
{
word_app = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");
word_app.Visible = true;
object inputFile = "C:\\Users\\t-aaalle\\Desktop\\GreetingFile2.docx";// "selected_doc" contains the document name
object confirmConversions = false;
object readOnly = false;
object missing = Type.Missing;
Document doc = word_app.Documents.Open(ref inputFile, ref confirmConversions, ref readOnly, ref missing);
}
catch (Exception e)
{
var trace = new System.Diagnostics.StackTrace(e);
}
}
public static void writeToOneNote()
{
GetNamespace();
//Gets the notebook, section, and firstpage
string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "Aaron @ Microsoft");
string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "New Section 1");
string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "My Page");
//Reads the contents of the first page
GetPageContent(firstPageId);
Console.Read();
}
//Gets the name of the notebook
static void GetNamespace()
{
string xml = "";
onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml);
var doc = XDocument.Parse(xml);
ns = doc.Root.Name.Namespace;
}
//Gets the OneNote document
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;
}
//Get's the pages content
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();
//THE LINE ABOVE IS WHERE THE ERROR IS BEING THROWN
string contentVal = content.Value;
content.Value = resultWordText;
onenoteApp.UpdatePageContent(doc.ToString());
return null;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
我是C#的新手,完整的错误是:
An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code
这个错误令人困惑,因为我已经搜索过,无法找到任何可以帮助我的东西。帮助解决这个错误会很棒!