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
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