How to auto reload Excel file during runtime?

Best practices, code snippets for common functionality, examples, and guidelines.
Madhumita Ghosh3
Posts: 8
Joined: Mon Dec 19, 2016 8:53 am

How to auto reload Excel file during runtime?

Post by Madhumita Ghosh3 » Wed Mar 01, 2017 12:52 pm


I have two test cases (say Test Case A and Test Case B) in my Test Suite each referring to the same Data Connector (i.e. same Excel Connector).
In Test Case A I have a code to open the Excel file during runtime, write some value (say '100') in a column named 'strcallID' in this case, save it and close it. Please follow the code below:

var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel._Worksheet worksheet = (Excel.Worksheet)excelApp.ActiveSheet;
worksheet.Cells[2,"AS"] = strcallID;

In Test Case B when am trying to refer to that new value that has been written by Test Case A, Test Case B is not being able to read that newly written value as Ranorex memory (Data Connector) is still referring to the initial excel. Is there any way (any code) to auto reload the excel file in Ranorex during runtime as I am not being able to run my test cases E2E.

Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: How to auto reload Excel file during runtime?

Post by McTurtle » Wed Mar 01, 2017 2:47 pm

Hello Madhumita,

I tried your code and for me it works fine.

What I did:

1. Test case 1 has first a report action to check what the value of the cell is. This is done via data connector to the excel file. Ranorex is reporting a value of the variable/cell which was manually preset before running the solution to "a".
2. In Test case 1, after the report action, Ranorex has your user code within the same recording module to set the value Ranorex was just reading to "b".
3. There is a 2nd Test case, in which Ranorex again reports the value of the variable which it read via data connector from the same Excel file.

It works for me. After the test-run completes it instantly asks me if I want to reload the excel file.

Some questions to better understand what could be the issue:

1. Are you reading the value in the 2nd test case via data connector or via code? If it was via code, you could remove the data connectors completely.
2. Have you ticked "Copy File to Project" in the Manage Data Sources window? In this case you are writing to a different Excel file than you are reading from, if you are using data connectors to read the value. The excel file gets copied into ..\bin\Debug and Ranorex is reading from there.