Menu
Jul 20, 2019 I have an issue where a word add-in will not load and I have narrowed it down to a missing microsoft.office.interop.word.dll I wanted to reproduce on another machine and so I deleted that dll and loaded word, but Word would automatically put that dll BACK.
-->Visual Studio offers features in C# and Visual Basic that improve Microsoft Office programming. Helpful C# features include named and optional arguments and return values of type
dynamic
. In COM programming, you can omit the ref
keyword and gain access to indexed properties. Features in Visual Basic include auto-implemented properties, statements in lambda expressions, and collection initializers.Both languages enable embedding of type information, which allows deployment of assemblies that interact with COM components without deploying primary interop assemblies (PIAs) to the user's computer. For more information, see Walkthrough: Embedding Types from Managed Assemblies.
This walkthrough demonstrates these features in the context of Office programming, but many of these features are also useful in general programming. In the walkthrough, you use an Excel Add-in application to create an Excel workbook. Next, you create a Word document that contains a link to the workbook. Finally, you see how to enable and disable the PIA dependency.
Prerequisites
You must have Microsoft Office Excel and Microsoft Office Word installed on your computer to complete this walkthrough.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the IDE.
To set up an Excel Add-in application
- Start Visual Studio.
- On the File menu, point to New, and then click Project.
- In the Installed Templates pane, expand Visual Basic or Visual C#, expand Office, and then click the version year of the Office product.
- In the Templates pane, click Excel <version> Add-in.
- Look at the top of the Templates pane to make sure that .NET Framework 4, or a later version, appears in the Target Framework box.
- Type a name for your project in the Name box, if you want to.
- Click OK.
- The new project appears in Solution Explorer.
To add references
- In Solution Explorer, right-click your project's name and then click Add Reference. The Add Reference dialog box appears.
- On the Assemblies tab, select Microsoft.Office.Interop.Excel, version
<version>.0.0.0
(for a key to the Office product version numbers, see Microsoft Versions), in the Component Name list, and then hold down the CTRL key and select Microsoft.Office.Interop.Word,version <version>.0.0.0
. If you do not see the assemblies, you may need to ensure they are installed and displayed (see How to: Install Office Primary Interop Assemblies). - Click OK.
To add necessary Imports statements or using directives
- In Solution Explorer, right-click the ThisAddIn.vb or ThisAddIn.cs file and then click View Code.
- Add the following
Imports
statements (Visual Basic) orusing
directives (C#) to the top of the code file if they are not already present.
To create a list of bank accounts
- In Solution Explorer, right-click your project's name, click Add, and then click Class. Name the class Account.vb if you are using Visual Basic or Account.cs if you are using C#. Click Add.
- Replace the definition of the
Account
class with the following code. The class definitions use auto-implemented properties. For more information, see Auto-Implemented Properties. - To create a
bankAccounts
list that contains two accounts, add the following code to theThisAddIn_Startup
method in ThisAddIn.vb or ThisAddIn.cs. The list declarations use collection initializers. For more information, see Collection Initializers.
To export data to Excel
- In the same file, add the following method to the
ThisAddIn
class. The method sets up an Excel workbook and exports data to it.Two new C# features are used in this method. Both of these features already exist in Visual Basic.- Method Add has an optional parameter for specifying a particular template. Optional parameters, new in C# 4, enable you to omit the argument for that parameter if you want to use the parameter's default value. Because no argument is sent in the previous example,
Add
uses the default template and creates a new workbook. The equivalent statement in earlier versions of C# requires a placeholder argument:excelApp.Workbooks.Add(Type.Missing)
.For more information, see Named and Optional Arguments. - The
Range
andOffset
properties of the Range object use the indexed properties feature. This feature enables you to consume these properties from COM types by using the following typical C# syntax. Indexed properties also enable you to use theValue
property of theRange
object, eliminating the need to use theValue2
property. TheValue
property is indexed, but the index is optional. Optional arguments and indexed properties work together in the following example.In earlier versions of the language, the following special syntax is required.You cannot create indexed properties of your own. The feature only supports consumption of existing indexed properties.Tamil movie hindi dubbed download 2017. For more information, see How to: Use Indexed Properties in COM Interop Programming.
- Add the following code at the end of
DisplayInExcel
to adjust the column widths to fit the content.These additions demonstrate another feature in C#: treatingObject
values returned from COM hosts such as Office as if they have type dynamic. This happens automatically when Embed Interop Types is set to its default value,True
, or, equivalently, when the assembly is referenced by the /link compiler option. Typedynamic
allows late binding, already available in Visual Basic, and avoids the explicit casting required in C# 3.0 and earlier versions of the language.For example,excelApp.Columns[1]
returns anObject
, andAutoFit
is an Excel Range method. Withoutdynamic
, you must cast the object returned byexcelApp.Columns[1]
as an instance ofRange
before calling methodAutoFit
.For more information about embedding interop types, see procedures 'To find the PIA reference' and 'To restore the PIA dependency' later in this topic. For more information aboutdynamic
, see dynamic or Using Type dynamic.
To invoke DisplayInExcel
- Add the following code at the end of the
ThisAddIn_StartUp
method. The call toDisplayInExcel
contains two arguments. The first argument is the name of the list of accounts to be processed. The second argument is a multiline lambda expression that defines how the data is to be processed. TheID
andbalance
values for each account are displayed in adjacent cells, and the row is displayed in red if the balance is less than zero. For more information, see Lambda Expressions. - To run the program, press F5. An Excel worksheet appears that contains the data from the accounts.
To add a Word document
- Add the following code at the end of the
ThisAddIn_StartUp
method to create a Word document that contains a link to the Excel workbook.This code demonstrates several of the new features in C#: the ability to omit theref
keyword in COM programming, named arguments, and optional arguments. These features already exist in Visual Basic. The PasteSpecial method has seven parameters, all of which are defined as optional reference parameters. Named and optional arguments enable you to designate the parameters you want to access by name and to send arguments to only those parameters. In this example, arguments are sent to indicate that a link to the workbook on the Clipboard should be created (parameterLink
) and that the link is to be displayed in the Word document as an icon (parameterDisplayAsIcon
). Visual C# also enables you to omit theref
keyword for these arguments.
To run the application
- Press F5 to run the application. Excel starts and displays a table that contains the information from the two accounts in
bankAccounts
. Then a Word document appears that contains a link to the Excel table.
To clean up the completed project
- In Visual Studio, click Clean Solution on the Build menu. Otherwise, the add-in will run every time that you open Excel on your computer.
To find the PIA reference
- Run the application again, but do not click Clean Solution.
- Select the Start. Locate Microsoft Visual Studio <version> and open a developer command prompt.
- Type
ildasm
in the Developer Command Prompt for Visual Studio window, and then press ENTER. The IL DASM window appears. - On the File menu in the IL DASM window, select File > Open. Double-click Visual Studio <version>, and then double-click Projects. Open the folder for your project, and look in the bin/Debug folder for your project name.dll. Double-click your project name.dll. A new window displays your project's attributes, in addition to references to other modules and assemblies. Note that namespaces
Microsoft.Office.Interop.Excel
andMicrosoft.Office.Interop.Word
are included in the assembly. By default in Visual Studio, the compiler imports the types you need from a referenced PIA into your assembly.For more information, see How to: View Assembly Contents. - Double-click the MANIFEST icon. A window appears that contains a list of assemblies that contain items referenced by the project.
Microsoft.Office.Interop.Excel
andMicrosoft.Office.Interop.Word
are not included in the list. Because the types your project needs have been imported into your assembly, references to a PIA are not required. This makes deployment easier. The PIAs do not have to be present on the user's computer, and because an application does not require deployment of a specific version of a PIA, applications can be designed to work with multiple versions of Office, provided that the necessary APIs exist in all versions.Because deployment of PIAs is no longer necessary, you can create an application in advanced scenarios that works with multiple versions of Office, including earlier versions. However, this works only if your code does not use any APIs that are not available in the version of Office you are working with. It is not always clear whether a particular API was available in an earlier version, and for that reason working with earlier versions of Office is not recommended.NoteOffice did not publish PIAs before Office 2003. Therefore, the only way to generate an interop assembly for Office 2002 or earlier versions is by importing the COM reference. - Close the manifest window and the assembly window.
To restore the PIA dependency
- In Solution Explorer, click the Show All Files button. Expand the References folder and select Microsoft.Office.Interop.Excel. Press F4 to display the Properties window.
- In the Properties window, change the Embed Interop Types property from True to False.
- Repeat steps 1 and 2 in this procedure for
Microsoft.Office.Interop.Word
. - In C#, comment out the two calls to
Autofit
at the end of theDisplayInExcel
method. - Press F5 to verify that the project still runs correctly.
- Repeat steps 1-3 from the previous procedure to open the assembly window. Notice that
Microsoft.Office.Interop.Word
andMicrosoft.Office.Interop.Excel
are no longer in the list of embedded assemblies. - Double-click the MANIFEST icon and scroll through the list of referenced assemblies. Both
Microsoft.Office.Interop.Word
andMicrosoft.Office.Interop.Excel
are in the list. Because the application references the Excel and Word PIAs, and the Embed Interop Types property is set to False, both assemblies must exist on the end user's computer.Jul 25, 2019 Tekken Tag Tournament 2 Free Download For PC Game Working Setup you can direct download for Windows.Tekken Tag Tournament 2 is these 8th main installment in these Tekken fighting game series. Tekken Tag may host the King of Iron Fist Tournament, But. Tekken Tag Tournament 2 Game is an Action, Adventure, Fighting video game for Xbox 360 Microsoft Windows, macOS, Pocket Pc.This game developed and published by BANDAI NAMCO Entertainment. This game was Release On September 14, 2011. This Post specially made for computer download, go to footer download link and Download to your Pc. - In Visual Studio, click Clean Solution on the Build menu to clean up the completed project.
See also
Active2 years, 9 months ago
I am interested in using C# to manipulate/Automate Excel files.
After browsing the web I have found VSTO but it seems you can not use that in Visual Studio Express Edition so I can not use that.
Just few minutes ago I noticed a question in this site that used this namespace in its code:
Microsoft.Office.Interop.Excel
So I am wondering if all I need to use that is add necessary reference and if so which reference to add?
UPDATE Sony vegas pro for mac.
I installed 'Primary Interop Assemblies' as the accepted answer suggested but for some reason they are still missing in the Add Reference dialog under '.NET' but they are present in GAC.
So I just added the reference using 'Browse' and located
Microsoft.Office.Interop.Excel.dll
in GAC folder.But looking for similar questions on this site regarding adding reference from GAC it seems this is not recommended.
user850010
user850010user8500104,59999 gold badges3232 silver badges5555 bronze badges
14 Answers
These assemblies are available as NuGet packages, which is much easier than my original answer.
You can install by either right clicking on References in your project and selecting Manage NuGet packages.. and searching for one of the packages listed below, or install using the Package Manager Console:
These are available as 'Primary Interop Assemblies', which can be installed with Office, or downloaded and installed separately. How to: Install Office Primary Interop Assemblies.
Once those are installed, you can reference them in your project in the Add Reference dialog, under .NET. If you do not see those Microsoft.Office.Interop assemblies listed, then they have not been installed yet. Install them from your setup, or download and install them separately (see my link above for the downloads).
HackedByChineseHackedByChinese38.5k88 gold badges122122 silver badges137137 bronze badges
Just had this problem myself and accepted answer didn't help me but I solved it with:
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_etc > Microsoft.Office.Interop.Excel.dll
4,1081313 gold badges5555 silver badges112112 bronze badges
Answers didn't help me to solve my problem, I couldn't find (and browse) the assemblies although I installed them using Microsoft's msi installer. For me, the excel assembly is located under
0x8BADF00DC:WindowsassemblyGAC_MSILMicrosoft.Office.Interop.Excel14.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
0x8BADF00D
90711 gold badge77 silver badges1818 bronze badges
Just add reference of
Microsoft.Office.Interop.Excel
.It includes MicroSoft Excel related classes, no need to add more references.
bluish15.2k1919 gold badges9696 silver badges155155 bronze badges
RAKESH HOLKARRAKESH HOLKAR1,18255 gold badges1717 silver badges3636 bronze badges
I guess what you are trying to do is add
MisamMisamMicrosoft.Office.Interop.Excel
with using
statement with out adding its reference in your application, in that case it wont be found. Before calling it with using
statement you need to add a reference to ur application. Right click on References
and add the Excel Interop
reference.3,32322 gold badges2121 silver badges4343 bronze badges
Add reference > Browse > C: > Windows > assembly > GAC > Microsoft.Office.Interop.Excel > 12.0.0.0_wasd. > Microsoft.Office.Interop.Excel.dll
TuranianTuranian
If you have Microsoft Office installed, then you should be able to add a reference to Interop.Excel.
For example, the PC I'm typing this on has MSVS 2010 C# Express and Office 2010. I can add a reference to Microsoft.Office.Interop.Excel 11.0.0.0.
'Hope that helps
paulsm4paulsm484.2k1010 gold badges112112 silver badges138138 bronze badges
its in the com component, named:'Microsoft Office 14 Object Library'
James TanJames Tan
I found it located under Assemblies->Extensions in VS2013.
bluebunny72bluebunny72
The best option since office 2007 is using Open XML SDK for it.We used Word.Interop but it halt sometimes, and it is not recommend for Microsoft, to use it as a server side document formatting, so Open XML SDK lets you creates word documents on DOCX and Open XML formats very easily.It lets you going well with scability, confidence ( the files, if it is corrupted can be rebuild ), and another very fine characteristics.
JosefMadridJosefMadrid
Here is super solid solution, you just need have excell.dll in your Debug/Release folderMine is 77,824 bytes, I downloaded it as a file, this also explain why some people have Debug compiled but Release not or vice versa.
Trento
Mario TrentoMario Trento
Microsoft.office.interop.word
I just had the same problem, but none of these answers helped me. I did find the dll on my pc in the location Mostey noted: (
C:WindowsassemblyGAC_MSILMicrosoft.Office.Interop.Excel14.0.0.0__71e9bce111e9429cMicrosoft.Office.Interop.Excel.dll
), but this is not the one that was referenced in the project I was trying to get building.The reference in our project in Visual Studio 2012 was pointing to
C:Program Files (x86)Microsoft Visual Studio 11.0Visual Studio Tools for Office
. This location was empty for me, but it worked fine for everyone else. It took a number of tries, but I finally tracked down a working installer. I hope this saves others the same hassle!-->Office Tools Bundle installer for VS2012 <--
This was located on the Office Documentation and Download page. Scroll down to Tools Downloads. There's also currently one for VS2013.
thehelixthehelix
1.Download and install: Microsoft Office Developer Tools
2.Add references from:
C:Program Files (x86)Microsoft Visual Studio 11.0Visual Studio Tools for OfficePIAOffice15
Microsoft Office Interop Dll 1
Artur TarnowskiArtur Tarnowski
Microsoft.office.interop Dll Path
I had the same issue and the Microsoft.Office.Interop was not appearing in 'Add Reference' option once I upgraded VS2012 to VS2015. I basically repaired the installation (Control Panel > Programs & Features > VS 2012 > Right click Change > Repair) and added the Microsoft Office component. After that the same solution started working.
sk1900sk1900