使用VS2008开发Excel AddIn,在部署的时候会出现很多奇怪的问题。

如:在开发机器上安装没有问题,然而到一台普通的机器上时则可能会出现安装不上的问题。

那么遇到此种情况首先需检查安装程序是否做了如下操作:

1)为你的Excel AddIn进行策略授权(Caspol)

2)客户环境是否安装了Microsoft Office 2003 Primary Interop Assemblies

3)客户环境是否安装了Microsoft Visual Studio 2005 Tools for Office Runtime

如果以上三步都已经符合要求了,那么则需要你在AddIn的构造函数中是否添加了代码(这个地方如果你初始化了自己的自定义对象可能会因为你的程序集尚未装载到内存而导致无法初始化的错误:未将对象设置到实例。这样你的程序AddIn将会无法正常加载,抛出加载时错误或本Excel禁用)。

下面的代码可以放在安装包的Commit时执行:

代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Diagnostics;
  6 using Microsoft.Win32;
  7 
  8 namespace CTPExcelAddInInstall
  9 {
 10     class Program
 11     {
 12         static System.IO.StreamWriter sw = null;
 13         static void Main(string[] args)
 14         {
 15             string fullpath = System.Reflection.Assembly.GetExecutingAssembly().Location;
 16             string installLocation = fullpath.Substring(0, fullpath.LastIndexOf('\\'));
 17 
 18             sw = System.IO.File.CreateText("C:\\CTPExcelAddInstall.log");
 19             sw.WriteLine("安装路径:" + installLocation);
 20 
 21             sw.WriteLine("开始安装时间:" + DateTime.Now.ToString());
 22 
 23             try
 24             {
 25                 CaspolExcelAddInFiles(installLocation);
 26 
 27                 sw.WriteLine("成功执行授权脚本!");
 28 
 29             }
 30             catch (Exception exp)
 31             {
 32                 sw.WriteLine("为应用程序授权时出错,错误信息:" + exp.Message);
 33             }
 34 
 35             try
 36             {
 37                 InstallOfficePIA(installLocation);
 38 
 39                 sw.WriteLine("成功执行安装 Office 2003 Primary Interop Assemblies!");
 40             }
 41             catch (Exception expPIA)
 42             {
 43                 sw.WriteLine("执行安装 Office 2003 Primary Interop Assemblies 时出错,错误信息:" + expPIA.Message);
 44             }
 45 
 46             try
 47             {
 48                 RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Microsoft Visual Studio 2005 Tools for Office Runtime");
 49                 if (regKey == null)
 50                 {
 51                     InstallVSTO2005(installLocation);                    
 52                 }
 53             }
 54             catch (Exception expVSTO)
 55             {
 56                 sw.WriteLine("执行安装Microsoft Visual Studio 2005 Tools for Office Runtime 时出错,错误信息:" + expVSTO.Message);
 57             }
 58             
 59             
 60 
 61             sw.WriteLine("结束安装时间:" + DateTime.Now.ToString());
 62             sw.Close();
 63 
 64             System.Threading.Thread.Sleep(2000);
 65         }
 66 
 67         /// <summary>
 68         /// 安装 Office 2003 Primary Interop Assemblies
 69         /// </summary>
 70         /// <param name="filePath">安装文件路径</param>
 71         static void InstallOfficePIA(string filePath)
 72         {
 73             Process pExecPIA = new Process();
 74             pExecPIA.StartInfo.FileName = "MSIEXEC.EXE";
 75             pExecPIA.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727";
 76             pExecPIA.StartInfo.CreateNoWindow = true;
 77             pExecPIA.StartInfo.Arguments = " /i \"" + filePath + "\\O2003PIA .msi\"";
 78             pExecPIA.StartInfo.UseShellExecute = false;
 79             pExecPIA.StartInfo.RedirectStandardInput = true;
 80             pExecPIA.StartInfo.RedirectStandardOutput = true;
 81             pExecPIA.StartInfo.RedirectStandardError = true;
 82             pExecPIA.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
 83             pExecPIA.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
 84             pExecPIA.ErrorDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
 85             pExecPIA.Start();
 86             pExecPIA.BeginOutputReadLine();
 87             pExecPIA.BeginErrorReadLine();
 88             pExecPIA.Close();
 89         }
 90 
 91         /// <summary>
 92         /// Excel插件执行策略授权
 93         /// </summary>
 94         /// <param name="filePath">授权文件夹</param>
 95         static void CaspolExcelAddInFiles(string filePath)
 96         {
 97             Process pCaspolCMD = new Process();
 98             pCaspolCMD.StartInfo.FileName = "cmd.exe";
 99             pCaspolCMD.StartInfo.UseShellExecute = false;
100             pCaspolCMD.StartInfo.RedirectStandardInput = true;
101             pCaspolCMD.StartInfo.RedirectStandardOutput = true;
102             pCaspolCMD.StartInfo.RedirectStandardError = true;
103             pCaspolCMD.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727";
104             pCaspolCMD.StartInfo.CreateNoWindow = true;
105             pCaspolCMD.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
106             pCaspolCMD.StartInfo.Arguments = "/K \r\nCaspol -u -ag All_Code -url \"" + filePath + "\\*\" FullTrust -n  \"CTPExcelAddIn\"";
107             pCaspolCMD.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
108             pCaspolCMD.ErrorDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
109             pCaspolCMD.Start();
110             pCaspolCMD.StandardInput.WriteLine("y");
111 
112             pCaspolCMD.BeginOutputReadLine();
113             pCaspolCMD.BeginErrorReadLine();
114 
115             pCaspolCMD.Close();
116         }
117 
118         /// <summary>
119         /// 安装 VSTO 2005 Runtime
120         /// </summary>
121         /// <param name="filePath">安装文件路径</param>
122         static void InstallVSTO2005(string filePath)
123         {
124             string DirectoryName = filePath + "\\Microsoft Visual Studio 2005 Tools for Office Runtime";
125 
126             if (System.IO.Directory.Exists(DirectoryName))
127             {
128                 Process pVSTO2005 = new Process();
129                 pVSTO2005.StartInfo.FileName = DirectoryName + "\\install.exe";
130                 pVSTO2005.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727";
131                 pVSTO2005.StartInfo.CreateNoWindow = false;
132                 pVSTO2005.StartInfo.UseShellExecute = true;
133                 pVSTO2005.Start();
134                 pVSTO2005.Close();
135 
136                 sw.WriteLine("成功执行安装 Microsoft Visual Studio 2005 Tools for Office Runtime!");
137             }
138             else
139             {
140                 sw.WriteLine("未找到“Microsoft Visual Studio 2005 Tools for Office Runtime”安装目录:" + DirectoryName);
141             }
142         }
143 
144         static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
145         {            
146             Console.WriteLine(e.Data);
147         }       
148 
149     }
150 }
151 

转载于:https://www.cnblogs.com/sucsy/archive/2010/09/14/1826125.html

使用VS2008开发及部署Excel AddIn 心得相关推荐

  1. [转】:VS 2010 : 如何开发和部署Outlook 2010插件(Add-in)

    VS 2010 : 如何开发和部署Outlook 2010插件(Add-in) 转自: http://guofblog.blog.163.com/blog/static/168088614201062 ...

  2. 浅谈Excel开发:一 Excel 开发概述

    做Office相关的开发工作快一年多了,在这一年多里,在插件的开发中遇到了各种各样的问题和困难,还好同事们都很厉害,在和他们的交流讨论中学到了很多的知识.目前Office相关的开发资料是比较少的,最最 ...

  3. .NET Core开源行动:一键创建Excel Add-in

    作为.NET Core开源行动的一部分,我此前已经创建和发布了一套基于.NET Core的Office 365开发模板库,是针对Microsoft Graph开发的场景的,有兴趣可以参考 https: ...

  4. VS2008 开发设计MOSS工作流 URN 注意了

    最近学习MOSS 很苦恼,进度也很慢,最近在学习VS2008开发工作流,其中有结合INFOPATH 2007来做,  出现个BUG或者说是设置的问题,整整花了我一天工作时间,是这样的: 在部署的时候关 ...

  5. 解读vs2003、vs2005、vs2008开发Windows CE环境、默认SDK开发包及测试

    文章来自http://swanmsg.blog.sohu.com/184427819.html 以前做过一些嵌入式开发,那么从以前做产品.做项目设计到嵌入式的开发,零碎时间累计到现在也有一年载已.本人 ...

  6. 中国移动Mas短信平台开发增值服务的一些心得

    Mas短信开发增值服务平台建设 中国移动MAS机的知识本人不是很熟悉,只知道如何使用它和.net开发链接起来做短信发布,下面是我在网上看到的一些介绍: 移动代理服务器 Mobile Agent Ser ...

  7. 解读vs2003、vs2005、vs2008开发Windows CE环境、默认S pda 智能设备

    解读vs2003.vs2005.vs2008开发Windows CE环境.默认SDK开发包及测试 标签: vs2003  vs2005  vs2008  wince5.0  wince6.0 分类:  ...

  8. MathWorks的AI之路:面向工业场景,打通开发到部署的全链路

    作者 | 阿司匹林 AI正在快速发展,并在更多的领域落地.对于MATLAB和Simulink的开发商MathWorks来说,把握AI的机会,显得尤为重要. 不少人对MATLAB等的印象依然停留在学校期 ...

  9. 用对方法,开发与部署深度学习原来如此简单……

    相信大部分人都会谈癌色变,正如我们所知的一样,晚期癌症患者的生存率低于 20%,而尽管早期患者可以被治愈,且治愈率高达 90% 以上,但因为大部分癌症早期起病隐匿,更重要的原因在于受限于现有医疗水平, ...

最新文章

  1. spring-amqp整合rabbitmq消费者配置和代码
  2. 【目录】python全栈工程师自动化+Py全栈+爬虫+Ai+python全栈工程师
  3. TPFanControl v0.62 + 汉化补丁
  4. 安装kenlm出现问题的解决方案gcc g++
  5. 服务端设置忽略更新_react服务端渲染: cookie如何透传给后端,后端如何设置cookie...
  6. 语法长难句-----状语与状语从句
  7. 面试常问:什么是红黑树?
  8. 文字转语音,有什么软件好用?
  9. 焦虑症和植物神经紊乱是同一种疾病吗
  10. Python爬取童程童美TTS网站知识点图片
  11. 剪枝中的train from scratch的解释
  12. ADXL345 驱动代码
  13. R语言——如何调用自己写的函数
  14. 金融之npv(净现值)_python实现计算公式_最通俗解释
  15. 55.跳跃游戏(Jump Game)
  16. 机械硬盘大势已去?NAS硬盘笑了
  17. 背包问题(Knapsack Problem)
  18. Android开发之ExpandableListView
  19. 16_张孝祥_多线程_同步工具CyclicBarrier与CountDownLatch
  20. 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

热门文章

  1. 禁用计算机服务LanmanServer,计算机关闭默认共享C$D$E$F$ADMIN$IPC$的方法
  2. 写给第十七届,来自十六届的感想与建议
  3. 2020人工神经网络第一次作业-参考答案第十部分
  4. Prolific PL2303SA 调试
  5. java简单springboot系统_Springboot系列 3 - 建立简单的用户登录系统
  6. 点击空白处遮罩层关闭_如何手动关闭win10系统自带的windows defender
  7. linux在生信的作用,【生信笔记】右键菜单打开WSL功能方法简介
  8. python数据趋势算法_Python数据拟合与广义线性回归算法学习
  9. android绘制环形进度_android 圆环进度view
  10. java方法声明无效_java 方法声明无效 需要返回类型