如何让代码变得更简洁

简约至上

 

去掉vsto生成的任务窗格

使用vsto对office进行二次开发时,一般会自定义任务窗格!但有时我们并不需要在打开office时都带上任务窗格。而且对于一些外部用户(没有安装office二次开发产品的用户),打开同样的文档时会弹出烦人的提示窗口。那么如何将任务窗格跟office分离呢?

在需要的地方加上,以下代码:

if (ServerDocument.GetCustomizationVersion(targetfilepath) == 3) ServerDocument.RemoveCustomization(targetfilepath);

使用上面的代码要引用

using Microsoft.VisualStudio.Tools.Applications;如下图

注意版本

然后再次打开保存过的文档,会发现任务窗格已经没有了!!!

下面的是我的一个例子,我在vs2010中新建了一个“excel模板”项目,在任务窗格中添加了一个按钮,用来自定义“另存为”功能,但是用户要求是另存为后,只想看到office文档,不要任务窗格。所以有代码如下:

 

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            try {
                string targetfilepath = @"D:\学习\DesignPattern\练习\WinformTest\ExcelTemplateTest\ExcelTemplateTest14.xlsx";
                Globals.ThisWorkbook.SaveCopyAs(targetfilepath);
                if (ServerDocument.GetCustomizationVersion(targetfilepath) == 3) ServerDocument.RemoveCustomization(targetfilepath);
            }
            catch (Exception ex) { throw ex; }
            finally {
                Globals.ThisWorkbook.ThisApplication.UserControl = true;
                Globals.ThisWorkbook.Close();
                Globals.ThisWorkbook.ThisApplication.Quit();
            }
        }

 

posted on 2011-09-29 18:21  我每天都在进步o(∩_∩)o...  阅读(933)  评论(0编辑  收藏  举报

导航