C# - Word 2003 - really weird error

L

lpeabody

Fellow Developers,

Thanks in advance for reading this. I am in dire need of help here.
I've asked a few people around the office already for help but they
haven't been able to come up with anything.

Project Type: Microsoft Word 2003 Shared Add-In
Development Language: C#
Problem: Attempt to cast applicationObject as
Microsoft.Office.Interop.Word.Application to access the CommandBars
Count property. Comes back as NullReferenceException when accessing
Count property. However, it appears that applicationObject and
CommandBars are not null.

When debugging, I add a watch to applicationObject. I then browse to
the CommandBars Count property, and I see that it is set to a value of
143. So, the object and everything about it has values, it's just when
I attempt to cast the object into
Microsoft.Officer.Interop.Word.Application (which is what it's supposed
to be) it throws a NullReferenceException.

Error Producing Code:
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(applicationObject.CommandBars.Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
System.NullReferenceException: Object reference not set to an instance
of an object.

However, the following code produces the value of the Count property.
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(((CommandBars)GetObject("CommandBars")).Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private object GetObject(string objName)
{
return applicationObject.GetType().InvokeMember(objName,
BindingFlags.GetProperty , null, applicationObject ,null);
}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
143

I don't understand why the second version of the code works and the
first version does not, it makes absolutely no sense to me and I am
screaming for help here.

Regards,
Les
 
B

Bernd

Hi lp,

I recently was in a similar mess (I am using VB.NET).
The solution was to use function 'DirectCast' instead of a CType cast .
As far as I know there is no such function in C#, but may be you
get an idea what to do.

Good luck
Bernd

Fellow Developers,

Thanks in advance for reading this. I am in dire need of help here.
I've asked a few people around the office already for help but they
haven't been able to come up with anything.

Project Type: Microsoft Word 2003 Shared Add-In
Development Language: C#
Problem: Attempt to cast applicationObject as
Microsoft.Office.Interop.Word.Application to access the CommandBars
Count property. Comes back as NullReferenceException when accessing
Count property. However, it appears that applicationObject and
CommandBars are not null.

When debugging, I add a watch to applicationObject. I then browse to
the CommandBars Count property, and I see that it is set to a value of
143. So, the object and everything about it has values, it's just when
I attempt to cast the object into
Microsoft.Officer.Interop.Word.Application (which is what it's supposed
to be) it throws a NullReferenceException.

Error Producing Code:
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(applicationObject.CommandBars.Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
System.NullReferenceException: Object reference not set to an instance
of an object.

However, the following code produces the value of the Count property.
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(((CommandBars)GetObject("CommandBars")).Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private object GetObject(string objName)
{
return applicationObject.GetType().InvokeMember(objName,
BindingFlags.GetProperty , null, applicationObject ,null);
}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
143

I don't understand why the second version of the code works and the
first version does not, it makes absolutely no sense to me and I am
screaming for help here.

Regards,
Les
 
H

Helmut Obertanner

Hello,

if you have created your addin with vs2003 wizard
try to remove the reference to the office dll and explicitly add
a new reference to Ofiice11 COM Library.

change

using Microsoft.Office.Core;
to
using Office = Microsoft.Office.Interop.Core;


Hope this helps.


--
Helmut Obertanner
Technical Consultant
Softwaredevelopment
DATALOG Software AG | Zschokkestr. 36 | D-80687 Munich

.... and IT works!



Fellow Developers,

Thanks in advance for reading this. I am in dire need of help here.
I've asked a few people around the office already for help but they
haven't been able to come up with anything.

Project Type: Microsoft Word 2003 Shared Add-In
Development Language: C#
Problem: Attempt to cast applicationObject as
Microsoft.Office.Interop.Word.Application to access the CommandBars
Count property. Comes back as NullReferenceException when accessing
Count property. However, it appears that applicationObject and
CommandBars are not null.

When debugging, I add a watch to applicationObject. I then browse to
the CommandBars Count property, and I see that it is set to a value of
143. So, the object and everything about it has values, it's just when
I attempt to cast the object into
Microsoft.Officer.Interop.Word.Application (which is what it's supposed
to be) it throws a NullReferenceException.

Error Producing Code:
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(applicationObject.CommandBars.Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
System.NullReferenceException: Object reference not set to an instance
of an object.

However, the following code produces the value of the Count property.
-------------------------------------------------------------------------------
namespace IssueLogger
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

[GuidAttribute("F9F0FC56-5638-47F8-80A6-2934D3E53158"),
ProgId("IssueLogger.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{

public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
applicationObject = (Microsoft.Office.Interop.Word.Application)
application;
addInInstance = addInInst;

if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
{
OnStartupComplete(ref custom);
}

}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
if(disconnectMode !=
Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
{
OnBeginShutdown(ref custom);
}
applicationObject = null;
}


public void OnAddInsUpdate(ref System.Array custom)
{
}

public void OnStartupComplete(ref System.Array custom)
{
try
{
MessageBox.Show(((CommandBars)GetObject("CommandBars")).Count.ToString());
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

public void OnBeginShutdown(ref System.Array custom)
{

}

private object GetObject(string objName)
{
return applicationObject.GetType().InvokeMember(objName,
BindingFlags.GetProperty , null, applicationObject ,null);
}

private Microsoft.Office.Interop.Word.Application applicationObject;
private object addInInstance;
}
}
-------------------------------------------------------------------------------
Displays a message box saying:
143

I don't understand why the second version of the code works and the
first version does not, it makes absolutely no sense to me and I am
screaming for help here.

Regards,
Les
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top