Converting excel macro to C#

ravi kumar 331 Reputation points
2021-01-17T07:36:35.72+00:00

I am creating excel VSTO , where I already have the macro to delete excel rows based on column values(if it is greater than zero)which now needs to be converted to c# , by seeing documentation on internet I have come up so far , but still three errors are present I am not able to solve them kindly help me:

original macro:
    Sub delete2()
    '
    ' delete2 Macro
    '

    '
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("$A$1:$S$901").AutoFilter Field:=10, Criteria1:=">0", _
            Operator:=xlAnd
        Rows("8:1382").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.delete Shift:=xlUp
        ActiveSheet.Range("$A$1:$S$891").AutoFilter Field:=10
        Selection.AutoFilter
    End Sub

modified by me till now:

using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace CpCpk
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            var excelappp = new Excel.Application();
            Excel.Worksheet excelApp = Globals.ThisAddIn.Application.ActiveSheet;

            excelappp.Rows["1:1"].Select();
            excelappp.Selection.AutoFilter();
            excelappp.ActiveSheet.Range["$A$1:$S$901"].AutoFilter(Field: 10,Criteria1:">0",Operator: Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlAnd);
            excelappp.Rows["8:1382"].Select();
            excelappp.Range[excelappp.Selection, excelappp.Selection.End(Microsoft.Office.Interop.Excel.XlDirection.xlDown)].Select();
            excelappp.Selection.delete(Shift: Microsoft.Office.Interop.Excel.XlDirection.xlUp);
            excelappp.ActiveSheet.Range["$A$1:$S$891"].AutoFilter(Field:10);
            excelappp.Selection.AutoFilter();
        }

although there are no syntax error , I am getting below error during execution :

System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'

kindly help me how to solve this

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,964 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,938 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 117.6K Reputation points
    2021-01-17T08:55:59.723+00:00

    Maybe remove excelappp variable and use your excelApp instead. (It can be renamed to something like active_sheet). Remove '.ActiveSheet.' parts. What error do you get in this case?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.