使用C#在Excel中更改链接的exception

作为文件存储迁移项目的一部分,我试图改变一些Excel工作簿中的一些Excel链接,以反映新的文件存储位置。

我在VS2017 RC中使用Winforms和C#来开发我打算部署的解决scheme。

在我的解决scheme, 我调用Excel Workbook对象上的ChangeLink方法,并传入旧链接,新链接和Excel链接types。

public string ProcessFile(string fileName) { // Private member variable and placeholder declarations missingValue = Type.Missing; string oldLink; string newLink; int splitLocation; string stringToFind = "\\Finance"; //Open the specified Excel Workbook Excel.Workbook excelWorkbook; StringBuilder resultsOut = new StringBuilder(); if (MsOfficeHelper.IsPasswordProtected(fileName)) { resultsOut = resultsOut.AppendLine("Password Protected - " + fileName); } else { // Open excelWorkbook = excelApp.Workbooks.Open(Filename: fileName, UpdateLinks: false); Array olinks = excelWorkbook.LinkSources(Excel.XlLink.xlExcelLinks) as Array; if (olinks != null) { if (olinks.Length > 0) { resultsOut = resultsOut.AppendLine("Contains Links - " + fileName); foreach (var olink in olinks) { oldLink = olink.ToString(); splitLocation = oldLink.IndexOf(stringToFind, 0); newLink = "C:\\SteveTest\\" + oldLink.Substring(splitLocation + 1); resultsOut = resultsOut.AppendLine(oldLink); resultsOut = resultsOut.AppendLine(newLink); try { excelWorkbook.ChangeLink(Name: oldLink, NewName: newLink, Type: Excel.XlLinkType.xlLinkTypeExcelLinks); } catch (Exception whoopsy) { MessageBox.Show(whoopsy.Message); //throw; } } } } excelWorkbook.Close(SaveChanges: false); } return resultsOut.ToString(); } 

但是,当我执行ChangeLink方法时,我得到以下exception 异常消息

有没有人有任何想法是什么导致例外? 您的回应将受到大大欢迎。