如何使用C#和Office Interop获取Office文档的完整文件名?

在下面的代码中,第二行引发一个错误,读取“HRESULT:0x800A03ECexception”。 我怀疑我得到一个空的FullName。 有人能告诉我我做错了什么吗?

Foo.DataClasses1DataContext db = new Foo.DataClasses1DataContext(); string ThisDocument = Globals.ThisAddIn.Application.ThisWorkbook.FullName; byte[] inputBuffer = System.IO.File.ReadAllBytes(ThisDocument); Foo.RFP_Document rfpDocument = new MediaDesk.RFP_Document(); rfpDocument.DocumentName = "Foobar"; rfpDocument.DocumentFile = new System.Data.Linq.Binary(inputBuffer); db.RFP_Documents.InsertOnSubmit(rfpDocument); db.SubmitChanges(); 

对于上下文,这是一个用C#编写的面向.NET 4.0的Excel 2010外接程序。 代码的目标是将文档保存到数据库。

使用Globals.ThisAddIn.Application.ActiveWorkbook.FullName 。 从MSDN Globals.ThisAddIn.Application.ThisWorkBook返回运行“macros”的工作簿。 既然你没有macros,这将抛出一个exception。