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

4
在以下代码中,第二行会抛出一个错误,错误信息为“HRESULT: 0x800A03EC”。我怀疑FullName返回了null。请问有人能告诉我哪里出了问题吗?
        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#编写的Excel 2010插件,目标是将文档保存到数据库中,它的目标是使用.NET 4.0。

1个回答

4

请使用Globals.ThisAddIn.Application.ActiveWorkbook.FullName。从MSDN可以看出,Globals.ThisAddIn.Application.ThisWorkBook返回的是“宏”正在运行的工作簿,因此如果没有宏,这将会抛出异常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接