Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 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


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

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


What I have tried:

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

            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();
        }
Posted
Comments
Richard MacCutchan 17-Jan-21 4:54am    
You are trying to access properties that do not exist. You need to open a workbook, and select one of its sheets.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900