> You can also check out my sample code at
>
http://blogs.msdn.com/johnguin/archive/2008/10/07/a-calendar-planner-powertoy-for-onenote.aspx > for ways of using a messagebox to show more useful exception information.
>
> --
> Thanks,
> John Guin
> OneNote Test Team
>
http://blogs.msdn.com/johnguin>
>
> "Ani" wrote:
>
> > You need to change the initialization line
> >
> > Application OneNoteApplication = new ApplicationClass();
> >
> > to
> >
> > Microsoft.Office.Interop.OneNote.Application = new
> > Microsoft.Office.Interop.OneNote.Application()
> >
> > That should fix the ambiguity. Since Windows.Forms also has an application
> > class, your code cannot decide which Application instance you are trying to
> > create.
> >
> > Ani
> > "Sam" wrote:
> >
> > > I am sorry I have not been able to make this clear. Maybe this time it will
> > > make sense.
> > >
> > > The following code gets compile error: 'Application' is an ambiguous
> > > reference between 'Microsoft.Office.Interop.OneNote.Application' and
> > > 'System.Windows.Forms.Application' G:\My Documents\Visual Studio
> > > 2008\Projects\OneNote Create Page\OneNote Create Page\Program.cs 17 10
> > > OneNote Create Page
> > >
> > > From this error message it appears that MessageBox, indeed ANY Windows Forms
> > > application, is incompatible with OneNote 2007 Addin code (any program code
> > > that uses Microsoft.Office.Interop.OneNote.Application). Is this correct or
> > > am I missing something here? If this compatibility is the case, is this the
> > > way it is designed to work or is there a workaround?
> > >
> > > using System;
> > > using System.Collections.Generic;
> > > using System.Linq;
> > > using System.Text;
> > > using System.Xml;
> > > using System.Xml.Linq;
> > > using Microsoft.Office.Interop.OneNote;
> > > using System.IO;
> > > using System.Windows.Forms;
> > >
> > > namespace OneNote_Create_Page
> > > {
> > > class Program
> > > {
> > > static void Main(string[] args)
> > > {
> > > Application OneNoteApplication = new ApplicationClass();
> > >
> > > string PCTList = "";
> > > string outFile = "g:\\OneNote\\OneNoteUpdate.xml";
> > >
> > > PCTList = "<?xml version=\"1.0\"?>" +
> > > "<one:Notebooks xmlns:one=" +
> > > "\"
http://schemas.microsoft.com/office/onenote/2007/onenote\">";> > > PCTList += "<one:Notebook name = \"Prioritized Composite Task
> > > List\">";
> > > PCTList += "<one:Section name = \"Prioritized Composite Task
> > > List\">";
> > > PCTList += "<one:Page name = \"Prioritized Composite Task List\">";
> > > PCTList += "<one:Title> <one:T><![CDATA[Prioritized Composite Task
> > > List]]>" +
> > > "</one:T></one:Title>";
> > > PCTList += "<one:Outline><one:T><![CDATA[Item one
> > > abcedf]]></one:T></one:Outline>";
> > > PCTList +=
> > > "</one:Page></one:Section></one:Notebook></one:Notebooks>";
> > >
> > > Console.WriteLine("XML: {0}", PCTList);
> > > FileStream fs = new FileStream(outFile, FileMode.Create,
> > > FileAccess.Write);
> > > TextWriter tw = new StreamWriter(fs);
> > > tw.Write(PCTList);
> > > tw.Flush();
> > > tw.Close();
> > > // Update hierarchy
> > > //OneNoteApplication.UpdateHierarchy(PCTList.ToString());
> > >
> > > try
> > > {
> > > OneNoteApplication.UpdateHierarchy(PCTList.ToString());
> > > }
> > > catch(Exception)
> > > {
> > > MessageBox.Show("Ordinarily, one would code Exception.Message
> > > here," +
> > > " but I cannot find the appropriate exception for
> > > UpdateHierarchy",
> > > "An error has occurred with UpdateHierarchy",
> > > MessageBoxButtons.OK,
> > > MessageBoxIcon.Error);
> > > }
> > > }
> > > }
> > > }
> > >
> > >
> > > "Ilya Koulchin" wrote:
> > >
> > > > First, please post a complete, concise, and compilable (or as close as
> > > > possible) source file (including any using directives and
> > > > namespace/class declarations) that demonstrates the problem. If a line
> > > > fails to compile, leave it in in its entirety instead of replacing it
> > > > with a comment which tells us absolutely nothing about what the error
> > > > was. Second, what are the compiler error messages and numbers that you
> > > > get? Do you get any warnings as well?
> > > > By the catch block not catching anything useful, do you mean that your
> > > > program doesn't enter the catch block when an exception is thrown, or
> > > > something else?
> > > >
> > > > Sam wrote:
> > > > > I experimented with the following code. It compiles, but I would like to use
> > > > > MessageBox.Show rather than Console.WriteLine. The other problem is that
> > > > > "Exception" in the catch statement does not catch anything useful. What is
> > > > > the proper exception to code in the catch statement to catch exceptions from
> > > > > UpdateHierarchy (and other OneNote 2007 methods)?
> > > > >
> > > > > try
> > > > > {
> > > > > OneNoteApplication.UpdateHierarchy(PCTList.ToString());
> > > > > }
> > > > > catch(Exception)
> > > > > {
> > > > > // Cannot use MessageBox. Conflict with OneNote.
> > > > > Console.WriteLine("OneNote 2007 error: ",
> > > > > Error.hrFileAlreadyExists);
> > > > > }
> > > > > "Ilya Koulchin" wrote:
> > > > >
> > > > >> Sam wrote:
> > > > >>> How can I use the try/catch facility in C# with OneNote 2007 methods? How
> > > > >>> do I code the exception in catch?
> > > > >>>
> > > > >>> Apparently MessageBox is incompatible with OneNote 2007 methods because I
> > > > >>> get a compile error when I code using System.Windows.Forms; which is
> > > > >>> necessary for MessageBox to work.
> > > > >> I'm not quite sure what exactly you're asking. Could you perhaps post a
> > > > >> small code sample demonstrating what you're trying to do and what
> > > > >> doesn't work?
> > > > >>
> > > >