Link Excel Worksheet in a Word document

ApsSanj 1 Reputation point
2021-02-22T05:48:09.56+00:00

What I wanted to achieve :

Implement a function in C# which executes similar to the Word object insertion as a link and attach a selected excel file into the word document with 'link to file', so that the changes to the excel source file is reflected in the Word document).

Word Insert > Object( in Text button group) > Create from File
70399-excellink.png

What I tried :

I am using Office 365.

Get the UsedRange.AddressLocal from a selected worksheet of the excel file, and get cell range from it. Then use the Word.Range.PasteSpecial method.

string addressLocal = worksheet.UsedRange.AddressLocal;  
if (addressLocal.Length > 0)  
{  
   cellRange = addressLocal.Replace("$", "");           
}  
Excel.Range range = this.worksheet.Range[cellRange];  
range.Copy();  

Get the range of the current selection.

wordRange = wordApp.Selection.Range;

Paste the copied range to the Word document.

wordRange.PasteSpecial(  
     Link: true,  
     DataType: Word.WdPasteDataType.wdPasteOLEObject,  
     Placement: Word.WdOLEPlacement.wdInLine,  
     DisplayAsIcon: false  
);  

But this gives an error when we try double click on the inserted excel.
70400-2kwaf.png

I have went through many articles in web related to this error, but none of them fixed my issue.

But when I change the copying excel cell range as below and pasting it doesn't give errors when double click and open the inserted excel.

string addressLocal = worksheet.UsedRange.AddressLocal;  
if (addressLocal.Length > 0)  
{  
    cellRange = addressLocal.Replace("$", "");  
    // If cellRange is "A1:E6", below code make it as "A1:E5"  
    string a = cellRange.Substring(0, cellRange.Length - 1);  
    int b = int.Parse(cellRange.Substring(cellRange.Length - 1, 1)) - 1;  
    cellRange  = a+ b.ToString();  
}  
Excel.Range range = this.worksheet.Range[cellRange];  
range.Copy();  

Is my approach for implementing the linking excel to word is incorrect? If so, could anybody please let me know what should I do to implement some functionality similar to word Excel object linking?

If my approach is correct, what should I do to insert the exact used range of the excel worksheet to the word?

Please help me with this.

Thank you in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,842 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,852 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-03-02T02:20:12.717+00:00

    Hi ApsSanj-3718,
    You can try to use Range.PasteExcelTable method to embed an excel-file in a word document.
    Here is my test code you can refer to.

    using Excel = Microsoft.Office.Interop.Excel;  
    using Microsoft.Office.Interop.Word;  
    using Application = Microsoft.Office.Interop.Word.Application;  
      
    var path =  @"C:\Users\Desktop\tt.xlsx";           
    Excel._Application excel = new Excel.Application();  
    var template = excel.Workbooks.Add(path);  
    var worksheet = (Excel.Worksheet)template.Worksheets["Sheet1"];  
    var table = (Excel.Range)worksheet.Range["A1:A5"];  
    table.Copy();  
    Application ap = new Application();  
    Document document = ap.Documents.Open(@"C:\Users\Desktop\tt.docx");  
    document.Bookmarks["test"].Range.PasteExcelTable(false, true, false);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.