Office Automation Troubleshooting

M

Mike W

I have an interesting and perplexing problem with office automation with C#,
..Net Windows Application. I'm trying to connecti to an existing copy of Excel

I followed the instructions in http://support.microsoft.com/kb/316126
Excel.Application oExcelApp;
this.Activate();
oExcelApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
MessageBox.Show(oExcelApp.ActiveWorkbook.Name);
oExcelApp = null;


An interesting thing happens when I run this code. On my home machine, it
works as expected. When I run this file on my work machine, it doesn't get
the reference to the Excel Application. oExcelApp is set to null and I get a
NullReferenceException on the MessageBox.Show line. I downloaded and
installed the PIA's at

http://www.microsoft.com/downloads/...3a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en

Both machines have Office 2003 installed(home also has Trial of Office 2007
installed)

The work machine also can not connect to Access.Application or
Outlook.Application either. It can, however, connect to Word.Application.
Another interesting note is that creating a new Excel Application works so
and I can manipulate an Excel Spreadsheet as expected, so the PIA's aren't
totally faulty.

I need an answer other than reinstalling Office since it is incredibly
impractical to have all users of the applications I develop to have to
reinstall Office if they encounter the same problem.
 
S

SvenC

Hi,
I have an interesting and perplexing problem with office automation
with C#, .Net Windows Application. I'm trying to connecti to an
existing copy of Excel

I followed the instructions in http://support.microsoft.com/kb/316126
Excel.Application oExcelApp;
this.Activate();
oExcelApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
MessageBox.Show(oExcelApp.ActiveWorkbook.Name);
oExcelApp = null;

oExcelApp is set to null and I get a NullReferenceException on the
MessageBox.Show line. I downloaded and installed the PIA's at

http://www.microsoft.com/downloads/...3a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en

Both machines have Office 2003 installed(home also has Trial of
Office 2007 installed)

I guess Office 2007 overwrites the ProgID Excel.Application to point to
itself. You might try if you get "Excel.Application.11". You might need to
try "Excel.Application" first and on failure "Excel.Application.11".

You can look at HKEY_CLASSES_ROOT\Excel.Application.... with RegEdit what is
registered on the system.
Maybe the PIAs are more sensible than the plain OLE automation types, as the
Application COM interfaces are compatible between the Excel versions.
 
M

Mike W

I'll try looking specifically at HKEY_Classes_Root when I'm at work again
next (Friday). I did try The "Excel.Application.11" previously as well
without any success. "Word.Application.11" did work though. but as before,
Word seemed to be the only one to respond. with the .11 appended Do you
think it would be enough to copy the registry settings from my home computer
(where it works) and put those on the work machine?

Sincerely,
Mike W

Previously Reply:
I guess Office 2007 overwrites the ProgID Excel.Application to point to
itself. You might try if you get "Excel.Application.11". You might need to
try "Excel.Application" first and on failure "Excel.Application.11".

You can look at HKEY_CLASSES_ROOT\Excel.Application.... with RegEdit what is
registered on the system.
Maybe the PIAs are more sensible than the plain OLE automation types, as the
Application COM interfaces are compatible between the Excel versions.
 
M

Mike W

I don't know if this helps at all. But I had been doing Excel Automation
with VB6 successfully on all machines. The following code is what I had
written to connect to Excel:

Dim xlapp As Excel.Application
Set xlapp = GetObject(, "Excel.Application")

This worked successfully, but the .Net equivalent fails on the same
computer.

Excel.Application oExcelApp;

oExcelApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

Mike W
 
S

SvenC

Hi,
I don't know if this helps at all. But I had been doing Excel
Automation with VB6 successfully on all machines. The following code
is what I had written to connect to Excel:

Dim xlapp As Excel.Application
Set xlapp = GetObject(, "Excel.Application")

This is all done with dispinterfaces so they are fully version independant.
Only when you call methods or properties you might get runtime errors when
calling new methods/properties which are not available on older Excel
versions.
This worked successfully, but the .Net equivalent fails on the same
computer.

Excel.Application oExcelApp;

How did you import the namespace Excel? From the Office PIAs I expect?
I guess those are not as version independant as the original COM type
libraries, so which version of the PIAs did you take? Try the oldest Office
PIAs you want to be compatible with.
oExcelApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

Does this work:

object oApp =
System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

Does this fail:

oExcelApp = (Excel.Application)oApp;

Do you also have Excel._Application as type available? If yes, try to use
that type.
 

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