Skip to content

Commit b49b0a6

Browse files
committed
Added the ExcelDna.Interop NuGet package and added a command example to the VB project template.
1 parent 9f9e890 commit b49b0a6

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Imports ExcelDna.Integration
2+
Imports Microsoft.Office.Interop.Excel
3+
4+
Public Module MyCommands
5+
' We make a command macro that can be run by:
6+
' * Pressing the quick menu under the Add-ins tab
7+
' * Pressing the shortcut key Ctrl + Shift + D
8+
' * Typing the name into the Alt+F8 Macro dialog (add-in macros won't we shown on this list, though)
9+
<ExcelCommand(MenuName:="$projectname$", MenuText:="Dump Data", ShortCut:="^D")>
10+
Public Sub DumpData()
11+
' We always get the root Application object with a call to ExcelDnaUtil.Application
12+
' If we reference both Window Forms, and the Excel interop assemblies,
13+
' we need to be a bit careful about which 'Application' we mean.
14+
Dim app As Application = CType(ExcelDnaUtil.Application, Application)
15+
Dim newBook = app.Workbooks.Add()
16+
17+
' While .NET arrays and collections are 0-based,
18+
' COM collections like Workbook.Sheets are 1-based.
19+
' We could also say newBook.Sheets["Sheet1"].
20+
Dim targetRange As Range = newBook.Sheets(1).Range("A1:C2")
21+
22+
' Set values to a range as a 2D object[,] array.
23+
Dim newValues As Object(,) = New Object(,) {
24+
{"One", 2, "Three"},
25+
{True, Date.Now, ""}}
26+
targetRange.Value = newValues
27+
28+
' Apply some formatting, so that the time is displayed correctly
29+
Dim dateCell As Range = targetRange.Cells(2, 2)
30+
dateCell.NumberFormat = "hh:mm:ss"
31+
End Sub
32+
End Module

ExcelDNAVSExtension/VBProjectTemplate/ProjectTemplate.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ $endif$
6666

6767
<ItemGroup>
6868
<Compile Include="MyFunctions.vb"/>
69+
<Compile Include="MyCommands.vb"/>
6970
<Compile Include="My Project\AssemblyInfo.vb"/>
7071
<Compile Include="My Project\Application.Designer.vb">
7172
<AutoGen>True</AutoGen>

ExcelDNAVSExtension/VBProjectTemplate/VBProjectTemplate.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<ItemGroup>
6868
<Compile Include="My Project\AssemblyInfo.vb" />
6969
<None Include="MyRibbon.vb" />
70+
<None Include="MyCommands.vb" />
7071
<Compile Include="Settings1.Designer.vb">
7172
<AutoGen>True</AutoGen>
7273
<DesignTimeSharedInput>True</DesignTimeSharedInput>

ExcelDNAVSExtension/VBProjectTemplate/VBProjectTemplate.vstemplate

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<TemplateContent>
2020
<Project File="ProjectTemplate.vbproj" ReplaceParameters="true">
2121
<ProjectItem ReplaceParameters="true" OpenInEditor="true" OpenOrder="10">MyFunctions.vb</ProjectItem>
22+
<ProjectItem ReplaceParameters="true">MyCommands.vb</ProjectItem>
2223
<ProjectItem ReplaceParameters="true">MyRibbon.vb</ProjectItem>
2324
<ProjectItem ReplaceParameters="true" TargetFileName="RibbonResources\Ribbon.xml">Ribbon.xml</ProjectItem>
2425
<ProjectItem TargetFileName="RibbonResources\Image1.png">Image1.png</ProjectItem>
@@ -44,6 +45,7 @@
4445
<packages repository="extension" repositoryId="f1b3b55f-9bdc-4e9a-802c-84b329e36587">
4546
<package id="ExcelDna.Integration" version="1.1.0" />
4647
<package id="ExcelDna.AddIn" version="1.1.1" />
48+
<package id="ExcelDna.Interop" version="14.0.1" />
4749
</packages>
4850
</WizardData>
4951
</VSTemplate>

0 commit comments

Comments
 (0)