2013-04-04 110 views
0

我开发了Excel插件到Visual Studio 2012.使用项目Excel 2010加载项。在该项目中创建了一个功能区(XML):Excel插件。缺少功能区(XMl)

[ComVisible(true)] 
public class PricelistRibbon : Office.IRibbonExtensibility 
{ 
    private Office.IRibbonUI _ribbon; 

    public PricelistRibbon() 
    { 
    } 

    public event EventHandler ClickButtonRibben; 

    protected virtual void OnClickButtonRibben() 
    { 
     var handler = ClickButtonRibben; 
     if (handler != null) handler(this, EventArgs.Empty); 
    } 

    #region IRibbonExtensibility Members 

    public string GetCustomUI(string ribbonID) 
    { 
     return GetResourceText("ProcessingPricelist.PricelistRibbon.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) 
    { 
     _ribbon = ribbonUI; 
    } 

    public void OnAction(Office.IRibbonControl control) 
    { 
     OnClickButtonRibben(); 
    } 

    #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丝带的工作是这样的:

public partial class ThisAddIn 
{ 
    private void ThisAddIn_Startup(object sender, EventArgs e) 
    { 
    } 

    private void ThisAddIn_Shutdown(object sender, EventArgs e) 
    { 
    } 

    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject() 
    { 
     var pricelistRibbon = new PricelistRibbon(); 
     pricelistRibbon.ClickButtonRibben += PricelistRibbonOnClickButtonRibben; 
     return pricelistRibbon; 
    } 

    private void PricelistRibbonOnClickButtonRibben(object sender, EventArgs eventArgs) 
    { 
     var control = sender as Office.IRibbonControl; 
     if (control == null) return; 
     try 
     { 
      Application.ScreenUpdating = false; 

      switch (control.Id) 
      { 
       case "customButton1": 
        CreateSpecification(); 
        break; 
       //.............. 
      } 
     } 
     catch (Exception throwedException) 
     { 
      MessageBox.Show(throwedException.Message, @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
     finally 
     { 
      Application.ScreenUpdating = true; 
     } 
    } 

    private void CreateSpecification() 
    { 
    } 

    #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() 
    { 
     Startup += ThisAddIn_Startup; 
     Shutdown += ThisAddIn_Shutdown; 
    } 

    #endregion 
} 

接下来,我通过的ClickOnce部署该插件。安装MS Office 2007. Addin已成功部署。在这些设置中你可以看到安装了Excel插件,但在磁带上看不到我的TabControl。 当磁带出现TabControl时,如果我创建功能区(可视化设计器)而不是Ribbon(xml)。我该如何解决它?

+0

请发布您的Ribbon.xml – Kiru 2013-04-04 13:30:12

回答

2

这可能是因为ribbon.xml中使用的名称空间在Office 2010和2007功能区中不同。 Office 2007的ribbon.xml需要像

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2006/01/customui"> 

命名空间,其中在2010你将不得不使用

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui"> 

建议创建两个ribbon.xml和代码做一些像下面

protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject() 
{ 
     majorVersion = Globals.ThisAddIn.Application.Version.Split(new char[] { '.' })[0]; 
     if (majorVersion == 12) //office 2007 
     { 
      return new Ribbon2007(); 
     } 
     else if (majorVersion >= 14) //office 2010 
     { 
      return new Ribbon2010(); 
     } 


} 


[ComVisible(true)] 
public class Ribbon2007: Office.IRibbonExtensibility 
{ 
    public string GetCustomUI(string ribbonID) 
     { 
        var ribbonXml = GetResourceText("Ribbon2007.xml");     

    } 
} 


[ComVisible(true)] 
public class Ribbon2007: Office.IRibbonExtensibility 
{ 
    public string GetCustomUI(string ribbonID) 
     { 
      var ribbonXml = GetResourceText("Ribbon2010.xml");     

    } 
}