VSTO & .NET & Excel

March 29, 2012

VLOOKUP Week; Using VLOOKUP in VB

Filed under: .NET & Excel, COM Add-ins, Excel, VSTO & Excel, XLLs — Dennis M Wallentin @ 3:49 pm

Bill Jelen, MrExcel, has marked this week to honor the VLOOKUP function in MS Excel. Around the online Excel development community You will find contributions, usually blog posts, that gives various aspects for using or not VLOOKUP.

I thought it would be interesting to create a case and see how VLOOKUP can be part of  VB-solutions. Actually, for several years ago I did similar solutions but with classic VB (VB6) for a customer. Imagine that we have a price list for our products. Because of the frequent price updates the company has decided to store it in a MS Excel workbook. To keep it very simple for the sales staff they use a standalone VB-solution where they select products, retrieve the price for each product item and add some additional information manually to get a total price.

As most Excel developers should know, we can acquire data from workbooks in several ways. The most common approach is to treat the workbook as a flat database table. Technically we either use classic ADO or DAO to connect to the database table and using a SQL query to get the wanted records. This is a smooth solution but it assumes that the ADO/DAO library is available on all computers that will use the solutions. To avoid a situation where we must check if the library is installed or not we can use a solution where we automate Excel and execute the VLOOKUP function to get the wanted data. Practical we get a robust solution that is not depended on ADO/DAO and when working with small data sets the performance is fully acceptable.

The following screen shot shows how the price list workbook looks like:

As we can see it’s a simple table that is manually updated.

The next screen shot shows the VB-solution in action. The product list is picked up from a storage system. Sales person starts to select the products he/she wants to calculate the prices for, retrieve the prices, add number of items and then get a total price per product. The total prices including freight etc. is done in another part of the VB-solutions:

As You can see it’s a simple tool to work with. The last piece of the solution is the code I use to retrieve the prices and populate the DatagridView with.

The following code is in use and is executed when clicking on the Get Price button control:

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices

Public Class Form1

Private Sub btnVLOOKUP_Click(sender As System.Object, e As System.EventArgs) _
Handles btnVLOOKUP.Click

Dim Location As String = Environment.GetEnvironmentVariable("USERPROFILE") _
.ToString() + "\Documents\Product Lists\"

Dim File As String = "'" + Location + "[P_List_2011.xlsx]ProductList'!R1C2:R78C3,2,0"

Dim itemChecked As Object

If Me.CheckedListBox1.CheckedItems.Count = 0 Then
MessageBox.Show("Please select at least one product.")
Exit Sub
End If

Dim xlApp As New Excel.Application

Try

For Each itemChecked In Me.CheckedListBox1.CheckedItems

Dim Price As String = String.Empty

'Here we do the lookup based on the selected product name.
Price = xlApp.ExecuteExcel4Macro("VLOOKUP(""" + itemChecked.ToString() _
+ """," + File + ")")

Dim rowValues As String() = New String() {itemChecked.ToString, Price.ToString}

'Add the row with data to the data grid,
Me.dgwPList.Rows.Add(rowValues)
Me.dgwPList.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

Next

Catch ex As Exception

MessageBox.Show(ex.ToString())

Finally

xlApp.Quit()

End Try

'Cleaning up.
If Not xlApp Is Nothing Then
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
xlApp.Quit()
End If

Marshal.FinalReleaseComObject(xlApp)

End Sub

Edit: I intentionally used the XLM macro program to show that we can still use it and with VB.NET. I thought that someone would point out that it can also be done by using xlApp.WorksheetFunction.VLookup. My friend Ken Puls, ExcelGuru, finally pointed it out.
Kind regards,
Dennis

February 14, 2012

From VBA to VB.NET using ExcelDna

Filed under: .NET & Excel, Excel, Tools, XLLs — Dennis M Wallentin @ 1:25 am

This is a notification about a recent published article by Patrick O’Beirne on his blog. Patrick has compiled a list of resources, for native Excel developers, to develop User Defined Functions (UDFs) with ExcelDna in VB.NET.

For some years ago several developers in the online Excel community made quite often critical comments and rant a lot about using .NET for developing Excel solutions.

Today the opinions are slowly changing to a more positive review of .NET which I welcome very much.

Here is the link to Patrick’s article: From VBA to VB.NET using ExcelDna

Of course, it exist other tools as well. For instance Add-in Express for Microsoft Office & .NET, which is a RAD tool that allows us to develop many different .NET solutions for Excel. Personally I prefer this tool than any other, it does all the hard work and allow me to set focus on the core in .NET solutions. It’s also the most powerful RAD tool and is unique.

Kind regards,
Dennis

February 4, 2012

The Great Microsoft Office Portal

Filed under: .NET & Excel, .NET Books, Excel, Valentina DB, Valentina Office Server, VSTO & Excel, XLLs — Dennis M Wallentin @ 4:46 pm

As most of us already know, Microsoft has for the last couple of years built up an enourmus  giant knowledge base about Microsoft Office on the internet. As a consequence it has also become more difficult to navigate around and find the wanted information we are looking for.

However, today I discovered the Great Portal to Microsoft Office knowledge Base. Instead of just saving the URL to my local computer I thought I would make it more available by publishing it here:

Office Development Site Map

One of the key people for the ongoing publishing of Microsoft Office knowledge is Erika Erhli. I have previously related to her so this is a (very good) reminder:

Adventures with Office Products & Technologies

Do You remember how it was during the 90′s? At that time it was rather easy to be updated on a numbers of softwares from Microsoft. Today the softwares have become much more, more complex and more advanced and on top of that the release cycles have become faster.

So I’m glad that I, at least, can keep myself updated of Microsoft Excel. However, in the future we may only be able to keep up with the rapid development for one platform that Microsoft Excel is used on. What do You think?

Kind regards,
Dennis

September 9, 2010

Office 2010 PIA Redistributable available for download!

Filed under: .NET & Excel, COM Add-ins, Excel, VSTO & Excel, XLLs — Dennis M Wallentin @ 6:23 pm

I know that some developers have been waiting to get access to Office 2010 PIA Restributable. Although we can create our own IA some developers prefer to use the officially PIA from Microsoft.

Today when I checked it up I was pleasant surprise to find out that the PIA package is now available for download.

Click on the following URL to get to the page from where You can download it: Microsoft Office 2010: Primary Interop Assemblies Redistributable

Enjoy!

Kind regards,
Dennis

August 3, 2010

Creating XLLs with Add-In Express.NET – Part 2

Filed under: .NET & Excel, Tools, XLLs — Dennis M Wallentin @ 1:12 am

This is the second article about creating XLLs with Add-In Express.NET and it will focus on creating a Setup package for our solution. The first article about it can be viewed here.

Creating a Setup package is also very easy and straightforward with the Add-in Express.NET tool; we add a Setup project to the solutions. Select the command Project from the main menu and next select the command Create Setup Project as the following screen shot shows:

The above command will start the Setup Project Wizard as the below screen shot shows:

In the next step in the wizard we customize the Setup package additionally which the following screen shot also shows:


In the next step the Setup project is created and added to the Solution Explorer. In the Solution Explorer we can find out which files that have been added to the Setup project which also the following screen shot shows:

The file adxloader.dll.manifest includes additional settings that can be manipulated if required:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <assemblyIdentity name="XLDennisXLLs, PublicKeyToken=a51ebb7add3c8218" xllClass="XLDennisXLLs.XLLModule" />
 <loaderSettings generateLogFile="true" shadowCopyEnabled="true" privileges="user" configFileName="app.config" />
</configuration>

The next file, adxloader.XLDennisXLLs.dll, is the loader on the x86 platform while the file thereafter is the loader on the x64 platform. In other words we can with the Add-in Express.NET tool target both platforms with only one solution. The last added file, adregistrator.exe,  creates all the required entries in the Windows Registry and also remove them when uninstalling the solution.

The next screen shot shows the updated properties of the Setup project, it’s accessible by selecting the Setup project in the Solution Explorer and then hit the F4-button:

By reviewing the File System (on the Target Machine) in the Setup project we can see that the following files will be added to the targeting computers:


Keep in mind that we, upon creating the solution, decided to include version-neutral IAs to the Setup package. That’s why the Interop files are included. The Extensibility.dll file includes the IDTExtensibility2 Interface so the file is vital for solutions that make use of it.

Next, when reviewing the Custom Actions in the Setup project it shows that the file adregistrator.exe is used in all the actions which the below screen shot also shows:


When reviewing the basic Launch Conditions it only includes the requirement of having .NET Framework 4.0 installed on the targeting computers which the below screen shot shows. In addition, we can add our own launch conditions to the Setup project which makes it very flexible.

In order to create the Setup package we can select the command Build on the main menu and then select the command Build Solution as the following screen shot also shows:


After running the build command the Setup package has been created and it includes two files; a setup.exe file and a MSI file. The following screen shot shows it as well:


Now we have a Setup package that can be deployed in order to install our solution.

When we install the solution by executing the setup.exe file the installation process starts and shows the following screen shots to the end users:

If we look in the list of installed softwares on the targeting computers we will find our solution among them as the below screen shot reviews:

Based on the stepwise walk-through we can conclude the following:

  • It’s very easy and simple to create a Setup package with the Add-In Express.NET tool.
  • It’s also flexible and time saving and it requires little effort to get a professional Setup package by using the Add-In Express.NET tool.

In the next article, which is also the last one on the subject, I will discuss the pros and cons with using Add-in Express.NET for creating XLLs solutions.

Kind regards,
Dennis

July 8, 2010

Creating XLLs with Add-In Express.NET – Part 1

Filed under: .NET & Excel, COM Add-ins, Excel, XLLs — Dennis M Wallentin @ 5:00 pm

Introduction

Recently I had a discussion with a local colleague and friend about using third-party developing tools. He prefers to not use any other tools than Excel and VS.NET. In that way he says he gets 100 % control of the developing process.

One of my main targets is to reduce the overall time the developing process takes. To achieve it I’m also prepared to use any third-party tools that can support me. After all, we are always depended on some softwares in order to develop solutions. So even when I’m using third-party tools I still feel that I have full control. By reducing the developing time my clients will also be saving money as their costs will be reduced.

Today it exist rapid application (RAD) tools that allow us to set focus on the core part in the developing process. One of them is Add-in Express 2010 for Office and .NET which I have found to be a good companion.

Of course, when using RAD tools and third-party controls it’s important that we can legally redistribute any assemblies that are required in order to run the solutions. Another critical aspect is that it must be easy to update the present versions when the vendors release new versions.

This article is the first of a series of total three articles that will cover how to create XLL add-ins with Add-in Express 2010 for Office and .NET. With the tool we can also easily create solutions for managed COM Add-ins, managed Automation Add-ins, Smart Tags and Real Time Data (RTD) Servers. The RAD tool is fully integrated in VS.NET and it works well with the latest versions of VS.NET and of Excel, including the 64-bit  version of Excel 2010.

In this first article I will describe how we easily can create an XLL add-in with the RAD tool. The second article will discuss the deployment of the XLL add-in. The final article will set focus on the pros and cons of using a RAD tool like Add-in Express .NET 2010 for developing XLL add-ins. I hope the series will shed some light on how we can develop solutions with the RAD tool.

The Add-in Express 2010 for Office and NET has recently been released. However, when I developed the first case here I used the beta version of the RAD tool together with Windows 7 x86, VS.NET 2010 and Office 2010 Professional.

Creating a XLL solution with Add-in Express 2010 .NET
Let us launch VS 2010 and start to create the project for the XLL add-in as the following screen shot shows:


Here we will create an XLL add-in so we select the “ADX XLL Add-in” template in the dialog. As we can see in the screen shot we will be using the 4.0 version of the framework. It should also be noted that we will, by selecting this template, create an isolated XLL add-in. I will discuss this more in detail in the third and final article. As we can see from the next screen shot (see below) the wizard is started which will guide us through the process of creating the framework for the XLL add-i n. In the first step we select the programming language we want to use and we also select the Excel version we intend to target (and above) as the below screen shot shows:


In the next step we either chose to create a new strong name key file or use an existing key file for the project as the following screen shot shows:

In the next step the project solution is created and the following screen shot shows its main parts in the Solution Explorer:

That is all! Now we can start to create our UDFs. However, in this case we will be using the example UDF that is created in the project, AllSupportedExcelTypes, so we will move to the next step. In order to debug the UDF we need to register the project on the development computer which is done by selecting the command Build from the main menu in VS IDE and select the command Register ADX Project as the following screen shot shows:

The next step is to add a description for the XLL add-in as well as a description of the UDF. To do so we need to open the Visual Designer for the main Component Class (XLLModule.vb) by right clicking on the class in the Solution Explorer and from the menu select the command View Designer as the below screen shot also shows.

In the Designer View we right click to access the menu from where we can add the wanted component. Here we will add the Excel Function Category component so we select the command Add Excel Function Category as the following screen shot shows:


When we have added the component to the Visual Designer we can access its properties and add a category name together with a description for the UDF as the following screen shot shows:


Next we can add a description for the only required argument in the UDF as the following screen shot also shows:


Now we have walked through all the basic steps in order to create a XLL add-in project and it’s now time to see it in action.

The following screen shot shows that the add-in is now available and active on the development computer:


When we want to access the UDF from the Insert Function wizard we see that it has its own category, XLDennis UDFs, and when selecting the UDF we see its description as the following screen shot shows as well:

When the UDF is selected and we click on the OK button the UDF’s argument(s) is showed as the following screen shot show:

When clicking on the OK button Excel inserts the UDF and show the result as the following screen shot shows.

In my opinion it’s very easy to create XLL add-ins with Add-in Express 2010 for Office and .NET which I also hope the walkthrough here has showed. In the next article I will discuss the deploymen of XLL add-ins t in more detail.

Kind regards,
Dennis

Note: The second article in the series can be found here.

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.