1

Question1: How to access windows form and its controls on VSTO excel using C#?

Question2: On Execution of VSTO Excel workbook application, I want total no. of active columns when the user paste the data. Suppose User may paste 5 column data or 10 column data. Through code i want to figure out the total no. of columns(Also Total no. of rows if possible) once the user pastes/loads the tabular data. How to get indefinite range value for cells on execution of VSTO excel when data is pasted on the same.

Please help me with code for these two questions.

1
  • Hi Venkat, working with .VSTO addins for excel can be fun, and also tricky. Q1 : When using visual studio, a user control can be added to your addin, along with even your own ribbon object if you want. You just go project, add component and there it is. Great thing, everything works exactly the same as if you were coding a normal winforms application. Q2: Not too sure what you mean, but in vba you can write this to get the number of rows? : Sub test() 'enter Dim counter As Long 'enter counter = Range("A:A").Rows.Count 'enter MsgBox (counter) 'enter End Sub Commented Jul 24, 2016 at 7:44

2 Answers 2

1

Q1: Same as you do in a winforms application, when you create the windows form, keep a reference to it. Make sure to expose property accessors (get;set;) for the form.

Q2: You need to implement a AppEvents_SheetChangeEventHandler and assign to Globals.ThisAddin.Application.SheetChange event property. In the event handler, the second argument is Excel.Range TargetRange. TargetRange is the Excel.Range object containing the pasted information. The range.Columns.Count property gets you the total columns in the pasted, range.Rows.Count gets you the total rows in the pasted.

Not sure what you mean by "how to get indefinite range value for cells". Can you clarify?

Sign up to request clarification or add additional context in comments.

Comments

1

Q1: you can simply add Win Form through -> project -> add new item -> Form Page you must make new instance of this form.

For example: when you have designed form wit name winForm

Example.ExampleForm exampleForm = new ExampleForm();
exampleForm.Show();

Q2:

I am not sure I understand it correctly. But for example if you have sheet blank at the beginning you may do that like that :

   Excel.Range rng;
   object[,] swap;

   rng = tws.UsedRange;
   swap = rng.Value2;

Values of all cell which user paste will be accessible like is mentioned above in array of objects. This array is 1based!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.