Jump to content



Photo

C# Excel Add-in HELP!


  • Please log in to reply
5 replies to this topic

#1 Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 28 September 2012 - 19:45

On my Form, this array is built then is sent to ThisAddin.cs

var inserts = new List<string> {mFunction, mColor, Convert.ToString(mQty), mFauc, Convert.ToString(mPrice)};

ThisAddIn.ExcelInsert(inserts.ToString());


and here is where hell starts

	    public static void ExcelInsert(string mInsert)
	    {
		    try
		    {
			    // DOESNT WORK <img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/sad.png' class='bbc_emoticon' alt=':(' />
			    var range = this.Application.ActiveCell() as Excel.Range;
			    range.Text.ToString(mInsert);
		    }
		    catch (Exception)
		    {
			    throw;
		    }

I know you can't use the first line in a static method, but honestly I cant wrap my head around an alternative.

I just want it so that at active cell in Excel it inserts that array into the range.

HAELP!


#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 28 September 2012 - 19:57

The ExcelInsert method could take the Excel.Range object as a parameter. The form has access to the Application object so it can get the ActiveCell() and pass it.

Also ToString() is a method that returns a string representation of the object and never takes a parameter. I'm quite sure that's not how you set the value of cells. See http://msdn.microsof...aluesintoaRange

By the way there are a few anti-patterns in this method:
- Don't catch System.Exception
- Don't use empty catch clauses that just rethrow the exception - that achieves nothing except resetting the stack trace, making debugging harder
- Don't use "as" to cast if you're expecting the cast to always succeed - use a regular cast instead (i.e. (Excel.Range)Application.ActiveCell())

#3 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 28 September 2012 - 20:21

Yeah I know about the catch, I have Resharper and it adds that in automatically.

Can you explain the Method a bit more? The form does have access to the application object but anything further I cannot access.

Thanks!

#4 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 28 September 2012 - 20:26

- Don't use "as" to cast if you're expecting the cast to always succeed - use a regular cast instead (i.e. (Excel.Range)Application.ActiveCell())


He doesn't need the cast at all, Application.ActiveCell returns Range.

#5 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 28 September 2012 - 20:33

Can you explain the Method a bit more? The form does have access to the application object but anything further I cannot access.

I don't know how to put it more plainly, so here's some code:

From the form:
ThisAddIn.ExcelInsert(inserts.ToString(), Application);

ExcelInsert becomes:
public static void ExcelInsert(string insert, Application application)
{
    var range = application.ActiveCell;
    // do whatever you want with range
}


#6 OP Kalint

Kalint

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 16-January 07

Posted 28 September 2012 - 20:50

I don't know how to put it more plainly, so here's some code:

From the form:

ThisAddIn.ExcelInsert(inserts.ToString(), Application);

ExcelInsert becomes:
public static void ExcelInsert(string insert, Application application)
{
	var range = application.ActiveCell;
	// do whatever you want with range
}


Okay so the ExcelInsert works (thanks for clearing that up). But now it says Interface not valid at this point on the form.