Open Excel document from Access issues - vba

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.

Related

When opening any excel file in system, excel file running in background created by windows form (vb.net) also appears

My windows form opens an excel file (let's say Test.xlsx) in background to read and write content. Test.xlsx file stays open in the background as long as the form is open. Excel visibility has been set to False in the code. But, when I open other excel files in the system, Test.xlsx is also appearing. Not sure why this is happening. Check code in below hyperlink.
Code
If you don't want 'Test.xlsx' file to appear, you need to close it first.
xlWb.Close(0)
xlApp.Quit()
Besides, close excel in form_closing event is not a good choice, because if the form does not close properly, the resource will not be released, then you will see multiple 'Excel.exe' in your task manager.
Release it in time.
xlWb = xlApp.Workbooks.Open(link)
'...
xlWb.Close(0)
xlApp.Quit()

auto answer to a message box on updating links

Hi have a macro that open some excel files, take some rows and close the file.
Everything works like a charm but I need to add a small feature.
On some of the files, once open, I see the message about some reference to external files missing and the option to update or not the sources.
The following is a picture of the message.
I need to click each time on "don't update" and I'd like to authomatize this action while the macro runs.
How to do it? From my research I have found how to dismiss completely messages from excel while the macro runs but I'm not sure this will solve my issue
Since you are only reading data so open them read only and tell VBA not to update the links so it will not ask you that at all:
Dim WB As Workbook
Set WB = Application.Workbooks.Open(Filename:=MyWorkBook, UpdateLinks:=False, ReadOnly:=True)
Where WB is the workbook that gets opened and MyWorkBook is the full name (path of the workbook) e.g. C:\MyDrive\MyWorkbook.xlsx

Excel application object in VB (apparently) prevents normal use of Excel while VB program is running

I have made a Visual Basic program in Visual Studio (NOT VBA) that creates an instance of Excel and uses it throughout the program to open existing/create Workbooks. The problem I'm encountering is that any time after the instance of Excel is created, I am unable to completely open a workbook normally (from within windows explorer). I say 'completely' because Excel DOES appear open, but the menus and workbook itself don't actually populate. The Excel border just sits there and the busy cursor is shown when I hover over it.
Once the VB program is closed and that object is released, Excel returns to 'normal' functionality. Additionally, when I quit the VB program, the partially opened workbook also goes closes, almost as though it was attached to the instance of Excel I created in code.
The instance of excel created should be totally silent and the user should never even know it's being used. To that end, I'd like the user to be able to open other workbooks as though Excel isn't already being used elsewhere. This is important because other Excel workbooks might need to be opened for reference by the user during runtime.
I declare the object this way because there are many sub routines in various modules that all might want to use the instance of Excel.
Public Shared XLapp As New Excel.Application
The following code runs when the first form opens in the applicaion. Once this bit runs and the XLapp object gets set to a new Excel.Application, I lose the ability to open a workbook normally, as described above.
If IsNothing(XLapp) Then XLapp = New Excel.Application
XLapp.Visible = False
XLapp.DisplayAlerts = False
XLapp.EnableEvents = False
XLapp.ScreenUpdating = False
After creating a workbook object, I'll later in the code, and in various places throughout, open a workbook typically as below:
Dim OpenedWorkbook as Excel.Workbook = Nothing
[...]
OpenedWorkbook = XLapp.Workbooks.Open(workbook_filepath)
I've always been under the impression that if I'm opening workbooks using this particular object (XLapp) that when a workbook is opened in windows explorer that a new instance of Excel would be created. That is to say, the instance of Excel my program creates should be isolated from other instances of Excel, but that doesn't seem to be the case here.
Is there something else I need to do (or have I done something wrong?) to allow the user to use Excel normally while my program is running?
Thanks in advance for the help and for having patience with this VB newbie!
(Sorry, too long for a comment.)
You might be using the wrong tool for the job. You write:
The instance of excel created should be totally silent and the user should never even know it's being used. To that end, I'd like the user to be able to open other workbooks as though Excel isn't already being used elsewhere.
The purpose of Excel interop is to remote-control Excel, just like a user would interact with it. However, users don't interact with silent, non-visible Excel windows. It's just not what Excel interop is made for.
I suspect that you don't really want to remote-control a locally installed Excel instance. What you really want to do is to open and manipulate Excel workbooks. Then do just that: Use one of the Excel libraries for .NET and modify the Excel files directly (personally, I like SpreadsheetLight, but others are fine as well). Additional bonus: Your users don't need to have Excel installed.
You should refer to this post ... How do I properly clean up Excel interop objects?
Basically, when you create the Excel object, and don't dispose of it correctly, it hangs around sucking up legitimate Excel file open requests but not acting on them!
Fixing this type of error is time consuming, because the worksheet/cell/etc references all need to be disposed of correctly.
Furthermore, if you use more than one '.' in an instruction (eg Parent.Child.GrandChild) then you are in even more trouble since you create a reference to Child, but don't store it anywhere, and thus can't dispose of it!
As #Heinzi mentions, you could well fare better by using a different library rather than Excel interop if all you are doing is reading/writing values. I've used EPPlus with no issues so far (add via Nuget)!

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
}

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.