VSTO VBA Interop Sample

This sample demonstrates how to call a method in a document-level customization for Microsoft Office Excel from Visual Basic for Applications (VBA) code in the workbook.

 
 
 
 
 
4 Star
(1)
6,046 times
Add to favorites
2/19/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
VB.NET
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ProjectTypeGuids>{BAA0C2D2-18E2-41B9-852F-F413020CAA33};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <OutputType>Library</OutputType>
    <RootNamespace>NorthwindEmployees</RootNamespace>
    <AssemblyName>NorthwindEmployees</AssemblyName>
    <VSTO_HostDocumentName>NorthwindEmployees.xlsm</VSTO_HostDocumentName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <OfficeApplication>Excel</OfficeApplication>
    <ProjectGuid>{42263D42-0FB3-4D02-A643-CDCA83C12A6D}</ProjectGuid>
    <SolutionID>470f2b2e-d168-4c8a-a268-d8257af3ebe9</SolutionID>
    <FileUpgradeFlags>
    </FileUpgradeFlags>
    <OldToolsVersion>4.0</OldToolsVersion>
    <VSTO_TrustAssembliesLocation>true</VSTO_TrustAssembliesLocation>
    <UpgradeBackupLocation />
    <BootstrapperEnabled>true</BootstrapperEnabled>
    <PublishUrl>publish\</PublishUrl>
    <InstallUrl />
    <TargetCulture>en</TargetCulture>
    <ApplicationVersion>1.0.0.0</ApplicationVersion>
    <AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
    <UpdateEnabled>true</UpdateEnabled>
    <UpdateInterval>7</UpdateInterval>
    <UpdateIntervalUnits>days</UpdateIntervalUnits>
    <IsWebBootstrapper>False</IsWebBootstrapper>
    <ProductName>NorthwindEmployees</ProductName>
    <PublisherName />
    <SupportUrl />
    <FriendlyName>NorthwindEmployees</FriendlyName>
    <OfficeApplicationDescription />
  </PropertyGroup>
  <ItemGroup>
    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Office.PIARedist.2007">
      <Visible>False</Visible>
      <ProductName>Microsoft Office 2007 Primary Interop Assemblies</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.VSTORuntime.4.0">
      <Visible>False</Visible>
      <ProductName>Microsoft Visual Studio 2010 Tools for Office Runtime %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
      <Visible>False</Visible>
      <ProductName>Windows Installer 3.1</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <PropertyGroup>
    <VSTO_EmbedTypeLibraryInAssembly>false</VSTO_EmbedTypeLibraryInAssembly>
    <OptionExplicit>On</OptionExplicit>
    <OptionCompare>Binary</OptionCompare>
    <OptionStrict>Off</OptionStrict>
    <OptionInfer>On</OptionInfer>
    <SignManifests>true</SignManifests>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>$(DefineConstants)</DefineConstants>
    <DefineDebug>true</DefineDebug>
    <DefineTrace>true</DefineTrace>
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DocumentationFile>NorthwindEmployees.xml</DocumentationFile>
    <RegisterForComInterop>false</RegisterForComInterop>
    <RemoveIntegerChecks>false</RemoveIntegerChecks>
    <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DefineConstants>
    </DefineConstants>
    <DefineDebug>false</DefineDebug>
    <DefineTrace>true</DefineTrace>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DocumentationFile>NorthwindEmployees.xml</DocumentationFile>
    <RegisterForComInterop>false</RegisterForComInterop>
    <RemoveIntegerChecks>false</RemoveIntegerChecks>
    <NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="Microsoft.Office.Tools.Common.v9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    <Reference Include="Microsoft.Office.Tools.Excel.v9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    <Reference Include="Microsoft.Office.Tools.v9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    <Reference Include="Microsoft.Vbe.Interop, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    <Reference Include="Office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="stdole, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.AddIn" />
    <Reference Include="System.Data" />
    <Reference Include="System.Data.Services.Client" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
  </ItemGroup>
  <ItemGroup>
    <Import Include="Microsoft.VisualBasic" />
    <Import Include="System" />
    <Import Include="System.Collections.Generic" />
    <Import Include="System.Data" />
    <Import Include="System.Linq" />
    <Import Include="System.Text" />
    <Import Include="System.Windows.Forms" />
    <Import Include="System.Xml.Linq" />
    <Import Include="Microsoft.VisualStudio.Tools.Applications.Runtime" />
    <Import Include="Office = Microsoft.Office.Core" />
    <Import Include="Excel = Microsoft.Office.Interop.Excel" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="My Project\AssemblyInfo.vb" />
    <Compile Include="Service References\NorthwindServiceReference\Reference.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Reference.datasvcmap</DependentUpon>
    </Compile>
    <Compile Include="Sheet1.Designer.vb">
      <DependentUpon>Sheet1.Designer.xml</DependentUpon>
    </Compile>
    <Compile Include="Sheet1.vb">
      <DependentUpon>NorthwindEmployees.xlsm</DependentUpon>
    </Compile>
    <Compile Include="Sheet2.Designer.vb">
      <DependentUpon>Sheet2.Designer.xml</DependentUpon>
    </Compile>
    <Compile Include="Sheet2.vb">
      <DependentUpon>NorthwindEmployees.xlsm</DependentUpon>
    </Compile>
    <Compile Include="Sheet3.Designer.vb">
      <DependentUpon>Sheet3.Designer.xml</DependentUpon>
    </Compile>
    <Compile Include="Sheet3.vb">
      <DependentUpon>NorthwindEmployees.xlsm</DependentUpon>
    </Compile>
    <Compile Include="ThisWorkbook.Designer.vb">
      <DependentUpon>ThisWorkbook.Designer.xml</DependentUpon>
    </Compile>
    <Compile Include="ThisWorkbook.vb">
      <DependentUpon>NorthwindEmployees.xlsm</DependentUpon>
    </Compile>
    <EmbeddedResource Include="My Project\Resources.resx">
      <Generator>VbMyResourcesResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.vb</LastGenOutput>
      <CustomToolNamespace>My.Resources</CustomToolNamespace>
      <SubType>Designer</SubType>
    </EmbeddedResource>
    <Compile Include="My Project\Resources.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <EmbeddedResource Include="ThisWorkbook.resx">
      <DependentUpon>ThisWorkbook.vb</DependentUpon>
    </EmbeddedResource>
    <None Include="My Project\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.vb</LastGenOutput>
    </None>
    <Compile Include="My Project\Settings.Designer.vb">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <None Include="NorthwindEmployees.xlsm" />
    <None Include="Service References\NorthwindServiceReference\service.edmx" />
  </ItemGroup>
  <ItemGroup>
    <None Include="ThisWorkbook.Designer.xml">
      <DependentUpon>ThisWorkbook.vb</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="Sheet1.Designer.xml">
      <DependentUpon>Sheet1.vb</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="Sheet2.Designer.xml">
      <DependentUpon>Sheet2.vb</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="Sheet3.Designer.xml">
      <DependentUpon>Sheet3.vb</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <WCFMetadata Include="Service References\" />
  </ItemGroup>
  <ItemGroup>
    <ServiceReferenceMetadataStorage Include="Service References\NorthwindServiceReference\">
      <Type>datasvcmap</Type>
    </ServiceReferenceMetadataStorage>
  </ItemGroup>
  <ItemGroup>
    <None Include="Service References\NorthwindServiceReference\Reference.datasvcmap">
      <Generator>DataServiceClientGenerator</Generator>
      <LastGenOutput>Reference.vb</LastGenOutput>
    </None>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
  <ProjectExtensions>
    <VisualStudio>
      <FlavorProperties GUID="{BAA0C2D2-18E2-41B9-852F-F413020CAA33}">
        <ProjectProperties HostName="NorthwindEmployees.xlsm" HostPackage="{825100CF-0BA7-47EA-A084-DCF3308DAF74}" OfficeVersion="14.0" VstxVersion="4.0" ApplicationType="XLS" Language="VB" TemplatesPath="VSTOTemplates" DebugInfoExeName="C:\Program Files\Microsoft Office\Office14\Excel.exe" DebugInfoCommandLine="&quot;[$OUTPUT]NorthwindEmployees.xlsm&quot;" />
        <Host Name="Excel Document" Path=".\NorthwindEmployees.xlsm">
          <HostItem Name="Sheet1" Code="Sheet1.vb" CanonicalName="Sheet1" DisplayName="Sheet1" Blueprint="Sheet1.Designer.xml" GeneratedCode="Sheet1.Designer.vb" />
          <HostItem Name="Sheet2" Code="Sheet2.vb" CanonicalName="Sheet2" DisplayName="Sheet2" Blueprint="Sheet2.Designer.xml" GeneratedCode="Sheet2.Designer.vb" />
          <HostItem Name="Sheet3" Code="Sheet3.vb" CanonicalName="Sheet3" DisplayName="Sheet3" Blueprint="Sheet3.Designer.xml" GeneratedCode="Sheet3.Designer.vb" />
          <HostItem Name="ThisWorkbook" Code="ThisWorkbook.vb" CanonicalName="ThisWorkbook" Blueprint="ThisWorkbook.Designer.xml" GeneratedCode="ThisWorkbook.Designer.vb" />
        </Host>
      </FlavorProperties>
    </VisualStudio>
  </ProjectExtensions>
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\OfficeTools\Microsoft.VisualStudio.Tools.Office.targets" />
  <!--
    To modify your build process, add your task inside one of the targets below and uncomment it.
    Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>