delay loading of Excel using COMAddIn - com

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
}

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

Way to capture double click to open file?

Is there a way to have an Excel macro to check what file I double clicked on to open.
When I open that file, the Add-Ins installed load first, then the file I clicked on loads. How can I write code inside one of my Add-Ins to check what the filename is that I am trying to open?
I tackled this by using 2 instances of Workbook_Open inside an excel Addin.
When a file is loaded, the addin starts up, and checks to see if there are any active workbooks. If there is none, then we wait a little bit and check again, looped as many times as you deem necessary, or until there is a valid workbook open. This makes sure your computer has time to load up the excel file. Once there is an active workbook, we process it to make sure it is not the workbook of the addin. If it is, then we exit our code. If it isn't then we Do Stuff.
I solved 2 of my questions on this site with the same code that can be found HERE
Note: I left out the parts where I Do Stuff... because really it's there to do anything you want with it. The code in the link just does what the previous paragraph describes.

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.

Keep getting Excel and Windows Prompts while working with Interop Services

All of this is being done in VB.NET using the Excel 14.0 Interop Services
I am at my wits end. I keep getting prompts from windows and excel during the middle of a batch run.
The program i have takes in a workbook with batch records, then runs simulations on each batch record, then writes the results back out to the excel file.
The steps:
Open workbook
check to see if workbook is already in use by another program.
if it is in use. we try to close the workbook. then we wait for a set amount of time before trying again.
if the workbook is not in use we continue.
Get the contents
Mark the records as being processed
save and close the file.
process the records.
do the same process above to open the workbook.
save the results to the workbook.
close the workbook.
loop these processes until all the records have been simulated.
Ok the problems that can occur:
Workbook is already in use or two programs are trying to interact with the save workbook at the same time.
Ok now for the problem that i am having.
When the workbook is being interacted with by two programs at the same time. a prompt will show saying the file is currently in use.
another problem that happens that i can't explain is excel will show a prompt saying that the file is now ready to be modified with the options read-write, notify, cancel.
I need to find a way to handle these prompts programmatically.
If any one can point me in the right direction I would be very greatful.
You can prevent the prompts from appearing by setting:
Dim xlApp As Excel.Application = New Excel.Application
xlApp.DisplayEvents = False
But I've not found a way to actually "catch" the prompts and do something useful. I've noticed that often if Interop cannot get hold of a file then it will throw an exception. The exception rarely contains any way to distinguish what the actual error is, but you can sometimes work it out based on what could happen at that point.

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.