• 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

    • Hello, Christian Maas' XVI32 is a nice (and very small) hex editor. Speaking of hex editors, many years ago a colleague and I who both worked at Tribal Voice managed to edit a copy of the company's PowWow instant messaging client to make it behave better now that all of its lookup servers and other server-side tech was gone.  The program didn't support NAT (RFC-3022 was introduced in January 2001, the same time Tribal Voice was shuttered), but it still worked okay if you manually set up port-forwarding on your router.  The server at http://powwow.jazy.net/ hosts a copy (usual warnings about downloading and running untrusted code from random internet servers apply). I occasionally use some tools like Funduc Software's Search and Replace and Application Mover when I need to make mass-edits to text-based files or move programs with a hard-coded installation directories, respectively.  When I need to figure out the exact LCD panel inside of a laptop, EnTech Taiwan's Monitor Asset Manager is my go-to tool for that purpose. JD Design's website (now hosted on github.io) has a number of interesting freeware and shareware utilities.  I used to use their TouchPro utility to set the file timestamps on software I was mastering to match its version number (e.g., version 3.00 of a program had all of its files dates set to 3:00AM, and so forth). Karenware has a number of interesting freeware utilities, too. Regards, Aryeh Goretsky  
    • I still use HexChat! Not really as ancient as the 1994 AutoCAD above my post, but I have never found anything better to replace it. Yes we still operate an IRC server https://www.neowin.net/irc/ 😛 
    • At work we still have a couple of people that use a version of AutoCAD LT purchased in 1994. This predates Windows 95 and works fine on versions of Windows up to XP. Its long since run in an locked down isolated XP VM, accessible via RDP. I did install LibreCAD for them, however they said it was just too different to get to grips with. In all fairness one of them is now 75 and the other is almost 60.
    • On my music making (non internet) PC Sony Acid Pro 7.0 Adobe Audition 2015 Korg Legacy Collection Windows 7 SP1
    • Anyway to download these versions without being on the Experimental builds?
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      82
  • Tell a friend

    Love Neowin? Tell a friend!