目前,参与PowerPoint Add-in项目开发。测试组发现打开两份PowerPoint2010文档,第二份文档里没有自定义任务窗格,而PowerPoint2007不会有该问题。查阅微软的MSDN相关文档,原来微软对PowerPoint2010文档框架作了改变,不再是采用PowerPoint 2007 会为所有文档(即工作簿和演示文稿)创建一个文档框架窗口,而是为每份文档都创建一个文档框架窗口。为了解决这个问题,我们需要在PowerPoint2010创建新文档或打开现有文档时为该文档创建自定义任务窗格。考虑到后续程序的需要,采用HashTable存储自定义任务窗格实例和索引。
///<summary>
///添加PPT2010CTP
///</summary>
///<param name="Pres"></param>
private void AddPPT2010CustomTaskPane(PowerPoint.Presentation Pres)
{
Microsoft.Office.Interop.PowerPoint.Application app = Pres.Application;
Microsoft.Office.Interop.PowerPoint.DocumentWindow docWin = app.ActiveWindow;
UCSample uc = new UCSample();
Microsoft.Office.Tools.CustomTaskPane ctp;
ctp = Globals.ThisAddIn.CustomTaskPanes.Add(uc, "Sample" , docWin);
ctp.Width = 260;
ctp.Visible = true;
SavePPT2010CTPUC(Pres.FullName, uc);
SaveCTPIndex(string.Format("{0}{1}",Pres.FullName,"Sample"), Globals.ThisAddIn.CustomTaskPanes.Count - 1);
}
///<summary>
///删除PPT2010CTP
///</summary>
///<param name="index"></param>
private void RemovePPT2010CustomTaskPane(int index)
{
if (Globals.ThisAddIn.CustomTaskPanes.Count > 0)
{
Globals.ThisAddIn.CustomTaskPanes.RemoveAt(index);
}
}
///<summary>
///添加PPT2010CTPUC
///</summary>
///<param name="fullName"></param>
///<param name="uc"></param>
private void AddPPT2010CTPUC(string fullName, UCSample uc)
{
if (HTUCSample.ContainsKey(fullName))//如果已存在,则删除
{
HTUCSample.Remove(fullName);
}
HTUCSample.Add(fullName, uc);
}
///<summary>
///删除PPT2010CTPUC
///</summary>
///<param name="fullName"></param>
private void RemovePPT2010CTPUC(string fullName)
{
if (HTUCSample.ContainsKey(fullName))//如果已存在,则删除
{
HTUCSample.Remove(fullName);
}
}
///<summary>
///添加PPT2010CTP索引
///</summary>
///<param name="key"></param>
///<param name="value"></param>
private void AddCTPIndex(string key,int value)
{
HTCTPIndex.Add(key, value);
}
///<summary>
///根据文件名删除PPT2010CTP索引
///</summary>
///<param name="key"></param>
private void RemoveCTPIndex(string key)
{
int index = 0;
foreach (DictionaryEntry item in HTCTPIndex)//遍历元索
{
if (Convert.ToString(item.Key) == key)
{
index = Convert.ToInt16(item.Value);
HTCTPIndex.Remove(key);
break;
}
}
Hashtable ht = new Hashtable();//重新赋值
foreach (DictionaryEntry item in HTCTPIndex)
{
int temp = Convert.ToInt16(item.Value);
if (temp > index)
{
ht.Add(item.Key, temp - 1);
}
else
{
ht.Add(item.Key, temp);
}
}
if (ht.Count > 0)
{
HTCTPIndex.Clear();
HTCTPIndex = (Hashtable)ht.Clone();
ht.Clear();
}
}
///<summary>
///根据文件名获取PPT2010CTP索引
///</summary>
///<param name="key"></param>
///<returns></returns>
private int GetCTPIndex(string key)
{
int index = 0;
foreach (DictionaryEntry item in HTCTPIndex)//遍历元索
{
if (Convert.ToString(item.Key) == key)
{
index = Convert.ToInt16(item.Value);
break;
}
}
return index;
}