Group:  Other Microsoft Office Products ยป microsoft.public.onenote
Thread: try/catch exception handling

Geek News

try/catch exception handling
Sam 12/29/2008 9:48:07 PM
Hello,

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.


Re: try/catch exception handling
Ilya Koulchin <ikoulchine[ at ]hotmail.com> 12/30/2008 1:13:57 AM
Sam wrote:
[Quoted Text]
> 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?
Re: try/catch exception handling
Sam 12/30/2008 1:17:01 PM
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:

[Quoted Text]
> 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?
>
Re: try/catch exception handling
Ilya Koulchin <ikoulchine[ at ]hotmail.com> 12/30/2008 7:17:16 PM
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:
[Quoted Text]
> 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?
>>
Re: try/catch exception handling
Sam 12/30/2008 7:40:14 PM
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:

[Quoted Text]
> 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?
> >>
>
Re: try/catch exception handling
Ilya Koulchin <ikoulchine[ at ]hotmail.com> 12/30/2008 8:21:59 PM
The error indicates that the problem is not with the message box call,
but with line 17, which reads "Application OneNoteApplication = new
ApplicationClass();". The problem is with the class type declaration,
since the type Application is defined in two of the namespaces that
you've explicitly included, specifically,
Micorosoft.Ofice.Interop.OneNote and System.Windows.Forms. The solution
is to either reference the object using the fully qualified type
(Microsoft.Office.Interop.OneNote.Application), or to not include both
of the conflicting namespaces directly into the global namespace.

Sam wrote:
[Quoted Text]
> 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?
>
<snip code>
>
> "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?
>>>>
Re: try/catch exception handling
Ani 12/30/2008 8:55:21 PM
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:

[Quoted Text]
> 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?
> > >>
> >
Re: try/catch exception handling
John Guin [msft] 12/30/2008 9:05:14 PM
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:

[Quoted Text]
> 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?
> > > >>
> > >
Re: try/catch exception handling
Sam 12/30/2008 9:31:37 PM
Thanks so much. That solved the compile problem.

Now for the second part of my original question: How do I code the exception
in the catch statement to retrieve the associated message text from
UpdateHierarchy? What I have coded (Exception) is obviously wrong. Please see
the bottom of this web page:
http://msdn.microsoft.com/en-us/library/ms788684.aspx. The description text
in the last column of Table 2 is what I am after.

"Ilya Koulchin" wrote:

[Quoted Text]
> The error indicates that the problem is not with the message box call,
> but with line 17, which reads "Application OneNoteApplication = new
> ApplicationClass();". The problem is with the class type declaration,
> since the type Application is defined in two of the namespaces that
> you've explicitly included, specifically,
> Micorosoft.Ofice.Interop.OneNote and System.Windows.Forms. The solution
> is to either reference the object using the fully qualified type
> (Microsoft.Office.Interop.OneNote.Application), or to not include both
> of the conflicting namespaces directly into the global namespace.
>
> 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?
> >
> <snip code>
> >
> > "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?
> >>>>
>
Re: try/catch exception handling
Sam 12/30/2008 9:33:35 PM
I have done several searches of your link and cannot find any hits on
MessageBox.

"John Guin [msft]" wrote:

[Quoted Text]
> 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?
> > > > >>
> > > >
Re: try/catch exception handling
Ilya Koulchin <ikoulchine[ at ]hotmail.com> 12/30/2008 10:52:53 PM
Sam wrote:
[Quoted Text]
> Now for the second part of my original question: How do I code the exception
> in the catch statement to retrieve the associated message text from
> UpdateHierarchy? What I have coded (Exception) is obviously wrong. Please see
> the bottom of this web page:
> http://msdn.microsoft.com/en-us/library/ms788684.aspx. The description text
> in the last column of Table 2 is what I am after.

The OneNote API only returns a single HRESULT. It is the .NET/COM
interop layer that translates that HRESULT into an exception, so the
only thing available from the exception is the error code. If you want
to translate it into a string you'll need to do it yourself.
Re: try/catch exception handling
"Josh Einstein" <josheinstein[ at ]hotmail.com> 12/31/2008 4:44:31 AM
When dealing with Office interop I always found it useful to import the
Office namespaces using aliases.

using System.Windows.Forms;
using ON = Microsoft.Office.Interop.OneNote;
using OL = Microsoft.Office.Interop.Outlook;

Application.DoEvents(); // system.windows.forms.application
var onApp = new ON.Application(); //
microsoft.office.interop.onenote.application
var olApp = new OL.Application(); //
microsoft.office.interop.outlook.application

etc...

Josh Einstein

"Ilya Koulchin" <ikoulchine[ at ]hotmail.com> wrote in message
news:uOBEFxraJHA.4684[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
> The error indicates that the problem is not with the message box call, but
> with line 17, which reads "Application OneNoteApplication = new
> ApplicationClass();". The problem is with the class type declaration,
> since the type Application is defined in two of the namespaces that you've
> explicitly included, specifically, Micorosoft.Ofice.Interop.OneNote and
> System.Windows.Forms. The solution is to either reference the object using
> the fully qualified type (Microsoft.Office.Interop.OneNote.Application),
> or to not include both of the conflicting namespaces directly into the
> global namespace.
>
> 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?
>>
> <snip code>
>>
>> "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?
>>>>>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net