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,044 times
Add to favorites
2/19/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
VB.NET
// Copyright © Microsoft Corporation. All Rights Reserved. 
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using NorthwindEmployees.NorthwindServiceReference;

namespace NorthwindEmployees
{
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
    public partial class ThisWorkbook : NorthwindEmployees.IThisWorkbook
    {
        private Uri ServiceURI = new Uri("http://localhost:60931/Northwind.svc/");
        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            this.Application.CalculateFull();
        }

        protected override object GetAutomationObject()
        {
            return this;
        }

        public string GetFirstName(int id)
        {
            NorthwindEntities ctx = new NorthwindEntities(ServiceURI);
            string FirstName = "<Invalid Employee ID>";
            try
            {
                FirstName = (from e in ctx.Employees where e.EmployeeID == id select e.FirstName).Single();
            }
            catch( InvalidOperationException e)
            {
                //Employee ID doesn't exist
            }
            return FirstName;
        }

        public string GetLastName(int id)
        {
            NorthwindEntities ctx = new NorthwindEntities(ServiceURI);
            string LastName = "<Invalid Employee ID>";
            try
            {
                LastName = (from e in ctx.Employees where e.EmployeeID == id select e.LastName).Single();
            }
            catch (InvalidOperationException e)
            {
                //Employee ID doesn't exist
            }
            return LastName;
        }
        public string GetNotes(int id)
        {
            NorthwindEntities ctx = new NorthwindEntities(ServiceURI);
            string Notes = "<Invalid Employee ID>";
            try
            {
                Notes = (from e in ctx.Employees where e.EmployeeID == id select e.Notes).Single();
            }
            catch (InvalidOperationException e)
            {
                //Employee ID doesn't exist
            }
            return Notes;
        }

        private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisWorkbook_Startup);
            this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
        }

        #endregion

    }
}