3

I'm building an excel add in using c#, and i want the user to select a range. I have the inputbox set up and it's receiving the range fine when entered as a text. The problem is that excel is unresponsive when the inputbox is open, the user can't go over to excel and just select the range with the mouse, after i stop the debugging i get a message that the "File Now Available".

This is the part of the code

Excel.Range str = excelApp.InputBox("Select table range", "", _ 
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
MessageBox.Show("range = " + this.range.get_Address());
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Peter Green
  • 99
  • 1
  • 7
  • You are saying if you type range value in the input box, it works but if you want to select using mouse then it doesn't work? – bonCodigo Nov 27 '12 at 10:00
  • Almost accurate, it does work when i type range value. I'm not able to select using the mouse, it's like the excel is stuck, and not responding to anything. – Peter Green Nov 27 '12 at 10:15
  • Can you try this in Excel itself - meaning use a button, do the inputbox in excel module and try to choose the range from Excel? So you can narrow down if it's an interop issue or just Excel issue. – bonCodigo Nov 27 '12 at 10:37
  • I'm really not sure on how to do what you say. When i go on a simple function i can select a range. I don't know how to work within excel to create an inputbox – Peter Green Nov 27 '12 at 10:56
  • What I meant was a simple test to do in excel vba to narrow down what gives you the issue. I am not front of a machine now. But you can try this link to get a small vba code snippet to test out excel environment for any doggey behaviour. [Reference](http://www.mrexcel.com/forum/excel-questions/235355-visual-basic-applications-code-select-range-using-mouse-hilighting-range.html) Put this code in a vba module and compile it. If you an issue I can help you when I get back to the machine....or just hope someone else would. Sorry. – bonCodigo Nov 27 '12 at 11:27
  • Ok i've done that, created a inputbox with the vba using the code from your reference and it works fine, so i'm understanding from this that the problem isn't in excel. – Peter Green Nov 27 '12 at 11:36

2 Answers2

2

This is entirely by design and standard for any Windows user interface. The InputBox function display a modal dialog. Dialogs always disable the other windows in an application. Very annoying but an important safety measure to prevent problems due to re-entrancy. This answer explains why modal dialogs behave that way.

You will have to cook up your own non-modal input form to work around that restriction. Do note the trouble you might get into when you do, as shown in the linked answer. You won't have much to fear from the user-closes-ui problem, that will kill the form as well. But definitely the user-reissues-command problem.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This is a better explanation to the head-scratching I had. Hope it works for greenpeter. I am curious to know if it solves the issue as his current code doen't look buggy. :) – bonCodigo Nov 27 '12 at 13:53
  • I think i understand the problem, but i don't really understand exactly it is that i need to do to solve it. i'm a beginner, more guidance would be appreciated. – Peter Green Nov 27 '12 at 14:55
  • Don't solve it, users are familiar with the way dialogs work. – Hans Passant Nov 27 '12 at 14:59
0

Check this out: how to select an excel range using RefEdit control

I can update the code if you require. Please give it a try first.

bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • Thanks, this is way over my level, i'm a total beginner. – Peter Green Nov 27 '12 at 14:24
  • For the link I provided, the tutorial is just there - when you try certain things you can have lots of fun :D Depends how much time you have though. **Most importantly did you manage to get your issue solved and how?** – bonCodigo Nov 27 '12 at 14:47
  • No, didn't solve it. Unfortunately i don't have much time for this at the moment. – Peter Green Nov 27 '12 at 15:00