Click here to Skip to main content
15,175,716 members
Articles / Programming Languages / C#
Posted 2 Apr 2012

Tagged as


21 bookmarked

Stock Quote Add-In For Excel 2007

Rate me:
Please Sign up or sign in to vote.
4.86/5 (7 votes)
26 Apr 2012CPOL4 min read
Creating an MSN-like Stock Quotes add-in for Excel 2007 using User-Defined Functions and Ribbons.

Installation Instructions

Before using the add-in you should check if the installation has been executed correctly. Open the Excel 2007 Options dialog and click the menu item Add-Ins. The list Active Add-ins should contain the " Stock Quotes Function Add-In" (COM-Add-In) as well as " Stock Quotes UI Add-In" (COM-Add-In).

Then open the Excel Add-In Manager dialog by pressing the button on the Add-Ins dialog window (see button on the bottom of the dialog). Then check if the Add-In "ParagoStockQuote.Functions" is available in the list and is marked. If not, add it to the list and mark it.


This article describes how to implement a managed stock quote add-in for Excel 2007 in C# that behaves similar to the “MSN MoneyCentral Stock Quotes” add-in you can download from Microsoft (see “Excel 2003/2002 Add-in: MSN Money Stock Quotes”). The add-in is developed from scratch, not using Visual Studio Tools for Office (VSTO).

Once the add-in is installed and registered, you can use the user-defined function (UDF) named “PSQ” to retrieve a stock quote price, date, time, and name for a given stock symbol from the Yahoo! finance service.

The following formula samples show the usage of the PSQ function:

=PSQ(B4;'PRICE') or just =PSQ(A1)to retrieve the last price
=PSQ(B4;'NAME') to retrieve the company name
=PSQ(B4;'DATE') to retrieve the last price date
=PSQ(B4;'TIME') to retrieve the last price time 

The add-in also implements a custom ribbon of the new Office “Fluent” user interface. The following two screenshots show the add-in in action using the German version of Excel:



The custom ribbon (named “Stock Quotes”) offers an Update button to retrieve (to be more precise to recalculate) all stock related formulas at once.

The main objective of this composition is to show how easy you can create an Excel 2007 add-in using the new UI elements and especially user-defined functions. Therefore, to keep this task simple and straightforward, I omitted the setup project and the usually necessary COM shim project (see explanation below) for managed Office extensions.

Office Extensions

In the course of the years, Microsoft exposed a huge number of different Office extensibility mechanisms like COM add-ins, XLL extensions, smart tags and much more, that developers could use to create their own custom solutions.

The stock quotes add-in described in this article is a COM add-in, to be more exact, it is broken into two add-ins. One add-in provides the user-defined function “PSQ”, the other one is responsible for the UI elements (ribbon). This technique allows you to use the UDF without activating the UI.

The COM add-in is an ActiveX-DLL (in-process server) that implements the IDTExensibility interface and all COM add-ins must provide an implementation of those five interface methods: OnAddInsUpdate, OnBeginShutdown, OnConnection, OnDisconnection, OnStartupComplete. In our case we just provide empty methods with no specific logic, except for OnConnection. This override saves a given reference to the host application (Excel) for later use:

public void OnConnection(object host, ext_ConnectMode connectMode, 
            object addInInst, ref Array custom)
   _excel = (Application)host;

In addition to the classic COM registration, the add-in must register with Excel or another Office application the extension is working with. To register the extension with Excel, the add-in should create a sub-key, using its ProgID as the name for the key, under the following location:


In our case the ProgIDs for the two COM add-ins are ParagoStockQuote.Functions and ParagoStockQuote.UI. The add-in registration provides more information values for Excel like a full description and a friendly name. In addition we specify the add-in LoadBehavior (0x03 for load on application start up).

In order to register the extension in the Registry, the add-in provides two static methods named RegisterFunction and UnregisterFunction. They are attributed with ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute. The CLR calls these methods once the containing assembly is registered through utilities like RegAsm.

public static void RegisterFunction(Type t)
   RegistryKey key;
   key = Registry.ClassesRoot.CreateSubKey(Functions.ClsIdKeyName + "Programmable");
   key = Registry.ClassesRoot.CreateSubKey(Functions.ClsIdKeyName + "InprocServer32");
   key.SetValue(string.Empty, Environment.SystemDirectory + @"\mscoree.dll");
   key = Registry.CurrentUser.CreateSubKey(Functions.ExcelAddInKeyName);
   key.SetValue("Description", " Stock Quotes Function Add-In for Excel 2007", RegistryValueKind.String);
   key.SetValue("FriendlyName", " Stock Quotes Function Add-In", RegistryValueKind.String);
   key.SetValue("LoadBehavior", 3, RegistryValueKind.DWord);
   key.SetValue("CommandLineSafe", 1, RegistryValueKind.DWord);

public static void UnregisterFunction(Type t)
   Registry.ClassesRoot.DeleteSubKey(Functions.ClsIdKeyName + "Programmable");
   Registry.ClassesRoot.DeleteSubKeyTree(Functions.ClsIdKeyName + "InprocServer32");

For more information about COM Interop and COM registration, I recommend the article: Understanding Classic COM Interoperability With .NET Applications.

Stock Quote Data

The add-in calls the finance portal from to retrieve the requested stock quotes. The following URL returns a comma separated string containing stock quote data:

The query string parameters are “s” for the stock symbol and “f” for the returned stock data fields. You can find a list of available fields in the documentation of the Perl module “Finance::YahooQuote” ( So, using this source it’s a matter of a few lines of code to get stock quotes (see code and section “User-Defined Functions”).

User-Defined Functions

User-defined functions are not a big deal. They are just plain methods with or without optional parameters. Optional parameters are flagged with OptionalAttribute. The method for “PSQ” looks as follows:

public object PSQ(Range Cell, [Optional] object InfoCode)
   string symbol = Cell.Value2 as string;
   string infoCode = (InfoCode is Missing) ? "PRICE" : InfoCode as string;

   if(string.IsNullOrEmpty(symbol) || string.IsNullOrEmpty(infoCode))
      throw new Exception();

   WebClient client = new WebClient();
   Stream data = client.OpenRead("" + 
                                 symbol.Trim() + "&f=sl1d1t1n");
   StreamReader reader = new StreamReader(data);
   string content = reader.ReadToEnd();

   string[] quote = content.Split(",".ToCharArray());

      case "NAME":
         return quote[4].Replace("\"", "").Replace("\r", "").Replace("\n", "");
      case "DATE":
         return Convert.ToDateTime(quote[2].Trim("\"".ToCharArray()), 
      case "TIME":
         return Convert.ToDateTime(quote[3].Trim("\"".ToCharArray()), 
      case "PRICE":
         return Convert.ToDouble(quote[1], CultureInfo.InvariantCulture);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jürgen Bäurle
Software Developer (Senior)
Germany Germany
I’m a software developer based in Germany.


Comments and Discussions

QuestionMessage Closed Pin
19-Apr-21 1:46
MemberJoshua Babashola19-Apr-21 1:46 
QuestionMessage Closed Pin
19-Apr-21 1:41
MemberJoshua Babashola19-Apr-21 1:41 
Question?Name Solved Pin
Member 113485182-Jan-15 10:59
MemberMember 113485182-Jan-15 10:59 
QuestionName Error Pin
Member 113485182-Jan-15 10:30
MemberMember 113485182-Jan-15 10:30 
Question#name error on updating Pin
Member 1134131029-Dec-14 5:22
MemberMember 1134131029-Dec-14 5:22 
GeneralMy vote of 5 Pin
Member 1133367724-Dec-14 15:10
MemberMember 1133367724-Dec-14 15:10 
QuestionNon US stock quotes Pin
Member 112174759-Nov-14 7:01
MemberMember 112174759-Nov-14 7:01 
QuestionDate, Time, and Name not working Pin
Member 1054233322-Jan-14 11:22
MemberMember 1054233322-Jan-14 11:22 
AnswerRe: Date, Time, and Name not working Pin
Member 1133714326-Dec-14 19:47
MemberMember 1133714326-Dec-14 19:47 
QuestionCan't get it to work Pin
Member 1047257817-Dec-13 8:08
MemberMember 1047257817-Dec-13 8:08 
AnswerRe: Can't get it to work Pin
Member 1077034723-Apr-14 12:23
MemberMember 1077034723-Apr-14 12:23 
GeneralRe: Can't get it to work Pin
Jürgen Bäurle8-May-14 0:28
MemberJürgen Bäurle8-May-14 0:28 
GeneralRe: Can't get it to work Pin
Member 1096392522-Jul-14 8:17
MemberMember 1096392522-Jul-14 8:17 
GeneralRe: Can't get it to work Pin
Member 1132996722-Dec-14 21:03
MemberMember 1132996722-Dec-14 21:03 
Question#NAME? Pin
Member 1028998423-Sep-13 4:08
MemberMember 1028998423-Sep-13 4:08 
AnswerRe: #NAME? Pin
Member 1037122230-Oct-13 14:37
MemberMember 1037122230-Oct-13 14:37 
GeneralRe: #NAME? Pin
Member 1132971922-Dec-14 18:44
MemberMember 1132971922-Dec-14 18:44 
GeneralRe: #NAME? Pin
taz607126-Dec-14 6:16
Membertaz607126-Dec-14 6:16 
GeneralRe: #NAME? SOLVED Pin
Member 1134131029-Dec-14 13:09
MemberMember 1134131029-Dec-14 13:09 
GeneralRe: #NAME? SOLVED Pin
Member 113485182-Jan-15 10:55
MemberMember 113485182-Jan-15 10:55 
QuestionDon't work with office 2010 pro plus 32bit Pin
silkman920-Aug-13 2:14
Membersilkman920-Aug-13 2:14 
Questioncryptographic failure when building project Pin
tophee13-Jan-13 4:26
Membertophee13-Jan-13 4:26 
AnswerRe: cryptographic failure when building project Pin
tophee13-Jan-13 5:31
Membertophee13-Jan-13 5:31 
Question64-bit Excel? Pin
billchurch7618-Dec-12 16:22
Memberbillchurch7618-Dec-12 16:22 
AnswerRe: 64-bit Excel? Pin
Jürgen Bäurle19-Dec-12 0:17
MemberJürgen Bäurle19-Dec-12 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.