Excel加载项被错误地呈现 [英] Excel Add-in being wrongly rendered

查看:125
本文介绍了Excel加载项被错误地呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我开发了Word,PowerPoint和Excel的加载项。我的加载项有一个功能区,其中一个按钮因文档的上下文而发生变化。


正如我在文档中看到的,加载项是基于应用程序的加载项,意味着功能区是同一应用程序中打开的所有文档的功能区。但是,我注意到刷新功能区的按钮仅影响应用程序
的活动窗口(这正是我需要的)。我只是注册了"Application.WindowActivate"。事件并根据当前活动文档重新显示按钮。


问题是在Excel中,此方法对我来说无法正常工作。每当我在2个Excel工作簿(在Office 2013中位于不同的窗口中)之间切换焦点时,两个工作簿都会重新绘制按钮的新值。如果我通过选择窗口的标题栏切换焦点
,一切都按预期工作,但如果我通过单击单元格区域更改焦点,则两个按钮都会刷新。


I我创建了一个虚拟加载项来演示问题:


        Ribbon1 m_Ribbon = new Ribbon1();
$


        private void ThisAddIn_Startup(object sender,System.EventArgs e)

        {

            Application.WindowActivate + = new Excel.AppEvents_WindowActivateEventHandler(Application_WindowActivate);

        }


        void Application_WindowActivate(Excel.Workbook Wb,Excel.Window Wn)

        {

            m_Ribbon.group1.Label = m_Ribbon.group1.Label +" 1";

        }


        protected override Microsoft.Office.Tools.Ribbon.IRibbonExtension [] CreateRibbonObjects()

        {

            m_Ribbon = new Ribbon1();

           返回新的Microsoft.Office.Tools.Ribbon.IRibbonExtension [] {m​​_Ribbon};

        } 


如果您运行此代码,您会注意到在两个窗口上按钮的标签都更新为"1"。请注意,如果您打开了超过2个Excel工作簿,则仅刷新最后2个工作簿。此外,如果焦点从一个工作簿
移动到另一个应用程序,然后再移动到第二个工作簿,则一切都按预期工作。



<请你帮忙吗?



谢谢


Misha

解决方案

你好Misha,


我用你的代码做了测试,但是我得到了错误Application_WindowActivate中的代码"对象引用未设置为对象的实例"。您能否分享一下重现问题的详细步骤?


根据您的描述,您似乎想在运行时更改组标签。但是为什么要在Application_WindowActivate中更改它?在我的选项中,当激活任何工作簿窗口时,将触发此事件。这意味着当您的工作簿激活
时,您的组标签会发生变化,您的问题将成为预期的行为。如果没有启用测试,我无法重现超过2个excel工作簿打开,只有最后2个被刷新。



对于一般性建议,我建议您使用getLabel调用来设置标签。

#Customizing the 2007 Office Fluent Ribbon for Developers(第3部分,共3部分)

https://msdn.microsoft.com/en-us/library/aa722523%28v=office.12%29.aspx?f=255&MSPPError=-2147217396


最好的问候,


Edward


Hi,

I've developed Add-ins for Word, PowerPoint and Excel. My Add-in has a Ribbon with one button that changes due to the context of the document.

As I read in the documentation the Add-in is an application based Add-in, means that the ribbon is one for all documents opened in the same application. However, I noticed that refreshing the button of the Ribbon affects only the active window of the application (which is exactly what I need). I simply register the "Application.WindowActivate" event and repaint the button according to the active document at the moment.

The issue is that in Excel this method doesn't work properly for me. Whenever I switch focus between 2 Excel workbooks (which in Office 2013 are situated in separate windows), both workbooks are repainted with the new value of the button. If I switch focus by selecting the title bar of the window, everything works as expected but if I change focus by clicking on the cells area, both buttons are refreshed.

I've created a dummy Add-in to demonstrate the issue:

        Ribbon1 m_Ribbon = new Ribbon1();

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(Application_WindowActivate);
        }

        void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
        {
            m_Ribbon.group1.Label = m_Ribbon.group1.Label + "1";
        }

        protected override Microsoft.Office.Tools.Ribbon.IRibbonExtension[] CreateRibbonObjects()
        {
            m_Ribbon = new Ribbon1();
            return new Microsoft.Office.Tools.Ribbon.IRibbonExtension[] { m_Ribbon };
        } 

If you run this code, you'll notice that on both windows the label of the button is updated with "1". notice that if you have more then 2 Excel workbooks open, only the last 2 touched are refreshed. In addition if the focus is moved from one workbook to another application and only then to the second workbook, everything works as expected.

Can you please assist?

Thanks

Misha

解决方案

Hi Misha,

I made a test with your code, but I got error "Object reference not set to an instance of an object" when the code in Application_WindowActivate. Could you share us details steps to reproduce your issue?

Based on your description, it seems you want to change the group label at runtime. But why did you want to change it in Application_WindowActivate? In my option, this event would fire when any workbook windows is activated. It means when you workbook is activated, your group label would change, and your issue would be expected behavior. Without enable testing it, I could not reproduce more than 2 excel workbooks open, only the last 2 touched are refreshed.

For a general suggestion, I suggest you use getLabel call to set the label.
# Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
https://msdn.microsoft.com/en-us/library/aa722523%28v=office.12%29.aspx?f=255&MSPPError=-2147217396

Best Regards,

Edward


这篇关于Excel加载项被错误地呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆