Remove DataLinks in an Excel Workbook Programmatically

This tip shows you how to automate the process of removing DataLinks in Excel using C#. A DataLink is a link stored in one Workbook that retrieves data from another Workbook. For example, suppose you have a chart in one Workbook that gets its data from another Workbook. That’s fine, as long as both Workbooks are available. However, if you want to send only the Workbook containing the chart, you need to remove the dependency on the Workbook containing the data.

Excel provides an Edit Links option to break the DataLinks manually. When you do so, Excel copies the current data values to populate the chart (or any other Excel object that uses DataLink data) from the source Workbook. Of course, you can break the links programmatically as well.

To get started, reference Microsoft’s Excel Interop Library in a .NET project. The Interop Library lets you access Excel Workbooks. The following function breaks the links in the Workbook you pass in as a parameter:

/// <summary>/// Breaks the DataLinks in a WorkBook/// /// private void BreakDataLinks(Excel._Workbook wb){            if (DialogResult.Yes ==       MessageBox.Show(this,      "Do you want to break the links to Data Sources ?",       "Links", MessageBoxButtons.YesNo))   {      Array links = (Array)wb.LinkSources(         Excel.XlLink.xlExcelLinks);      if (links != null)      {         for (int i = 1; i <= links.Length; i++)         {            wb.BreakLink((string)links.GetValue(i),            Excel.XlLinkType.xlLinkTypeExcelLinks);         }      }   }}

You also should suppress the Excel confirmation dialog that pops up when you break a DataLink. The following function suppresses the confirmation dialog:

oXL.DisplayAlerts   = false;oXL.AskToUpdateLinks = false; 

In the preceding code, oXL is the Excel Application Object defined as follows:

//Excel Application ObjectExcel.Application oXL;oXL = new Excel.Application();
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS