Patrick
Patrick

Reputation: 2583

C# Excel interop merge not working

I have defined a range with:

Range range =  ws.Range[ws.Cells[7, 1], ws.Cells[7, 4]];

and checked with

range.Interior.Color = rgbBlueViolet;

So the range is correct since I see it coloured.

But when I do

range.Merge(true);

I get the following exception:

{System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.Merge(Object Across)

which I can't understand.

My goal is to merge the first 4 columns toghether which to me seems legit.

Thank you for any help Patrick

Upvotes: 3

Views: 1922

Answers (1)

Patrick
Patrick

Reputation: 2583

Ok the problem was that a range to be merged has to be selected first. And to do that the worksheet can't be minimized. Thus the code is:

Application app = new Application();
app.Visible = false;
app.WindowState = XlWindowState.xlNormal; <---not minimized   
...
Range r = ws.Range[ws.Cells[row + 7, 1], ws.Cells[row + 7, 5]];
r.Select(); <-----necessary
r.Merge(false);

Upvotes: 4

Related Questions