Archive for August, 2009

Visual Studio 2008 and SQL Server 2008 Express – needs SP1

August 27, 2009

Need to up install SP1 for VS 2008 to get support for SQL Server 2008

Also, if using the web download tool from Microsoft, it may install the 64bit version (a problem if
you use 32bit os!!). In which case, and error message of: Connections to SQL Server files (*.mdf) require SQL Server Express 2005 to function properly,
appears. To work around this problem, uninstall the 64-bit version of SQL Server Express 2008, and then install the 32-bit version of SQL Server Express 2008
(great advice from the kb on this this article

Advertisements

Ambiguity using Worksheet Activate

August 26, 2009

Activate is both a method and an event of Application.Excel, so the Activate keyword must be cast to either an event or method.

Examples (based on a VSTO Excel 2007 Add-in):

Method

Excel.Workbook workbook = Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.Worksheet worksheet = workbook.Sheets["Sheet2"] as Excel.Worksheet;
//The next line will throw a warning
//worksheet.Activate(); 
//Need to cast as a method
((Excel._Worksheet)worksheet).Activate(); 
//or 
((Microsoft.Office.Interop.Excel._Worksheet)worksheet).Activate() ;

Event

((Excel.DocEvents_Event)worksheet).Activate += 
                new Microsoft.Office.Interop.Excel.DocEvents_ActivateEventHandler(
                    Worksheet_Activate);

VSTO Power Tools – Control state between CTP and Ribbon

August 25, 2009

Ver 1.0 available from here

VSTO_PTRibbonIDs.exe gives access to the Office images that can be associated with Ribbon controls.

VSTO_PT.exe – among other things, has reusable code to control the state between CTP (custom task pane) and Ribbon e.g. CTP visible on one open document, but not on another etc.

Documentation and how to here

Visual Studio Tools for Office 2007 Book, Eric Carter and Eric Lippert – Get the code

August 18, 2009

This book, which is probably the best VSTO book for excel programmers (but not for beginners), does not come with a CD, but I only noticed yesterday that you can view the book online once you have purchased a print copy.

On the last page there is an activation key which can be used at http://www.informit.com/safarifree. Very handy for copying the code.

VBA to C#: Public, Private and Static Variables in C#

August 17, 2009

Was answering a forum question by a VBA programmer about public, private and static variables in C#. (Create a New>Project in Visual Studio 2008, and choose Visual C#>Console Application, name it PublicPrivateStatic_Test, and paste in the code below.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PublicPrivateStatic_Test
{
    class Program
    {
        public int publicInt = 1;
        private int privateInt = 2;
        static private int privateStaticInt = 3;

        public int getPrivateInt()
        {
            return privateInt;
        } 

        public void setPrivateStaticInt(int i)
        {
            privateStaticInt = i;
        }
        public int getPrivateStaticInt()
        {
            return privateStaticInt;
        }
    }

    class OtherProgram
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Program q = new Program();

            Console.Write("Public int is {0}", p.publicInt);
            //Prints 1, as it is public

            //Console.Write("Private int is {0}", p.pirvateInt);
            //Wont Compile as it is a private variable.
            //Need to use p.getPrivateInt(). Prints 1.
            Console.Write("\nPrivate int is {0}", p.getPrivateInt());

            //Console.Write("Private int is {0}", p.pirvateStaticInt);
            //Wont Compile as it is a private variable.
            //Need to use getPrivateStaticInt(). Prints 3.
            Console.Write("\nPrivate Static int is {0}", p.getPrivateStaticInt());

            p.setPrivateStaticInt(999);
            Console.Write("\nPrivate Static int is now {0}", q.getPrivateStaticInt());
            //Will print 999 instead of 3. Think of a static variable
            //as only existing once, and p and q both reference it. If
            //instance p changes it, then it is also changed for instance q.

            //Keeps the console open until any-key pressed
            Console.ReadLine();
        }
    }
}

Console output:

Console output

Console output

VSTO Design-Time Adaptor error

August 17, 2009

When working with VSTO and creating a new Excel 2007 Workbook project, I got an error that the VSTO Design-Time Adaptor for Excel 2007 was corrupt or unavailable. Just before repairing the install, as is advised, I had a quick check of the Task Manager, and noticed that an Excel.exe process was still running. Ended this process, and hey presto, it fixed the problem.

It appears that sometimes when debugging/running your project, if it fails or stops before the Excel app gets closed by your code, the design time adaptor hangs. This happened to me because an Automation Excetuable had left an instance of Excel up and running in visible = false mode.

Hello world!

August 17, 2009

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!