Sign in to follow this  
Followers 0

C# Excel Add-in HELP!

6 posts in this topic

Posted

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

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

ThisAddIn.ExcelInsert(inserts.ToString());

[/CODE]

and here is where hell starts

[CODE]
public static void ExcelInsert(string mInsert)
{
try
{
// DOESNT WORK :(
var range = this.Application.ActiveCell() as Excel.Range;
range.Text.ToString(mInsert);
}
catch (Exception)
{
throw;
}
[/CODE]

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!

Share this post


Link to post
Share on other sites

Posted

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 [url="http://msdn.microsoft.com/en-us/library/office/bb978779(v=office.12).aspx#DevGuideExcel2007RangeObject_InsertingValuesintoaRange"]http://msdn.microsoft.com/en-us/library/office/bb978779(v=office.12).aspx#DevGuideExcel2007RangeObject_InsertingValuesintoaRange[/url]

By the way there are a few anti-patterns in this method:
- [url="http://blogs.msdn.com/b/codeanalysis/archive/2006/06/14/631923.aspx"]Don't catch System.Exception[/url]
- Don't use empty catch clauses that just rethrow the exception - that achieves nothing except resetting the stack trace, making debugging harder
- [url="http://www.neowin.net/forum/blog/460/entry-3941-casts-in-c/"]Don't use "as" to cast[/url] if you're expecting the cast to always succeed - use a regular cast instead (i.e. (Excel.Range)Application.ActiveCell())

Share this post


Link to post
Share on other sites

Posted

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!

Share this post


Link to post
Share on other sites

Posted

[quote name='Dr_Asik' timestamp='1348862260' post='595213389']
- [url="http://www.neowin.net/forum/blog/460/entry-3941-casts-in-c/"]Don't use "as" to cast[/url] if you're expecting the cast to always succeed - use a regular cast instead (i.e. (Excel.Range)Application.ActiveCell())
[/quote]

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

Share this post


Link to post
Share on other sites

Posted

[quote name='Kalint' timestamp='1348863703' post='595213463']Can you explain the Method a bit more? The form does have access to the application object but anything further I cannot access.[/quote]I don't know how to put it more plainly, so here's some code:

From the form:
[CODE]ThisAddIn.ExcelInsert(inserts.ToString(), Application);[/CODE]

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

Share this post


Link to post
Share on other sites

Posted

[quote name='Dr_Asik' timestamp='1348864424' post='595213493']
I don't know how to put it more plainly, so here's some code:

From the form:
[CODE]ThisAddIn.ExcelInsert(inserts.ToString(), Application);[/CODE]

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

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.