Guidance on Embedding Excel in Word using UiPath

Hi Team,

We have a requirement where we need to embed or attach an Excel file within a Word document. The goal is to have the Excel file open upon double-clicking it from inside the Word document.

Please note that we are looking to natively embed the Excel file within the Word file itself β€” not through a SharePoint or HTML link.

Could you please advise on how to achieve this using UiPath?

Thank you!

@tejaskumar.darji,

It seems that there is no direct activity available in UiPath Studio to embed an Excel file into a Word document as an object directly. However, you can utilize the Invoke Code activity to achieve this functionality by using the Microsoft.Office.Interop.Word library.

' Add required namespaces for Word Interop
Imports Microsoft.Office.Interop.Word

' Define variables
Dim wordApp As Application = New Application()
Dim wordDoc As Document = wordApp.Documents.Open("C:\path\to\your\word\document.docx")

' Move to the desired location in the document where you want to insert the Excel file
Dim range As Range = wordDoc.Bookmarks("\endofdoc").Range

' Add the Excel object as an embedded OLE object in the Word document
' Note: Change the path to the actual location of your Excel file
range.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet", _
                                FileName:="C:\path\to\your\excel\file.xlsx", _
                                LinkToFile:=False, _
                                DisplayAsIcon:=True)

' Save and close the document
wordDoc.Save()
wordDoc.Close()
wordApp.Quit()

' Release COM objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp)

LLM helped me to write this but it’s validated by me.

Can you explain steps to import the Microsoft.Office.Interop.Word

@tejaskumar.darji,

You have to import it from the import panel at the bottom of your workflow.

1 Like

Getting below error .. do we need to import more namespace ?

@nidhi.goyal2,

Try this updated code.

' Add required namespaces for Word Interop
Imports Microsoft.Office.Interop.Word

' Define variables
Dim wordApp As Application = New Application()
Dim wordDoc As Microsoft.Office.Interop.Word.Document = wordApp.Documents.Open("C:\path\to\your\word\document.docx")

' Move to the desired location in the document where you want to insert the Excel file
Dim range As Microsoft.Office.Interop.Word.Range = wordDoc.Bookmarks("\endofdoc").Range

' Add the Excel object as an embedded OLE object in the Word document
' Note: Change the path to the actual location of your Excel file
range.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet", _
                                FileName:="C:\path\to\your\excel\file.xlsx", _
                                LinkToFile:=False, _
                                DisplayAsIcon:=True)

' Save and close the document
wordDoc.Save()
wordDoc.Close()
wordApp.Quit()

' Release COM objects
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp)

Getting below error

Message: Exception has been thrown by the target of an invocation.

Exception Type: System.Reflection.TargetInvocationException

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. β€”> System.Runtime.InteropServices.COMException: The file appears to be corrupted. at Microsoft.Office.Interop.Word.Documents.Open(Object& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog, Object& XMLTransform)
at UiPathCodeRunner_93f9f59ab4db4017aa1b949c1009ffeb.Run()
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
β€” End of inner exception stack trace β€”
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object providedArgs, ParameterModifier modifiers, CultureInfo culture, String namedParams)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object args)
at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object args)
at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List1 inArgs, IEnumerable1 imps, Object args)
at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

@nidhi.goyal2,

Check if the file path provided is correct and accessible

1 Like