0

I want to get Excel events in late binding without Excel dll. I can get events with my code in target cpu in "AnyCpu" and "x64" but am unable to get the same result in target cpu "x86". I need to run my application in "x86".

I have created my Excel object as follows:

Dim ExcelType As Object = Type.GetTypeFromProgID("Excel.Application")

Dim exlApp As Object= Activator.CreateInstance(ExcelType)

Dim evSheetActivate As EventInfo = ComType.GetEvent("SheetActivate")

If my TargetCpu is "AnyCpu" or "x64", object ExcelType shows "Microsoft.Office.Interop.Excel.ApplicationClass".

If my TargetCpu is "x86", ExcelType shows "System.__ComObject".

If it comes like "System.__ComObject" I am unable to get my events.

Am I able to cast "System.__ComObject" into "Microsoft.Office.Interop.Excel.ApplicationClass", or is there any other solution to resolve this?

MJH
  • 2,301
  • 7
  • 18
  • 20
  • What bitness does the installed Excel version have? (You can see this in Windows Task Manager if you look for the Excel.exe process.) – haindl Oct 06 '16 at 14:37
  • @haindl am using 64 bit version. – Sabari Saravanan Oct 06 '16 at 14:49
  • I tested your code in my environment which is similar to yours but unfortunately I cannot reproduce your ExcelType problem. In my case `ExcelType` is always an instance of `Excel.ApplicationClass` no matter if x86/x64/AnyCPU or Debug/Release. Maybe you can try to repair your office installation using Control Panel --> Programs and Features. – haindl Oct 06 '16 at 14:54
  • @haindl thanks a lot.I found what the issue is i am having 64 bit excel if my target cpu is "AnyCpu" means it brings Excel.ApplicationClass In my server it shows system._object because there we are having 32 bit.Another case is if my target cpu is "x86" means it shows system._object in 64 bit and Excel.ApplicationClass in 32 bit.how can we handle this.help me. – Sabari Saravanan Oct 06 '16 at 15:12
  • Which version of Excel are you using? (e.g. 2003, 2007, 2010, 2013 or 2016) And which version of .net are you using? (e.g. 3.5, 4, 4.5 or 4.6) – haindl Oct 06 '16 at 15:28
  • @haindl i have tested with excel 2016 and 2010 and i am using 3.5 framework.if i build target cpu as x86 for 32 bit office means it shows Excel.ApplicationClass and if i build target cpu x64 for 64 bit office it shows Excel.ApplicationClass i dont want to build exe based on bits.Is there a way for casting system._object to Excel.ApplicationClass. – Sabari Saravanan Oct 07 '16 at 08:37
  • If you use .net 3.5 and Excel 2010 then you have to make sure that you have installed the [Office primary interop assemblies](https://www.microsoft.com/en-us/download/details.aspx?id=3508) on that machine. (There are no extra PIAs for Excel 2016.) Generally the bitness of Excel and your application shouldn't matter. Yes, you can try to cast the `System.__ComObject` in `ExcelType` to `Excel.Application` but then I may ask the question: Why do you use late binding and not early binding in your code? – haindl Oct 07 '16 at 09:08
  • I want to run my application in all version of office i.e avoiding dependency of using dll. – Sabari Saravanan Oct 07 '16 at 09:53
  • If you don't want to have a deployment dependency on the Office 2010 PIAs then you have to upgrade your .net framework at least to .net 4, use early binding instead of late binding and then embed the Office interop types like shown [here](http://stackoverflow.com/questions/6431348/support-different-office-versions-with-office-automation) and [here](http://www.claudiobernasconi.ch/2014/02/13/painless-office-interop-using-visual-c-sharp/). Maybe this will solve your original problem too. – haindl Oct 07 '16 at 09:59
  • @haindl But is there any other way to achieve this without changing framework and adding interop? – Sabari Saravanan Oct 10 '16 at 12:48
  • You can't have the best from both worlds at the same time. Either you use .net 3.5 and ensure that the Office 2010 PIAs are installed (in case of Excel 2010) - or you use .net 4+ and embed the Excel interop assembly. But more important: Use late binding only when really needed. If it isn't needed then use early binding. So if I were you, I would try early binding first. Maybe this is all you need to solve your problem. – haindl Oct 10 '16 at 12:58

0 Answers0