Search code examples
vbaexcelsoftware-distribution

Converting .xlsm file into exe


I have build a tool using VBA (.xlsm) and would like to convert it to an .exe file. I would like that excel icon is not showing once converted in to an .exe file (acting as a custom app). I know you can create a shortcut and change the icon, but would like completely to remove the excel icon, or at least still have the excel file somewhere in the folder but be accessible via .exe icon.

Any idea what is the best way to do it?

Thanks a lot!


Solution

  • If you are limited to developing in Excel, you can try using VB script to launch Excel.
    How do you do that?

    1. Using Notepad, create a .vbs file. How do you create one?
      Just save the file and then select All Files(*) in the File Save As Dialog Box.
      Create a useful name and use .vbs extension.
      And thats it, you now have a Vb Script executable file.

      enter image description here

    2. Then write the code to open and launch your Excel.
      You can take a look at what brettdj has to say here.
      Take note of David's comment though. If you need to open the workbook in Readonly (no need to save workbook, it is a template) or ReadWrite (needs saving the workbook after running macro) mode. Simplest code would look something like what brettdj wrote. Let's just pay attention to opening the workbook as ReadOnly.

      xlFilePath = "C:\User\Username\myexelfile.xlsm"
      Set xlWb = xlApp.Workbooks.Open(xlFilePath, 0, True) '/* opens as ReadOnly */
      

      Change True to False to make it ReadWrite. Save and your exe file is done.

    3. Since you want an executable file, I assume you have forms in Excel.
      If my assumption is correct, you can then use Events to close and terminate Excel.
      Something like below which closes the workbook after closing the Userform.

      Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
          ThisWorkbook.Close False '/* do not save changes */    
      End Sub
      
    4. You will have to use another Event on the workbook itself to close the Excel Application.
      Which should look something like below:

      Private Sub Workbook_BeforeClose(Cancel As Boolean)
          Application.Quit '/* quits Excel before actually closing the workbook */
      End Sub