Display Excel inside VB6 / VB.NET Form & work with Excel without excel menu - vb.net

I have an Excel sheet with Macros enabled. I want to display the Excel sheet inside a VB6/VB.NET form without excel menu header and perform excel activities inside the form.
Please note that I need to perform some macros actions when some cell is entered...
My Objective is to create a standalone VB6/VB.Net Form in which excel cells should be displayed for performing marco actions.
I have tried searching for any sample but no luck. It should be something like this...
Can any one hint me to achieve this ?

As far as I can tell - this is not possible. The reason being that you cannot subclass the excel window in VBA.
If you program outside of excel with VB.NET or VB6 it would be sort of possible, but would probably not work really good.
If you only want to display some data from a sheet and perform some small actions on them, you could probably read the Data via the EPPLUS Library, display them as a datatable (EPPLUS can convert between datatable and excel files), perform your actions and save them to the file afterwards. But this would only work in VB.net

I did a lot of research into this last year. There is no available package to do what you are asking that is provided by MS or for free, however, you could create a datagridview (DGV) in vb.net and load the data into the DGV via ADO.NET. The data loaded into the cells will mirror your spreadsheet. Then when you modify the cells in the DGV, have a label as you have shown to update the data changes back to the excel sheet.
http://social.msdn.microsoft.com/Forums/en-US/7a1c828d-04ed-4a8d-927d-3649f29d2060/import-data-from-excel-sheet-into-datagridview
As far as calling the macro, use the excel interop feature to call a macro:
http://social.msdn.microsoft.com/Forums/office/en-US/2e33b8e5-c9fd-42a1-8d67-3d61d2cedc1c/how-to-call-excel-macros-programmatically-in-c
There may be some third party support for what you are doing, but of course it costs money:
http://www.componentone.com/SuperProducts/SpreadStudio/?gclid=CLKIy6TqwLkCFYZlOgodhToAZg

Related

Hiding or not displaying excel VBA UDF module/code

I wrote a simple UDF in Excel VBA. I saved it as an Add-in and imported it in so that I can use the function like any other excel function. But whenever I open an Excel workbook and subsequently VBA window to create a macro, the module I wrote is displayed in the Project Explorer window and its code in the coding window.
Is there any way I can hide or not display every time I open VBA?
Please let me know if you need more information. Thanks in advance!

Excel 2007 ComboBox without VBA

I am trying to generate a combo box on a worksheet without using VBA. I am very familiar with the process through VBA, but I would like this workbook to be maintained by users with no VBA experience. I know there is a way to generate and fill a combo box without VBA but it has been eon's since I have used it. Any refresher guidance would be appreciated.
You are thinking of Data Validation.

Standalone code for Excel

Can VBA code be written to perform actions on any Excel file?
When I create a project in Visual Studio, it asks for an Excel file to be linked to it. All the code that I write is in ThisWorkbook.vb and hence acts only on the Excel file linked to the project.
Ideally, I want a script that:
When the user double-clicks, he/she should be allowed to select an Excel file of choice for the actions to be performed on that file.
If the above is not possible, I'd at least like to invoke the VBA script from within an Excel file.
Basically, the VBA code should be independent of any Excel file.
I looked at VBScript, but the syntax for it seems to differ slightly.
You've mentioned Visual Studio, VBA, and VBScript. The solution I'm outlining works directly with VBA rather than Visual Studio or VBScript. (However, you might adapt Visual Studio (C# or VB) along the lines of what I'm outlining below.) Hope it's helpful, so here goes:
Here's what I do, and, it ultimately results in an .xlam Excel AddIn as #chris above has commented.
Overview
I start with ordinary .xslx workbook, to which I add a VBA project, making it an .xlsm. Then create some VBA Subs (macros). Then create some Excel QAT (Quick Access Toolbar) buttons for the workbook, which are bound to (i.e. they invoke) the VBA subs/macros. Then convert the workbook (with VBA in it) to an .xlam, which is an Excel AddIn. When you're all done, the buttons are accessible from any workbook (and the VBA code has access to any user workbooks as well as those originally in your .xlsm). Further, the workbook associated with the .xlam is invisible. So it just looks like you've added some buttons to the QAT that appear on all users .xlsx windows. The .xlam is pretty easy for users to install (though I provide a buttons to uninstall/reinstall/check version). You can upgrade an .xlam independently of users' workbooks; users' workbooks can thus be data only (.xlsx, no VBA).
Details
Write some Excel Subs you want to use later
You need to be aware that the buttons can only invoke macros (VBA Subs) without parameters, so the macros will have to check things like ActiveSheet and ActiveWorkbook and Selection to figure on what sheet the button was pressed, hence what user data to really operate on. (If you need to refer to your workbook with the VBA code in it, use “ThisWorkbook”). You should be aware that there can be naming conflicts, so try to name the parameterless subs with rather long names, such as MySomewhatUniqueProjectName_button1, etc…
Add Buttons to your .xlsm
Using Excel 2010 (I think this works with 2007 or later), I put workbook-specific buttons on the QAT part of the ribbon, which connect to macros (VBA subs) in the VBA code.
To do this, you from the Quick Access Toolbar customization drop down (the tiny down arrow at the very top row of the Excel window, the last icon from left to right) choose "More Commands…". When the “Customize Quick Access Toolbar” dialog box comes up, from the (2nd) "Customize Quick Access Toolbar:" heading (top to the right), choose "For XYZ.xlsm" from the dropdown instead of the "For all documents (default)". And from under "Choose Commands From:", use "Macros" (instead of “Popular Commands”) from the dropdown. Once you have those two things selected, you can move VBA subs from the left box to the right box using “Add >>”. Each so moved will become buttons visible in your QAT. As you’re doing this you can also edit the icon and text for the buttons, add a separator as needed (I always end with a separator in case other .xlam’s use the QAT). (Now is a good time to save this .xlsm.)
Convert the .xlsm into a .xlam
Then I convert the .xlsm to an Excel add-in, by merely saving it as an .xlam file. This will end up (1) hiding the workbook associated with the code you have (though it is still accessible to itself.). Further, now, (2) the (invisible, as now it's an .xlam) workbook will load whenever Excel is loaded. (To keep this fast for when users use Excel but don’t run my VBA code, I don't run any code when the .xlam is loaded, I only run code when a button is pushed.)
You can manage the AddIn using Excel’s AddIn manager. To update the AddIn, you have to use some trickery. While you can copy over it when Excel is not running, on the other hand, you cannot directly uninstall the AddIn, you can only disable it from Excel. But once disabled, you can delete the .xlam, and relaunch Excel, go to the AddIn manager to try to work with the (now gone) AddIn and you’ll get Excel saying it can’t find it, so do you want to delete it. Say yes, and it will be uninstalled.
FYI, Notes
I keep the .xlsm to edit later, but you can actually debug and edit the .xlam and later convert it back to an .xlsm with a minor bit of trickery: find its "ThisWorkbook" entry in VBA, and then the "IsAddIn" property, set to false, its workbook will suddenly appear and you can save as .xlsm, or edit its workbook and set the property back to true to resave as .xlam directly.)
Answer 1
You can do that in VB.Net too. Just make a regular project. (comment by #SLaks)
This worked for me very well and was exactly what I was looking for.
Answer 2
The very descriptive answer posted by #ErikEidt
I haven't tried this, but it seems like a good and alternative way of getting macros to work.

Please let me know feasibility using Excel and VBA programming and suggest solution approach

We have following requirement to be done via excel and VBA programming. Please check and let me know if it feasible to do so and if not what way would be best to do it.
There would be excel spreadsheet say excel 2007/2010 with 2 tabs.
1st tab contains normal excel data
In 2nd tab user can enter a particular value and then click on a button in same tab. Is it possibly to have a button in excel tab and can it raise events like windows/web applications?
Now if button can be kept in excel and it can raise button click event then on such an event we need to create xml files. Is it possible to create xml files adhering to schema using excel &/or VBA programming
You need to activate the developer tab (from backstage go to Options and check the visibility of this tab) to get access to normal windows controls, like buttons and drop-down lists etc. You can easily place those in Excel sheets and write event handlers: the controls support the full range of events, including OnClick, OnMouseEnter etc...
You can write custom text files directly from VBA, but you can't validate those. The usual way to do so is just to concatenate small XML blocks into a bigger file, so XML output is more like a normal text output. But it works fine and as long as you do not need to perform something really complicated it is also ok.
EDIT: You can easily add references to custom COM objects (.dll or .ocx) and use them directly in your VBA code. One very helpful suggestion (see the comment below) is to use the MS XML library.
To add a reference just open the code editor (Alt + F11) and then go to Options -> References. Most of the references you need will be already in the list and you just have to check them, but you can also add your own custom libraries by clicking on Browse.

Excel VBA - Run macro on Application open

I've got this Design Template I want to apply to the application by default whenever it's opened. But a document needs to be open in order to apply a Design template. So I have to target some handlers whenever a new or existing workbook is opened/created.
So which approach can I take here?
I think you can use the Workbook_open event. It sounded like that would be sufficient for the behavior that you want, but since you want your macros to be global, you might need to make them into an add in for them.
The add-in approach is best for applying to existing workbooks.
If you're looking to have a standard design template applied to each new workbook, though, I suggest you create a template.
To do this:
Create a new empty workbook
Apply the design template
Save the file as Book.xlt (for Excel 2003; Book.xltx for versions 2007, 2010) in the following folder: %appdata%\Microsoft\Excel\XLSTART\
Each time you create a new workbook (by opening Excel or hitting the "New" button), it'll be generated from this template file.