In theory…
To read the rather disjointed documentation that MS publishes, you’d think that all you needed to do is:
1) Download the Primary Interop assembly installer appropriate to your version of Office. - e.g., O2003PIA.EXE from http://www.microsoft.com/downloads/details.aspx?familyid=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en)
2) Install the download
3) Start VS and, in your project, go to Add Reference, COM tab and set a reference to the particular Office application you need to interface with – e.g., Microsoft Word 11.0 Object Library
4) At the beginning of the code file for your class, insert a corresponding “Imports” statement – e.g.,
Imports Microsoft.office.Interop.Word
5) … and program happily ever after.
‘T’aint necessarily so!
If you do steps 1-3, but step 4 results in a green squiggle under Microsoft.office.Interop.Word, you might have run into the problem – or something very like it - described on page http://support.microsoft.com/kb/823996, though the symptom described at the top of that page is probably not at all what you would notice first (“…you notice that Visual Studio .NET 2003 creates an Interop Assembly (IA) for the library instead of referencing the Primary Interop Assembly (PIA).” Yeah, right; this symptom just leaps to the eye!)
To check if you indeed have this problem, right-click and open My Project in Solution Explorer for your project, click the References tab (at the left hand side of the window) and widen the Path column so you can see the full paths associated with each reference.
If the Microsoft Office references are correct, they will have paths that begin
C:\Windows\assembly\GAC\
If the references are not correct, the paths will point to a location within the project’ own folder structure – e.g., to a sub-folder of %My Documents%\Visual Studio 2005\Projects\.
If the referenced path is wrong, close VS and do the resolution step as described at http://support.microsoft.com/kb/823996 - “To resolve this problem, Microsoft recommends that you run Office Setup from Add or Remove Programs in Control Panel:
•If the PIA does not appear in the GAC, run Office Setup and then mark the PIA for the application as Run from My Computer. Each Office PIA appears as a .NET Programmability Support feature for the corresponding Office application or for the corresponding Office component.
•If the PIA does appear in the GAC, run Office Setup and then select the option to repair your Office installation.”
That is, for each Office application, click the drop-down list beside .NET Programmability Support and set it to “Run from My Computer”. (The picture here shows that option selected for Word).
After that, there are another couple of steps that the Microsoft KB article does not cover, but which I was alerted to be a post at http://www.vbforums.com/showthread.php?t=510713. (Thanks, masfenix)
· Reopen your project and go to My project > References
· Remove the incorrect references and save the project.
· Add once more a reference to the Office application you need to work with –
e.g., Microsoft Word 11.0 Object Library
· Note that a corresponding reference to (e.g.) the Microsoft Object 11.0 Object Library has also been automatically added, along with a reference to the Visual Basic for Applications Extensibility Library.
· Confirm that the paths now point to locations in C:\Windows\assembly\GAC\
(if they don’t, you will have to Google further – sorry!).