Open workbook and select worksheet from winform - vb.net

I created two projects. One project is a windows form, and the other project is an Excel workbook project. I then added the Excel workbook project to the windows form project.
The window form is a welcome page. That page has some buttons that are supposed to open the workbook on a specific worksheet. For example, the screenshot below has a button Summary Worksheet, when the user clicks that button, I want to open the Excel project, on a spreadsheet by that name.
If my welcome page form was part of my Excel workbook project or any other workbook stored on my hard drive, I would know how to open the workbook. But because they are separate projects combined into one, I have no idea how I can access my workbook from the form. I guess I would like to add a reference to the winform for Excel.
I also attached a screenshot of my VS 2012 project window.
Welcome Page

It is one solution that is holding two projects. When you build the solution then you will have the .exe for your windows form and the .xlsx and .vsto for your Excel workbook. The easiest way to do what you need is to use Excel interop, and you'll need to know the path on disk to your workbook. The code could be something along these lines:
Imports Microsoft.Office.Interop
Dim xlApp as new Excel.Application
xlApp.Visible = True
Dim xlBook as new Excel.Workbook = xlApp.Workbooks.Open("path/toyour/workbook.xlsx")
xlBook.Worksheets("Welcome Sheet").Activate
That should get you started

Related

VBA Userform read another workbook dynamically without opening it

I'm fairly new to VBA. I'm trying to create a user form where the user can select another workbook as read only and copy some of the data to the current workbook. I don't want the other workbook to open.
I found this https://www.encodedna.com/excel/copy-data-from-closed-excel-workbook-without-opening.htm but the excel document is hard coded. Is it possible to select a workbook dynamically, through a file dialog without that workbook popping up?
Thank you

Open Excel document from Access issues

In my database I manage different Excel files. There's a button in a Report which allows to open those files, and to do it I use this linse of code (runs the code at click on the button):
Dim str_file As String
str_file = "C:\[directory of the file]"
Application.FollowHyperlink str_file
It works, and opens the file I want. The problem is that it does not set the excel program as active, when it opens the file it shows very quickly the file but for something I don't know Excel application is hiden and Access application is active. I I've experienced some problems with popup forms that because it's a popup, you can't set an active window different from the popup, but this problem solves if you close the popup before setting an active form. I've tried also lines like these above but they don't work also:
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
I found the problem. The code in Access works well, it does what it's supposed to do. The excel files I have are ".xlsm", because I need to run some code when the file is saved, I need to update some fields on Access database. So I needed to call Access from excel, and this code runs when the file opens. So I run Excel from Access and then Access from Excel. I hope that this helps someone with that problem. Remember: if you have problems whith opening files from Access and your excel files can run code, check that all runs when you really need.

delay loading of Excel using COMAddIn

I have got a question to excel community active here.
I am trying to intercept Excel workbook open event. the scenario is like this:
when a user double click a workbook in windows explorer, before opening the workbook in Excel, I would like to display a dialog box to user if he would like to open the workbook or cancel the open operation. If user clicks "YES" to open the workbook then workbook is loaded in Excel or else Excel window is empty.
Additionally I would like to capture the workbook name before it is actually visible to user.
as per my research, I found that .xls while which are associated with Excel application, Excel takes the file name and passes it to the DDE. the picture between what happens between DDE loads the file and Workbook is actually shown up, is not so clear to me. Is there any way to intercept anything in between DDE loading and workbook shown event in anyway?
So my questions are:
can we delay loading of Excel and inject some other program in between so that I could capture the name of workbook before it is actually visible to user.
is it possible via COM AddIn?
PS: people might say that its a duplicate question. but it is not.
Any help in this regard would be highly appreciated?
this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
void ThisAddIn_WorkbookNewWorkBook(Excel.Workbook Wb)
{
var result = MessageBox.Show("message", "caption",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.No)
Wb.Close();
//write here the code
}

Triggering external macro on button click from normal excel workbook (.xlsx)

Stated simply my question is as follows Is it possible for a button on a worksheet in a normal excel workbook (.xlsx not .xlsm) to trigger a macro in another file specifically an installed excel add in (.xlam).
Here is some background on why I want to achieve this. I have a workbook that many users need to be able to view but only some need to be able to update by filling in a form on another sheet and calling a macro in an add in. The worksheet should not contain any macros to avoid security warnings when opened by normal users. I can do this by having a ribbon button in the add in that the user clicks which will then check that the correct workbook is open and that the form is filled in etc. before executing the update code. However the interface would be nicer if the button instead of appearing on the ribbon was on the worksheet just below the form. Therefore my question is it possible to trigger an external macro from a button click in a non macro enabled workbook.
Yes, you can assign an external macro (which should be .xlsm file) to a non-macro-enabled workbook (.xlsx) button. My xlsm macro resides in the same directory as the xlsx workbook for simplicity. (Note: I am using Excel 2010)
Firstly, you must open your xlsm macro in the same instance of Excel window (i.e. do not open a new instance of Excel) so that your xlsx workbook will be able to see/access it.
Right-click on your xlsx workbook button and select "Assign Macro..."
Make sure you select Macros in: All Open Workbooks
All macros in open workbooks will be shown (this is why it is important to do step 1).
Select the desired macro from the list then click OK.
I don't see how you can execute a macro from a control in a non-macro-enabled workbook.

Saving Non-VSTO copy of VSTO Workbook

I am trying to save a ListObject from a .NET 3.5 Excel 2007 VSTO Workbook to a new sheet (done) and save that new sheet to a new workbook (done) without that workbook requiring the VSTO customization file (!!!!!).
Has anyone had any luck with this? The only way I've had any success is just saving as a CSV file but that's not exactly acceptable in this case. I'd rather not save to a CSV just to copy back to a XLS file.
worksheet.SaveAs(saveDialog.FileName, Excel.XlFileFormat.xlOpenXMLWorkbook)
If I understand correctly, you do not want the new workbook file to depend or load any VSTO customization?
Try this MSDN link to remove VSTO customization assemblies from workbooks.
Ok this didn't end up working for me and here's why. The answer is still correct but I want to clarify for future users.
I have a ListObject I wanted to save in an external workbook using VSTO. Creating a new Worksheet and using SaveAs would rename the current Workbook to that and therefore I would have to close the entire workbook to remove the Customization.
What I should have done from the beginning is this:
Create the Worksheet and populate the ListObject on said Worksheet. Then use .Copy() with no parameters to create a new workbook. How do i find the workbook then though? I simply named the Worksheet Now.Ticks.ToString() and looked for any open workbook with the ActiveSheet.Name as Now.Ticks.ToString(). For this application, it doesn't need to be more in depth then that. I the saved THAT workbook and then closed it. Since the workbook was created with Copy it had no customizations on it and problem solved.