打破excel WorkBook链接

时间:2015-10-05 15:57:51

标签: c# excel excel-vba hyperlink vba

我试图以编程方式破坏工作簿的所有链接。我尝试使用方法Workbook.LinkSources方法但不起作用,如果有链接,它也会返回null。

 public static string BreakLinks(string excelfilename, string newexcelfile, bool close, Workbook mywrk)
        {
            string nomefile = excelfilename;
            Workbook mywrk = GetWorkBook(excelfilename);

            try
            {
                Array olinks = mywrk.LinkSources(Microsoft.Office.Interop.Excel.XlLink.xlExcelLinks) as Array;
                if (olinks != null)
                {
                    for (int i = 1; i <= olinks.Length; i++)
                    {
                        mywrk.BreakLink((string)olinks.GetValue(i), Microsoft.Office.Interop.Excel.XlLinkType.xlLinkTypeExcelLinks);
                    }

                    SafeSaveCopyAs(mywrk, newexcelfile);
                    nomefile = newexcelfile;
                }
            }
            catch
            {

            }
            if (close) CloseWorkBook(ref mywrk);
            return nomefile;
        }

我在同一文档中使用此vba代码来验证是否存在链接,并且它们是。

Sub main()
    Dim olinks As Object
    Dim v As Variant
    v = ActiveWorkbook.LinkSources(xlExcelLinks)
    Dim first_link As String
    first_link = v(1)
    Debug.Print first_link
End Sub

为什么VBA会返回链接而c#approch没有?有什么想法吗?

0 个答案:

没有答案
相关问题