WORD:管理具有多个文档的自定义任务窗格时出现问题 [英] WORD: Problems managing custom task panes with multiple documents

查看:116
本文介绍了WORD:管理具有多个文档的自定义任务窗格时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试在多个文档的单词中使用自定义任务窗格,我用作指南 http://msdn.microsoft.com/en-us/library/bb264456.aspx#managingmultipletaskpanes_managingtaskpanesinmultipledocuments 但我无法正常工作。



我遇到了一些问题,孤立的任务窗格也没有像窗口的现有任务窗格那样被发现。因为在某些情况下我有两个窗格或没有窗格而不是一个窗格。



我看到的问题是,当我尝试访问窗格的窗口属性时,它在某些情况下无法返回和异常。



Another问题是任务窗格窗口和文档窗口等于不是真的,并且最后窗口是相同的,并且显示该窗口的多个窗格。 (我尝试使用equals方法并使用==)




我可以看到只使用一个关闭并打开多次的文档,经过一段时间我们可以看到我有一个多个任务窗格而不是一个收集并且不止一次也可以看到多个。 我多次关闭并打开同一个文档,可以对其进行一些操作。


我使用下面的方法:



private void Application_NewDocument(Word。文档 doc)


{


ShowTaskPaneForDocument(doc);


}



private void Application_DocumentOpen(Word 。文件 doc)


{


RemoveOrphanedTaskPanes();


ShowTaskPaneForDocument(doc);


}



private void ShowTaskPaneForDocument(Word。 Document doc)


< span lang = EN-GB style ="font-size:10pt; font-family:"> {


if (Application.ShowWindowsInTaskbar)


{


CustomTaskPane tpane = GetT askPaneForWindow(doc, this .Application.ActiveWindow);


if (tpane == null


{


AddTaskPane(doc);


}


else


<跨度> {


tpane.Visible = true ;


}


}


}



private void Application_DocumentChange()


{


RemoveOrphanedTaskPanes();


}



private CustomTaskPane GetTaskPaneForWindow(Word。文档 doc,Word。窗口 wn)


{


Word。 Window docWn = < span style ="color:blue"> null ;


尝试


{


docWn = doc.ActiveWindow as Word 。窗口;


}


catch


{


//无法进行无法到达的窗口


}


for int i = this .CustomTaskPanes.Count;我> 0; i - )


{


< span style ="color:rgb(43,445,175)"> CustomTaskPane tpane = this .CustomTaskPanes [i - 1];


尝试


{


Word 。窗口 tpWn = tpane.Window as Word。窗口;


if (tpane.Title == TPANE_TITLE&&


(tpWn.Equals(docWn)|| tpWn.Equals(wn)))


{


return tpane;


}


}


catch


{


//无法访问窗口


}


}


返回 null ;


}



private void RemoveOrphanedTaskPanes()


{


< p class = MsoNormal> for int i = .CustomTaskPanes.Count;我> 0; i - )


{


< span style ="color:rgb(43,445,175)"> CustomTaskPane tpane = this .CustomTaskPanes [i - 1];


Word。窗口窗口= null ;


尝试


{


< p class = MsoNormal> window = tpane.Window as Word。 Window ;


}


< span style ="color:blue"> catch


{


//无法进行无法访问的窗口


}


if (window == < span style ="color:blue"> null


{


.CustomTaskPanes.Remove(tpane);


}


}


} < span lang = EN-GB>

解决方案

插件内自定义任务窗格的对象窗口集合,未找到时包含任何属性,但有例外:


System.Runtime.InteropServices.COMException(0x800A16C1):Object已删除。

"在Microsoft.Office.Interop.Word.Window.get_Visible()"


作为值。由于这个原因,它找不到旧窗格,而且它的结果是重复的。

我可以看到,在其他事件中,如果我访问自定义任务窗格集合,也会发生这种情况。

I trying to use a custom task pane in word with multiple documents, I use as guide http://msdn.microsoft.com/en-us/library/bb264456.aspx#managingmultipletaskpanes_managingtaskpanesinmultipledocuments but I can’t do it work fine.

 

I have some problems, the orphaned task panes hasn’t been found well as same too the existing task pane for a window. Because of this in some occasion I have two panes or none instead of always one.

 

I see that the problem it’s that when I try to access to the window property for the pane in some occasions it fails returning and exception.

 

Another problem it’s that the task pane window and document window the equals are not true, and at the end the window are the same and appear more than one pane for that window. (I try with equals method and with ==)


I can see that using only one document that is close and open several times, after some time we can see that i have a multiple task panes instead of one in the collection and in more than one occasion more than one visible too. I close and open a same document a lot of times to can do some operations over it.

 

I use the next methods:

 

  private void Application_NewDocument(Word.Document doc)

        {

            ShowTaskPaneForDocument(doc);

        }

 

        private void Application_DocumentOpen(Word.Document doc)

        {

            RemoveOrphanedTaskPanes();

            ShowTaskPaneForDocument(doc);

        }

 

        private void ShowTaskPaneForDocument(Word.Document doc)

        {

            if (Application.ShowWindowsInTaskbar)

            {                                                

                CustomTaskPane tpane = GetTaskPaneForWindow(doc, this.Application.ActiveWindow);

                if (tpane == null)

                {

                    AddTaskPane(doc);

                }

                else

                {

                    tpane.Visible = true;

                }

            }  

        }

 

        private void Application_DocumentChange()

        {

            RemoveOrphanedTaskPanes();

        }

 

        private CustomTaskPane GetTaskPaneForWindow (Word.Document doc, Word.Window wn)

        {

            Word.Window docWn = null;

            try

            {

                docWn =  doc.ActiveWindow as Word.Window;

            }

            catch

            {

                //Nothing to do the window in unreachable

            }

            for (int i = this.CustomTaskPanes.Count; i > 0; i--)

            {

                CustomTaskPane tpane = this.CustomTaskPanes[i - 1];

                try

                {

                    Word.Window tpWn = tpane.Window as Word.Window;                   

                    if (tpane.Title == TPANE_TITLE &&

                         (tpWn.Equals(docWn) || tpWn.Equals(wn)))

                    {

                        return tpane;

                    }

                }

                catch

                {

                    //Nothing to do the window in unreachable

                }

            }

            return null;

        }       

 

        private void RemoveOrphanedTaskPanes()

        {

            for (int i = this.CustomTaskPanes.Count; i > 0; i--)

            {

                CustomTaskPane tpane = this.CustomTaskPanes[i - 1];

                Word.Window window = null;

                try

                {

                    window = tpane.Window as Word.Window;

                }

                catch

                {

                    //Nothing to do the window in unreachable

                }

                if (window == null)

                {

                    this.CustomTaskPanes.Remove(tpane);

                }

            }

        }

解决方案

The object window of the custom task panes inside of addin collection, when isn't found contains any property with the exception :

System.Runtime.InteropServices.COMException (0x800A16C1): Object has been deleted.

"   at Microsoft.Office.Interop.Word.Window.get_Visible()"


as value. For this reason the old pane it's not found and a result of it it's duplicated.

I can see that in other events if i access to the custom task panes collection this happen too.


这篇关于WORD:管理具有多个文档的自定义任务窗格时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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