这个问题在中文、英文 MSDN 的 Office 开发论坛上都看到过(也正好都是我提供了答案),看来也算是一个“国际化需求”了。今天就记录一下和大家分享。
故事是这样的:开发人员有一个 WinForm 应用程序,它从数据库里面读取出一条记录后,获得了一个记录 ID,然后需要打开 Word 或者 Excel,用已经准备好了的 VSTO 文档或者是插件,根据这个记录 ID 生成相应的 Office 文档内容。问题是,怎么把这个 ID 传给 Word 或者 Excel?
有一个很简单的实现方法——直接在启动 Word / Excel 的命令行里面加参数!比如:
<Excel.exe> /r "D:\ExcelWorkbook1.xlsx" /e /id:1234
其中 <Excel.exe> 应该是 Excel.exe 的完整路径。
/r 后面跟 VSTO Excel 文档的完整路径。
/e 告诉 Excel 不要打开一个空白新文档。
/id:1234 就是自己定义的参数了,你可以在这里把你的ID号传入。当然参数名字也可以是其它的,并不是一定要“id”。
第二步,在 VSTO Excel 文档里面,可以通过下面的代码枚举命令行参数:
For Each a As String In Environment.GetCommandLineArgs
MsgBox(a)
Next
上面的代码如果被执行,会把所有命令行参数都显示出来。
只要找到其中/id:开头的参数,把数字取出来就好了。
对于 Word,也可以用类似的方法,只是 /r /e 之类的参数要换成对应的 Word 参数。
这个方法用在 Word 2007 / Excel 2007 没有问题,但是我不能保证将来的版本是否也可以这样用。特别是如果你添加的自定义参数不巧被新版本用作标准参数名的话……所以参数名最好独特一点啦。

>>
Source Link>>Blog:
joycode>>Publish Date: 4/8/2008 7:02:27 AM
>>Keywords: excel word
Related Posts>>Why are the Microsoft Office file formats so complicated? (And some workarounds) # Last week, Microsoft published the binary file formats for Office. These formats appear to be almost completely insane. The Excel 97-2003 file format is a 349 page PDF file. But wait, that’s not al
>>Google Apps Premier Edition Coming # A screenshot of the current Google Apps for Your Domain offering
A good source tells me that Google will soon announce the selling of a premium version of their Google Apps service for $50 per year
>>Fauxto Online Photo Editor # Fauxto*, released in late 2006, is a Photoshop-like online image editor that"s constantly growing its list of features. Once you edited a photo with brushes, layers, fill tools, eraser and so on, you
>>Box推出Microsoft Office插件 # 作者:Src Google文件非常受欢迎的一个重要原因就是在线存储,可以随时随地,使用非常方便。但是Google文件本身是基于Web浏览器的,功能十分有限。如果在MS Office里面实现文件的在线编辑和存储,会不会是一件非常爽的事情? 基于这样的需求,老牌的在线存储服务Box新推出一款基于Microsoft Office的插件Office On Demand,通过它用户可以直接在编辑时
>>Google Office vs. MS Office Home and Student # In the past, I"ve often said that Google Docs and Spreadsheets is not a Microsoft Office competitor, based on the fact that MS Office is expensive, meant for business use and had much more functionali
>>Gmail Rolls Out PowerPoint Preview # Interesting: Google rolled out their slideshow feature for Gmail (some of you were already getting this as experiment for a while). What happens is that whenever someone sends you a PowerPoint attachm
>>将业务系统与Office和SharePoint集成 # Wednesday, January 30, 2008 11:29 AM 我之前曾在博客中谈到过Visual Studio Tools for Office (VSTO)将作为Visual Studio 2008专业版的一部分发行,这样所有使用 VS专业版的开发人员就可以使用这些工具将Office作为智能客户端应用程序平台了。 大多数大型的业务系统,如ERP或CRM,无论是作为自定义应用
>>VSTO Power Tools 发布! # VSTO Power Tools 1.0 发布啦! 下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=46B6BF86-E35D-4870-B214-4D7B72B02BF9&displaylang=en 总共九个小工具、扩展类库,被包含在三个安装文件内(因为授权类型有不同的三种,所以需要三个安装包)。其
>>微软 Office Live Workspace 正式上线运营! #
Office Live Workspace 是微软推出的一款基于Web的在线工作空间,用户可以直接从本地Office办公套件中向其发送/同步数字文档,并可以让人们通过在线方式访问和分享他们的数字文件等等。近日,微软正式将 Office Live Workspace 推向了用户前端,接受来自全球用户的注册使用!
Office Live Workspace 是一款基于Web的工作空间,它以插件的形式
>>Excel Web Access Web部件 # 我们知道MOSS2007企业版提供了Excel Service,通过ES,我们可以通过一个网页浏览一个Excel文件,甚至通过这个页面输入一些数据进去以获得不同的计算结果。 如果我们需要在某些页面上通过Web部件来展现一个Excel文件,则可以通过一个叫“Excel Web Access”的内置Web部件。这个部件功能很全面,支持与其他筛选Web部件进行连接,来展现不同的内容。 这个Web部件可在