• 0

C# Excel Add-in HELP!


Question

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());

[/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!

Link to comment
https://www.neowin.net/forum/topic/1108879-c-excel-add-in-help/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

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.microsoft.com/en-us/library/office/bb978779(v=office.12).aspx#DevGuideExcel2007RangeObject_InsertingValuesintoaRange

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())

  • 0

- 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.

  • 0
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);[/CODE]

ExcelInsert becomes:

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

  • 0

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

From the form:

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]

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Putin was behind Farage/Brexit and behind Trump/MAGA. Different idiot lying beasts, same fascist master. Same screwed up results for both nations.
    • For me the Final Fantasy games, Langrisser I & II, Hardspace: Shipbreaker, Horizon Forbidden West Complete Edition, Metaphor: ReFantazio and LUNAR Remastered Collection
    • Limited Prime Day deal sees Amazon Echo Show 11 is at its lowest price yet by Ivan Jenic The Amazon Echo Show 11 is currently $149.99 on Amazon during Prime Day, down from $219.99. That's 32% off and $70 saved on Amazon's newest mid-size smart display (purchase link below). As its name suggests, the Echo Show 11 features a 11” Full-HD touchscreen, which is a decent upgrade over the Echo Show 8. It offers 60% more viewing area, which makes a genuine difference for recipes, calendars, video calls, or any other scenario where the smaller display can feel a bit cramped. The Show 11 is built around Alexa+, Amazon's more capable AI assistant that can handle natural conversation. You can make reservations, order groceries, and find content based on your preferences, instead of always having to give it precise commands. There’s also the Omnisense technology, which adds ambient awareness, allowing the device to trigger routines based on temperature, presence detection, or visual cues. Smart home control is built in without needing a separate hub, and the home screen doubles as a photo slideshow when not in use. Taking all of this into account, the Show 11 is a perfect smart addition to your kitchen counter or living room. At $149.99, this is a strong Prime Day buy for anyone already in the Amazon ecosystem or looking to build one out. Amazon Echo Show 11 - $149.99 | 32% off on Amazon Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • Dang that sucks. I just started using this feature instead of Pushbullet.
  • Recent Achievements

    • One Year In
      bernmeister earned a badge
      One Year In
    • Week One Done
      Scoobystu earned a badge
      Week One Done
    • Week One Done
      tuben earned a badge
      Week One Done
    • First Post
      OffsetAbs earned a badge
      First Post
    • Reacting Well
      OffsetAbs earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      463
    2. 2
      +Edouard
      215
    3. 3
      PsYcHoKiLLa
      156
    4. 4
      Steven P.
      73
    5. 5
      FloatingFatMan
      71
  • Tell a friend

    Love Neowin? Tell a friend!