VSTO Excel Com添加功能区未加载

时间:2010-08-09 17:02:58

标签: c# excel com vsto

我正在使用VS 2010开发Excel 2007 COM加载项。因为它是VS 2010 Office Project,所以它面向.NET 4.0 Client Framework。我添加了一个名为MyRibbon的新Ribbon(XML)项,因此默认文件名为ThisAddIn.cs,MyRibbon.cs和MyRibbon.xml。

一切都很好。它以.vsto扩展名发布。当我安装加载项(通过提供的Setup.exe)时,它将安装Excel,并在COM加载项列表中进行检查。它也被指定在启动时加载。但是,首先打开Excel或打开Excel文件不会将选项卡添加到功能区。

我可以告诉Add-In加载,因为它在第一个工作表的第一个单元格中加载了“COM加载项”。似乎没有调用CreateRibbonExtensibilityObject()。

有没有人有任何想法,或者可以告诉我如何显示可能被隐藏的任何错误消息?

详情如下。

我添加了CreateRibbonExtensibilityObject()的覆盖:

protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new MyRibbon();
        }

MyRibbon.xml看起来像这样,标签内的一组中有三个按钮:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab id="TabAdvComTracking" idMso="TabAdvComTrackingMso" label="Adv.com Tracking">
        <group id="groupPrepare" label="Prepare">
          <button id="GenerateNewWorkbook" idMso="GenerateNewWorkbookMso" enabled="1" size="large" onAction="GenNewWorkbook" label="Make"  />
          <separator visible="1"/>
          <button id="ClearData" idMso="ClearDataMso" enabled="1" size="large" onAction="ClearData" label="Clear" />
        </group>
        <group id="GroupDoIt" idMso="GroupDoItMso" label="Just Do It">
          <button id="CaptureIds" idMso="CaptureIdsMso" enabled="1" size="large" onAction="CaptureData" label="Eat" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

MyRibbon.cs看起来像这样:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

namespace AdvComTrackingAddIn
{
    [ComVisible(true)]
    public class MyRibbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public MyRibbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            //ribbonID when loaded into Excel should be Microsoft.Excel.Workbook
            return GetResourceText("AdvComTrackingAddIn.MyRibbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        public void GenNewWorkbook(Office.IRibbonControl control)
        {
            Excel.Workbook newWorkBook = Globals.ThisAddIn.Application.Workbooks.Add();

            Excel.Worksheet newWorkSheet = (Excel.Worksheet)newWorkBook.Worksheets.Add();
            newWorkSheet.Name = "DBTS " + GetDateRange("MMDDYYYY");


        }

        public string GetDateRange(string format){
            string day = DateTime.Now.DayOfWeek.ToString();
            int offSet = 0;
            if(day == "Sunday") offSet = 1;
            else if(day == "Monday") offSet = 0;
            else if(day == "Tuesday") offSet = -1;
            else if(day == "Wednesday") offSet = -2;
            else if(day == "Thursday") offSet = -3;
            else if(day == "Friday") offSet = -4;
            else if(day == "Saturday") offSet = -5;

            DateTime MondayStartDate = DateTime.Now.AddDays(offSet);           

            return MondayStartDate.ToString(format) + "_" + MondayStartDate.AddDays(4).ToString(format);                
        }

        public void ClearData(Office.IRibbonControl control)
        {
            Excel.Sheets wksheets  = Globals.ThisAddIn.Application.Worksheets;
            Excel.Worksheet sheet;

            for(int i = 0; i < wksheets.Count; i++){
                sheet = wksheets[i];
                if(sheet.Name.StartsWith("DBTS")){
                    sheet.get_Range("A6:H12").Clear();
                    sheet.get_Range("A16:D22").Clear();
                    sheet.get_Range("A26:D10000").Clear();
                }
                else if(sheet.Name == "Advisory.com Activity"){
                    sheet.get_Range("A4:B10000").Clear();
                    sheet.get_Range("D4:F10000").Clear();
                    sheet.get_Range("H4:J10000").Clear();
                }
                else if(sheet.Name == "Unique Hits Per URL"){
                    sheet.get_Range("A4:E10000").Clear();
                }
            }            
        }

        public void CaptureData(Office.IRibbonControl control)
        {
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion
    }
}

最后,ThisAddIn.cs看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.IO;
using System.Reflection;

namespace AdvComTrackingAddIn
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Globals.ThisAddIn.Application.get_Range("A1").Value = "COM add-in loaded";
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            GC.Collect();
        }

        protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new MyRibbon();
        }

        #region VSTO 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(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

3 个答案:

答案 0 :(得分:4)

我遇到了同样的问题。我有一个不正确的XML文件路径,它返回空:

 public string GetCustomUI(string ribbonID)
 {
    return GetResourceText("CheckThisIsTheCorrectNameSpace.Ribbon.xml");
 }

将命名空间硬编码为字符串并不是一个好主意,主要是因为它们不是可重构的,以及我们的Stackoverflow在其他人的评论中提到的有用的原因。

答案 1 :(得分:3)

您应该删除CreateRibbonExtensibilityObject的覆盖。默认情况下,这是由ThisAddIn的基类实现的,并调用CreateRibbonObjects。您可以覆盖CreateRibbonObjects(这应该返回所有Ribbon对象的数组),或者只是让CreateRibbonObjects的默认实现做它的事情(也就是说,每次你的addin启动时反映整个程序集)。 / p>

您可以在此blog post

中详细了解所有这些内容如何联系在一起

答案 2 :(得分:2)

尝试更改默认TabAddIns时遇到同样的问题。 我看到的是idMso用于办公室栏标签,id用于新标签。 以下适用于我。

<tab id="TabAdvComTracking" tag="TabAdvComTracking" label="Adv.com Tracking" visible="true" insertAfterMso="TabAddIns">