Embed Excel in Word

I’ve created a Word document with WordApplication. Now I want to embed an Excel document (xlsx) into the Word document (docx).

It should look like this: Example of Result - You have a normal DOCX and within the text you see an Excel icon with a file name like TableXYZ.xlsx

It seems that I must do something like this: Create an Excel sheet with ExcelApplication and embed it with something like this:

oWord.Selection.InlineShapes.AddOLEObject(72,72,-1,-1,oExcel,<Name of the table>+".xlsx",Office.msoFalse)

I have a Windows 10 with Office 365 and XOJO Version 2019 Release 3.1

Does anyone have any idea how this works?

Hello Michael,

Here are the instructions for an example that works with Windows 10, Word 2016, and xojo 2019 Release 3.1:

  1. Create a new Excel program and do something like add 1 and 1 for an answer of 2, and save the file on the desktop of your computer with the filename Test1.xlsx.
  2. Create a desktop program in Xojo and add a button. In the button create an action event and add the following code:

[code]Sub Action() Handles Action
Dim Word as New WordApplication
Word.Documents.Add
Word.Visible = True

Word.Selection.InlineShapes.AddOLEObject(“Excel.Sheet.12”,“C:\Users\eugen\Desktop\Test1.xlsx”, False, False)
End Sub
[/code]

This worked on Microsoft Word that is installed on my computer. I am not sure if it works with Office 365 that is running in the cloud.

Happy to help.

Hello Eugene!

I will test your proposal later this weekend.

As a result, I need a Word document that contains the Excel document - just one file. Only the Word document should be passed on to the user.

Thanks for the proposed solution!

Hello Eugene,

your example works.
With

Word.Selection.InlineShapes.AddOLEObject("Excel.Sheet.12","C:\\Users\\eugen\\Desktop\\Test1.xlsx", False, False)

i get an Excel table embedded in my document.
With

Word.Selection.InlineShapes.AddOLEObject("Excel.Sheet.12","C:\\Users\\eugen\\Desktop\\Test1.xlsx", False, True)

i get only an icon with the embedded Excel sheet in my document. That’s what I need, because the Excel sheet may contain many, many columns and rows - too much for a Word document.

Now I am looking for the green icon with the Excel symbol. Actual I get an icon with an empty page like the GenericDocumentIcon in macOS.

Now I have the solution with the Excel-Icon and the right comment beneath the icon:

Word.Selection.InlineShapes.AddOLEObject("Excel.Sheet.12","C:\\Users\\eugen\\Desktop\\Test1.xlsx", False, True,"EXCEL.EXE",0,"MyEmbeddedExcelSheet.xlsx")

The last question for me is: Do I always have to first create an Excel file on the hard drive before embedding it in the Word file? Or is that also possible directly from the Excel object?

Hi Michael, I am glad that the solution is working for you.

It appears that the AddOLEObject requires a file path and name, which means it looks like a file is needed. I am not sure if the file must be located on your computer or from a public file system on the internet with an https:// prefix?

I am not sure what is means by directly from the Excel object, could you help me understand your question a little more? Thanks.

Normally I would create an Excel file with

var oExcel              as new ExcelApplication
var oBook               as ExcelWorkbook

oExcel.Visible = true
oBook = oExcel.Workbooks.add
oExcel.Application.DisplayAlerts = false 

<...fill the cells with values...>

oBook.SaveAs( <Path of ExcelSheet><Excel file.XLSX> )

//  Quit Excel
oExcel.Application.DisplayAlerts = true 
oExcel.Application.Quit

but I like to embed the Excel file without the “SaveAs” and use maybe the oBook object for embedding. So I can save the transfer to the hard drive and the deletion of the temporary file afterwards.

Hello Michael,

I tried a few modifications to the program, and I could only get the program to embed Excel in Word with a saved file. There might be a workaround, and for now it looks like the program needs an Excel file and then embed the file in Word.

Sincerely…

What is the function of oWord.Visible or oExcel.Visible?

I learned that I should work with

var oWord  As WordApplication
oWord = new WordApplication
oWord.Visible = true

or

var oExcel as new ExcelApplication oExcel.Visible = true .

I’ve testet it, it also works with FALSE.

See Application.Visible property (Excel).

This property can be used to hide an office application while working. It is useful so that a user becomes not confused from busy Excel windows while a table is created or other tasks are performed.