Answered by:
VS 2012 RC and Visual Studio Tools for Office

Question
-
I am trying to write a simple application working with Visual Studio Tools for Office and Visual Studio 2012 RC (Windows 7 64-bit). The application, written in C#, is described in http://www.dotnetperls.com/word . The code opens a doc file and reads the contents. I've installed not only VS 2012 RC but Office Home and Student 2010 (64-bit). Unfortunately, I get the following error on the Open() call.
"Additional information: Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))."
This is a clean machine, with virtually nothing installed before installing VS2012 and Office 2010.
Does anyone have a suggestion to fix this problem? Does VS2012 RC and Office 2010 in fact actually work together, or is it some other version of Office that is to be used?
Ken
Monday, August 6, 2012 11:36 PM
Answers
-
Hi Ken,
It's based on my experience that you target machine has been install Office 2013, however you don't know it. Please reference Tom XU_MSFT's reply at Office 2013 preview forum: http://social.msdn.microsoft.com/Forums/en-US/officedevpreview/thread/b3320dc0-7bcb-4bc1-8d77-1a29ef491e95. I think you need check it.
T.X.
- Marked as answer by 许阳(无锡) Monday, August 13, 2012 7:14 AM
Wednesday, August 8, 2012 11:27 AM -
Microsoft do 'recommend' using Office 2013 Preview with Visual Studio 2012 RC; however if Visual Studio 2012 RC cannot be used to program for Office 2010 that's obviously a problem that needs to be addressed.
Could you just confirm, Ken, what version of the Microsoft.Office.Interop.Word.dll you're using?
I suspect that VS 2012 RC has installed the Office 2013 type libraries, and that there are now duplicate versions registered. I got that specific error message ('unable to cast _Application'....'TYPE_E_LIBNOTREGISTERED') after a messy uninstall, and then re-installing an older version of Office. I solved it by following the advice of another forum thread here which I'm sadly unable to find...basically I searched the registry for the CLSID in the error message, so, {00020970-0000-0000-C000-000000000046}. Its registry key contained two keys, one of which was called 'TypeLib', which in turn contained another CLSID for the type library. I then searched the registry, starting at the beginning again, for that second CLSID, which led me to the relevant interop type library. It had two entries....8.3 and 8.4, the bigger number corresponding to the later version of Office....which I deleted...and immediately was able to run my program.
Wednesday, August 8, 2012 2:38 PM
All replies
-
Hi Ken,
Thanks for posting in the MSDN Forum.
Would you please provide me some snippets for further research?
Have a good day,
Tom
Tom Xu [MSFT]
MSDN Community Support | Feedback to us
Tuesday, August 7, 2012 2:56 AM -
Hi Tom,
Thanks for your help.
Here is the code.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Interop.Word; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Open a doc file. Application application = new Application(); Document document = application.Documents.Open("C:\\word.doc"); // Loop through all words in the document. int count = document.Words.Count; for (int i = 1; i <= count; i++) { // Write the word. string text = document.Words[i].Text; Console.WriteLine("Word {0} = {1}", i, text); } // Close word. application.Quit(); } } }
Ken
Tuesday, August 7, 2012 11:02 AM -
I was able to get the program to work after installing Office 2013 Professional Plus Preview, and Office Developer Tools for Visual Studio 2012. It looks like VS2012 does not work with Word 2010.
Ken
Tuesday, August 7, 2012 5:57 PM -
Hi Ken,
It's based on my experience that you target machine has been install Office 2013, however you don't know it. Please reference Tom XU_MSFT's reply at Office 2013 preview forum: http://social.msdn.microsoft.com/Forums/en-US/officedevpreview/thread/b3320dc0-7bcb-4bc1-8d77-1a29ef491e95. I think you need check it.
T.X.
- Marked as answer by 许阳(无锡) Monday, August 13, 2012 7:14 AM
Wednesday, August 8, 2012 11:27 AM -
Microsoft do 'recommend' using Office 2013 Preview with Visual Studio 2012 RC; however if Visual Studio 2012 RC cannot be used to program for Office 2010 that's obviously a problem that needs to be addressed.
Could you just confirm, Ken, what version of the Microsoft.Office.Interop.Word.dll you're using?
I suspect that VS 2012 RC has installed the Office 2013 type libraries, and that there are now duplicate versions registered. I got that specific error message ('unable to cast _Application'....'TYPE_E_LIBNOTREGISTERED') after a messy uninstall, and then re-installing an older version of Office. I solved it by following the advice of another forum thread here which I'm sadly unable to find...basically I searched the registry for the CLSID in the error message, so, {00020970-0000-0000-C000-000000000046}. Its registry key contained two keys, one of which was called 'TypeLib', which in turn contained another CLSID for the type library. I then searched the registry, starting at the beginning again, for that second CLSID, which led me to the relevant interop type library. It had two entries....8.3 and 8.4, the bigger number corresponding to the later version of Office....which I deleted...and immediately was able to run my program.
Wednesday, August 8, 2012 2:38 PM -
The other thread you're looking for is probably this one: http://stackoverflow.com/questions/12957595/error-accessing-com-components
- Proposed as answer by Tom McLain Monday, April 7, 2014 8:32 PM
Friday, April 19, 2013 3:46 PM -
The thread posted by Abel contains the solution that solved the problem for me. Thanks.Monday, April 7, 2014 8:33 PM