XLS files still contained Macros

Hi,

There are some xls files still contained Macros after I removed it. Here is my code:

Aspose.Cells.LoadOptions opt = new Aspose.Cells.LoadOptions();
opt.MemorySetting = MemorySetting.MemoryPreference;
Workbook workbook = new Workbook(inputXlsFile, opt);
if (workbook.HasMacro)
{
workbook.RemoveMacro();
}

I’m using Aspose Cell 17.5.0.0.

Best regards,
Han Luongsample_files.zip (116.1 KB)

@HanLuong,

Thanks for the sample code and template file(s).

Well, my antivirus displayed a possible threat (Threat Detail | ESET Virusradar) when I tried to download your attached zipped archive, so I could not download it. Could you scan your files to re-archive and then re-attach the files, we will check it soon.

By the way, could you try our latest version/fix if it makes any difference:

Thank you.

sample_files_pass_123456.zip (113.1 KB) Pass extract archive file:123456

I tried with Aspose Cell 17.7, but my issues still remained. I attached sample files again, could you double check it.

Thanks,

@HanLuong,

Thanks for the archive.

Your files are still infected. Anyways, I did disable my antivirus for a few minutes and extracted both files. I then opened first file into MS Excel but I was surprised it started generating lots of files/sheets via some macros or something else, so I have to manually end the MS Excel process and removed the files. I am afraid, I may not have the courage to evaluate the template files by opening them into MS Excel manually as it can affect my pc. So, it is requested to kindly do provide us clean and simple Excel files, so we could evaluate your issue.

Thank you.

@Amjad_Sahi,

I just have those sample files, so could you try to prepare a clean snapshot virtual machine to check it?

Thank you,

@HanLuong,

Could you open the first file into MS Excel and let us know how it goes? Give us some screenshots taken when it is finally opened and display huge list of files it generated via some macros or else.

Thank you.

sample_files_opened.zip (218.4 KB)
@Amjad_Sahi,

Attached screenshots after I opened these files. Let me make clear my question:

  • I used Aspose Cell 17.5 and 17.7 to read these files and check macro by sample code here:
    Aspose.Cells.LoadOptions opt = new Aspose.Cells.LoadOptions();
    opt.MemorySetting = MemorySetting.MemoryPreference;
    Workbook workbook = new Workbook(inputXlsFile, opt);
    if (workbook.HasMacro)
    {
    // Return has macro
    }else
    {
    //No macro
    }
    

Then it returned No macro, but when I checked it by manually opened using MS Excel, I saw that it contained macro. So, how can I check macro in these files and removed it?

Thanks

@HanLuong,

First of all, I have to disable my antivirus to download and extract the files from your attachments (file_1.xls and file_2.xls). If I open the first file into MS Excel, I got a sort of infinite loop creating lots of spreadsheets/worksheets for quite sometime, so, I had to manually close the process via task manager. Then I evaluated your issue by using the following sample code with your template file and found Aspose.Cells does not detect macros in the file, I even removed macros and re-saved the file but still the macros are there in the file.
e.g
Sample code:

Aspose.Cells.LoadOptions opt = new Aspose.Cells.LoadOptions();
opt.MemorySetting = MemorySetting.MemoryPreference;
Workbook workbook = new Workbook(“e:\test2\file_1.xls”, opt);
if (workbook.HasMacro)
{
MessageBox.Show(“Macros”);

        } 
        else 
        { 
            
            MessageBox.Show("No Macros"); 
        } 
    
    //I manually removed macros but in vain. 
   workbook.RemoveMacro(); 
        workbook.Save("e:\\test2\\out1.xls") ;

I have logged a ticket with an id “CELLSNET-45528” for your issue. Our concerned developer from product team will look into it soon.

Once we have an update on it, we will let you know here.

Thank you.

1 Like

@HanLuong,

Please try the following codes:
e.g
Sample code:

  Workbook workbook = new Workbook(dir + "file_1.xls");
            if (workbook.HasMacro)
            {
                workbook.RemoveMacro();
            }
            for (int i = workbook.Worksheets.Count - 1; i >= 0; i--)
            {
                switch (workbook.Worksheets[i].Type)
                {
                      case SheetType.BIFF4Macro:
                        case SheetType.VB:
                          workbook.Worksheets.RemoveAt(i);
                        break;
                    default:
                        break;

                }
                
            }

            workbook.Save(dir + "result.xls");

And it’s better that you can delete hidden macro sheet by yourself, otherwise the file will be deleted by antivirus.

Thank you.

@Amjad_Sahi,

I used your snippet code to remove all macro on sample_files_pass_123456.zip (113.1 KB) , then I opened output result and saw this error error_corrupted.PNG (4.2 KB) . Can I handle this error?

Thanks

@HanLuong,

Thanks for considering Aspose APIs.

Please remember, XLSX is a newer format than XLS and it is a Macro Free Format. XLSX does not support Macros, if someone wants to use Macros in XLSX format, he will have to use another format i.e. XLSM.

If you simply save your XLS file into XLSX file, all existing Macros will be removed automatically and you do not need to worry about anything. You can keep your files in XLSX format because it is a newer format. But if you still want to save your file in XLS format, then you can convert the XLSX format into XLS format.

Please see the following sample code and its output XLSX file which is by nature Macro free.

C#

Workbook workbook = new Workbook("file_1.xls");
workbook.Save("output.xlsx");

output xlsx file.zip (23.8 KB)

@shakeel.faiz,

Do you have any other solutions? Because I don’t want to save my sample file(file_1.xls) into XLSX then convert XLSX into XLS.

@HanLuong,

I opened your Excel file and it opened so many Excel files that I had to crash the Microsoft Excel 2016 Application. So, it means, your Excel file is corrupt or it has a virus or it behaves like a virus. Amjad also reported that his Anti-Virus software detected or caught your Excel file.

If your Excel files were good, we would definitely like to fix the issues with Aspose.Cells. But since, they are corrupt, we are afraid, we cannot fix the issues in it. Thanks for your understanding and have a good day.

@shakeel.faiz

Thanks you for your response
I understand your reasoning.
But in my point of view, the corrupted file is the file which cannot be opened, Excel will threw exception or error when opening them.
In my case, my file has a macro and clearly can be opened by using Aspose Cell. Therefore, i think my ORIGINAL file is NOT CORRUPTED but it has macro.
Could you please deeply try to investigate it? This issue is very importance for me.

@HanLuong,

I have logged your concerns against your issue “CELLSNET-45528” into our database for product team’s further investigations. Our concerned developer will evaluate it if we could do something to cope with error message (by MS Excel) when opening the output XLS file into it.

Thank you.

@HanLuong,

Please remove the two defined names, it should fix your issue, see following codes:
e.g
Sample code:

  Workbook workbook = new Workbook(@"D:\FileTemp\sample_files_pass_123456\sample_files\file_1.xls");
            if (workbook.HasMacro)
            {
                workbook.RemoveMacro();
            }
            for (int i = workbook.Worksheets.Count - 1; i >= 0; i--)
            {
                switch (workbook.Worksheets[i].Type)
                {
                    case SheetType.BIFF4Macro:
                    case SheetType.VB:
                        workbook.Worksheets.RemoveAt(i);
                        break;
                    default:
                        break;

                }

            }
            //for (int i = 0; i < workbook.Worksheets.Names.Count; i++)
            //{
            //    Name name = workbook.Worksheets.Names[i];
            //    Console.WriteLine(name.Text +" " + name.RefersTo);
            //}
            workbook.Worksheets.Names.Remove("Auto_Open");
            workbook.Worksheets.Names.Remove("Auto_Close");
            workbook.Save(dir + "dest.xls");

Thank you.

1 Like

Great!!! Thanks @Amjad_Sahi, @shakeel.faiz very much.