简体   繁体   English

VSTO Excel Com 添加功能区未加载

[英]VSTO Excel Com Add In Ribbon Not Loading

I'm using VS 2010 to develop an Excel 2007 COM Add-In.我正在使用 VS 2010 开发 Excel 2007 COM 加载项。 Because it's a VS 2010 Office Project, it targets the .NET 4.0 Client Framework.因为它是一个 VS 2010 Office 项目,所以它针对 .NET 4.0 客户端框架。 I've added a new Ribbon (XML) item called MyRibbon, so default filenames of ThisAddIn.cs, MyRibbon.cs, and MyRibbon.xml.我添加了一个名为 MyRibbon 的新功能区 (XML) 项目,因此默认文件名是 ThisAddIn.cs、MyRibbon.cs 和 MyRibbon.xml。

Everything builds fine.一切都很好。 It publishes with a .vsto extension.它以 .vsto 扩展名发布。 When I install the add-in (via the provided Setup.exe) it takes in Excel as being installed, and is checked in the COM Add-Ins list.当我安装加载项(通过提供的 Setup.exe)时,它会在 Excel 中安装,并在 COM 加载项列表中进行检查。 It also is designated to load on start up.它也被指定在启动时加载。 However, either opening Excel first or opening an Excel file does not add the tab to the ribbon.但是,无论是先打开 Excel 还是打开 Excel 文件,都不会将选项卡添加到功能区。

I can tell the Add-In loads because it puts "COM add-in loaded" in the first cell of the first sheet.我可以告诉加载项加载,因为它将“COM 加载项加载”放在第一个工作表的第一个单元格中。 It appears as though CreateRibbonExtensibilityObject() is not getting called.看起来好像 CreateRibbonExtensibilityObject() 没有被调用。

Does anyone have any ideas, or could tell me how to display any error messages that might be getting buried?有没有人有任何想法,或者可以告诉我如何显示可能被掩埋的任何错误消息?

Details below.详情如下。

I've added the override of CreateRibbonExtensibilityObject():我添加了 CreateRibbonExtensibilityObject() 的覆盖:

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

MyRibbon.xml looks like this, three buttons in a group inside of a tab: 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 looks like this: 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
    }
}

Finally, ThisAddIn.cs looks like this:最后,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
    }
}

I encountered the same problem.我遇到了同样的问题。 I had an incorrect path to the XML file and it was returning empty:我有一个不正确的 XML 文件路径,它返回空:

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

Hard coding a Namespace as a string is not a good idea, mainly because they are not refactor-able as well as the helpful comments below.将命名空间硬编码为字符串不是一个好主意,主要是因为它们不能重构以及下面的有用注释。

You should remove your override of CreateRibbonExtensibilityObject.您应该删除对 CreateRibbonExtensibilityObject 的覆盖。 By default, this is implemented by ThisAddIn's base class, and calls CreateRibbonObjects.默认情况下,这是由 ThisAddIn 的基类实现的,并调用 CreateRibbonObjects。 You can either override CreateRibbonObjects (this should return an array of all of your Ribbon objects), or just let the default implementation of CreateRibbonObjects do its thing (which is to say, reflect over the entire assembly everytime that your addin starts up).您可以覆盖 CreateRibbonObjects(这应该返回所有 Ribbon 对象的数组),或者只是让 CreateRibbonObjects 的默认实现做它的事情(也就是说,每次您的插件启动时反映整个程序集)。

You can read more about how all of this ties together in this blog post您可以在这篇博文中阅读有关所有这些如何联系在一起的更多信息

I have the same problem when trying to change the default TabAddIns.我在尝试更改默认 TabAddIns 时遇到了同样的问题。 What i see is that idMso is for office bar tabs and id is for new tabs.我看到的是 idMso 用于办公室栏标签,而 id 用于新标签。 The following works for me.以下对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行时添加Excel功能区控件(VSTO 2005SE) - Add Excel ribbon controls at runtime (VSTO 2005SE) Excel VSTO功能区DropDownItem索引为-1 - Excel VSTO ribbon DropDownItem index to -1 VSTO Excel保留功能区状态 - VSTO Excel preserve Ribbon state C# VSTO - 缓慢加载 Outlook 加载项(由 Ribbon.xml 引起?) - C# VSTO - Slowly loading Outlook Add-in (caused by Ribbon.xml?) 部署VSTO后Excel COM加载项崩溃 - Excel COM-Add-in crash after deploying VSTO 添加到 Excel 的 VSTO 的功能区是 RibbonBase 类型而不是 IRibbonExtensibility - The ribbon added to a VSTO for Excel is of type RibbonBase and not IRibbonExtensibility 将控件添加到Office(VSTO)中的现有功能区组 - Add controls to existing ribbon group in Office (VSTO) VSTO Ribbon Combo Box 动态添加项目 - VSTO Ribbon Combo Box Add items Dynamically 在vs2012 vsto解决方案中用clickonce安装后无法通过excel附加功能区按钮打开excel模板文件 - Can't open the excel template file by excel add-in ribbon button after install with clickonce in vs2012 vsto solution 如何使用嵌入式 WPF UserControl(从 Excel VSTO 加载项的功能区实例化)从 WinForm 处理 Windows 缩放? - How can I handle Windows scaling from a WinForm with an embedded WPF UserControl (which was instantiated from an Excel VSTO add-in's ribbon)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM