Invoke Code for Word - How to attach to an opened file

Hi,
I need to interact with a Word Document from SAP.
I use the Invoke code activity in C#.
When I try to add a text box from a local Word document, this code is working fine

var wordApplication = new Microsoft.Office.Interop.Word.Application() { Visible = false };
var myDocument = wordApplication.Documents.Open(in_FilePath);
var sh = myDocument.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 10, 10, 110, 20);
sh.TextFrame.TextRange.Text = "RPA 10 10";
sh.TextFrame.MarginLeft = 0; sh.TextFrame.MarginRight = 0; sh.TextFrame.MarginTop = 0; sh.TextFrame.MarginBottom = 0;
myDocument.Save();
wordApplication.Quit();

My problem is that I need to perform the same task to an existing opened file. I don’t want to open a local file, I want to “attach”! How can I modify my code to do that?

Other question: how can I add the text box on every page except from page 1. I know how to get

  • the number of pages with range.get_Information and Microsoft.Office.Interop.Word.WdInformation.wdNumberOfPagesInDocument and the
  • the current active page with range.get_Information and Microsoft.Office.Interop.Word.WdInformation.wdActiveEndPageNumber

Thank you!

1 Like

Hi,

I’ve done this in C# project in visual studio (for excel worksheet but libraries methodology should be the same), but its really hard compared to simple opening workbook. We can have many word applications opened and many workbook in each of them…

Getting list of excel applications looks like this:

// ________________________________________________________________________get list of excel applications_________________________________________________________
            List<Microsoft.Office.Interop.Excel.Application> lstApps = new List<Microsoft.Office.Interop.Excel.Application>();
            EnumChildCallback cb;
            List<Process> procs = new List<Process>();
            procs.AddRange(Process.GetProcessesByName("excel"));

            foreach (Process p in procs)
            {
                if ((int)p.MainWindowHandle > 0)
                {
                    int childWindow = 0;
                    cb = new EnumChildCallback(EnumChildProc);
                    EnumChildWindows((int)p.MainWindowHandle, cb, ref childWindow);

                    if (childWindow > 0)
                    {
                        const uint OBJID_NATIVEOM = 0xFFFFFFF0;
                        Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
                        Microsoft.Office.Interop.Excel.Window window = null;
                        int res = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref window);
                        if (res >= 0)
                        {
                            lstApps.Add(window.Application);
                        }
                    }
                }
            }

After that we can iterate through each word application, through each wordbook inside given application and attach to workbook (in this case using name):

wb = objApp.Workbooks[Name];

Inside invoke code it can be harder because of late binding and we need to import required libraries.
So I think its better to simply use open workbook method after all :slight_smile:

1 Like

Thank you for your advice.
I found the solution to perform tasks on an opened file with this line:

Microsoft.Office.Interop.Word.Application wordApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;

Then I can do whatever task I want (Ex to get the numbers of pages):

var nbPages = wordApp.ActiveDocument.ComputeStatistics(WdStatistic.wdStatisticPages, false);
Console.WriteLine(String.Format("Total number of pages in document: {0}", nbPages));
1 Like

You can’t attach to an already opened file. You never attach to an Excel file. The way UiPath connects to an Excel file is different than how it attaches to an app or browser window.

Close the Excel file, then open it with the Excel activities, then put your Invoke Code in there.

What happens if more than one instance of Word is open?

Its possible to attach to one of already opened excel, even if we have many workbooks or many excel applications. I’ve already test it in my custom activity package.

Attaching to non-active workbook/word document is still possible but difficult

This solution will work if we have sure that there is only one word application opened or this document is on active word application

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.