Convert Excel files (.xlsx) to Excel Macro Enabled files (.xlsm)
In this article i will explain you that how you can convert simple excel files (.xlsx) to excel macro enable files (.xlsm). .xslx is the file extension of excel files which will be converted to .xlsm file format which is known as macro enable files. Macro enables files can run macros which contains VBA code (also known as VBA).
It is the programming language used within Excel to develop macros.
Step 1: Create one form if you are using Windows Application
I have created a sample windows form like this :
Step 2: Add reference of Microsoft.Office.Interop.Excel
Step 3: Add some namespaces to be used
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
Step 4: Create two objects like mentioned below
Excel.ApplicationClass _Excel;
Workbook workbook;
Step 5: Create a function to kill the process running in task manager
private void killProcess(string obj)
{
Process[] pname = Process.GetProcessesByName(obj);
if (pname.Length > 0)
{
Process[] proc = Process.GetProcesses();
foreach (Process pr in proc)
{
if (pr.ProcessName == obj)
{
pr.Kill();
}
}
}
}
Step 6: Create a function to release the created excel objects
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Source path from where the files will be selected
This can be anything. It's up to you. You can give any path.
Step 7: Write the below mentioned code to convert the files from .xlsx to .xlsm...
private void ButtonConvert_Click(object sender, EventArgs e)
{
string sourcepath = "E:\\Test";
string destinationpath = "E:\\Output";
string currentfilename, newfilename;
try
{
_Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
string[] filelist = Directory.GetFiles(sourcepath, "*.xlsx", SearchOption.AllDirectories);
foreach (string filename in filelist)
{
currentfilename = Path.GetFileNameWithoutExtension(filename);
// open book in any format
workbook = _Excel.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// save in XlFileFormat.xlOpenXMLWorkbookMacroEnabled format which is XLSM
newfilename = currentfilename + ".xlsm";
workbook.SaveAs(destinationpath + "\\" + newfilename, XlFileFormat.xlOpenXMLWorkbookMacroEnabled, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// close workbook
workbook.Close(false, filename, Type.Missing);
// shutdown excel
_Excel.Quit();
}
MessageBox.Show("Process completed successfully.","Process completed.",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
workbook.Close(false, Type.Missing, Type.Missing);
MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
// kill excel app process.
killProcess("EXCEL");
}
I have written the code inside the button click event. You can use only code if you are doing by another way.
Destination path to where the files will be saved
This can be anything. It's up to you. You can give any path.