• 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

    • Server Summit had a heap of announcements, ADCS changes are baller.
    • Nice, hope they *finally* fixed the issue with the NTFS driver where the system would completely brick during large file copies using the built in driver. It's been broken for years requiring me to use the older, slower, NTFS-3G FUSE driver.
    • Windows 11 KB5094126 BSODing, freezing, forcing BitLocker lockout, breaks OneDrive, and more by Sayan Sen Microsoft released Windows 11 KB5094126 and KB5093998 last week as the latest Patch Tuesday updates. Following that the company also published the accompanying dynamic updates under KB5094149, KB5095971, and KB5094156. While Microsoft has so far not acknowledged any major problems with the release, some users online are running into problems. These range from OneDrive and Dropbox access issues, BitLocker recovery lockouts, to blue screens and BSODs. The most common one seems to be happening with HP systems wherein affected users say they hit 0xc0430001 BSOD (blue screen of death) error code after the KB5094126 update. We wonder if this could be related to the recent bug we covered on HP devices wherein the ongoing Secure Boot certificate updates are leading to similar issues. While we are not certain, users affected by this issue likely need to ensure that the boot.stl file is included on the installation media (such as a USB installer or ISO), if the above-mentioned dynamic updates are deployed. If this file is missing, computers may fail to boot from the installation media and could display the error 0xc0430001. This STL file is used by Secure Boot to verify that the boot files are trusted, so it must match the same Windows version and system architecture. To ensure the file is included, Microsoft recommends using the Update WinPE script, which automatically updates the image and handles the required files. Alternatively, you can manually copy the boot.stl file from the Windows\Boot\EFI folder on a Windows device and place it in the matching folder on your installation media before deploying the updated image. Aside from blue screening some users also note their systems have been freezing following the update. This could be happening to Lenovo PCs specifically. In the case of the OneDrive and Dropbox access issues, a user figured out that there could be a conflict with UAC. He explained: "Okay, so I did some digging, and in our environment KB5094126 breaks OneDrive and Dropbox in Explorer. I went through all our GPOs and found out that the combination of disabling UAC and having my user being a local admin breaks OneDrive in Explorer. ... If I enable UAC again, then it works, even with KB5094126 still installed." Hopefully, Microsoft will look into these issues. Source: Microsoft forum (link1, link2, link3, link4), Reddit (link1, link2, link3, link4)
    • It is when it's a desktop in my house though for a PC that's lightly used and not really important when it is. If it was a laptop, it would be a different story. The real solution is varied and begins starting at post #22 in that thread.
  • 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
      508
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!