Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Visual Studio offers features in Visual Basic that improve Microsoft Office programming. Features in Visual Basic include automatically implemented properties, statements in lambda expressions, and collection initializers. You can embed 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.
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, 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.
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.
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 to the top of the code file if they are not already present.Imports Microsoft.Office.Interop
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. Click Add.
Replace the definition of the
Account
class with the following code. The class definitions use automatically implemented properties. For more information, see Automatically implemented properties.Public Class Account Property ID As Integer = -1 Property Balance As Double End Class
To create a
bankAccounts
list that contains two accounts, add the following code to theThisAddIn_Startup
method in ThisAddIn.vb. The list declarations use collection initializers. For more information, see Collection Initializers.Dim bankAccounts As New List(Of Account) From { New Account With { .ID = 345, .Balance = 541.27 }, New Account With { .ID = 123, .Balance = -127.44 } }
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.Sub DisplayInExcel(ByVal accounts As IEnumerable(Of Account), ByVal DisplayAction As Action(Of Account, Excel.Range)) With Me.Application ' Add a new Excel workbook. .Workbooks.Add() .Visible = True .Range("A1").Value = "ID" .Range("B1").Value = "Balance" .Range("A2").Select() For Each ac In accounts DisplayAction(ac, .ActiveCell) .ActiveCell.Offset(1, 0).Select() Next ' Copy the results to the Clipboard. .Range("A1:B3").Copy() End With End Sub
Method Add has an optional parameter for specifying a particular template. Optional parameters 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
Range
andOffset
properties of the Range object use the indexed properties feature. 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.
Add the following code at the end of
DisplayInExcel
to adjust the column widths to fit the content.' Add the following two lines at the end of the With statement. .Columns(1).AutoFit() .Columns(2).AutoFit()
For more information about embedding interop types, see procedures "To find the PIA reference" and "To restore the PIA dependency" later in this article.
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.DisplayInExcel(bankAccounts, Sub(account, cell) ' This multiline lambda expression sets custom ' processing rules for the bankAccounts. cell.Value = account.ID cell.Offset(0, 1).Value = account.Balance If account.Balance < 0 Then cell.Interior.Color = RGB(255, 0, 0) cell.Offset(0, 1).Interior.Color = RGB(255, 0, 0) End If End Sub)
To run the program, press F5. An Excel worksheet appears that contains the data from the accounts.
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.Dim wordApp As New Word.Application wordApp.Visible = True wordApp.Documents.Add() wordApp.Selection.PasteSpecial(Link:=True, DisplayAsIcon:=True)
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 (parameter
Link
) and that the link is to be displayed in the Word document as an icon (parameterDisplayAsIcon
).
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.
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.
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.
Note
Office 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.
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
. - 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. - In Visual Studio, click Clean Solution on the Build menu to clean up the completed project.