excel vba suppress "downloading file" message - vba

I realize this question has been asked before, but the supplied answers did not solve the problem. I'm running VBA code from within Excel which does the following:
1.) Open a local worksheet (i.e. C:\desktop\AOR\Names.xlsx)
2.) Read down column B getting the names of files that need to be verified
3.) Open the filename (as taken from column B) from a Network drive.
4.) Verify some cells from the opened file
5.) Close the file.
6.) Iterate.
All this is working with no problems; my situation is that when the file is opened from the Network drive (i.e. step 3) I get the "Downloading File" message. The only option on this is "Cancel" or let it go to completion. Pressing cancel (or using SendKeys to do so) causes the file open to cancel, thus stopping the rest of the process. I've had users accidentally press this cancel enough times that I want to suppress the dialog box completely but cannot find anything that works.
Here is some code with the options I've tried all to no avail:
WhatName = Workbooks(AgencyNamesList).Worksheets("Agencies").Range("B" & i).Value ' get name of file from locally opened workbook.
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.EnableEvents = False ' Prevent code in workbook from running
' Application.SendKeys "{ESCAPE}", False ' Cancels subsequent file opens
' Application.SendKeys "{ESCAPE}", True ' Cancels the file open
' Application.SendKeys "~", True ' Cancels the file open
QuarterDueDate = Workbooks.Open(SourceFolder & WhatName).Worksheets("YTD").Range("I6").Value ' open book get value of I6
This page shows a picture of the dialog box along with someone else trying to find a solution (since 2012) to this problem.
OPSYS: Windows 7 Enterprise SP1
EXCEL: MS Office Professional Plus 2013
I'm sure hoping someone has found a way to prevent this pop-up box from popping!
NOTE: When this code is run against the same files stored locally, the dialog box does NOT display.
Thanks!

Related

How to suppress "SQL Server Login" window on Excel?

I am using Microsoft Task Scheduler to run Excel models everyday. The models retrieve data from the database and do stuff. The connection strings contain all information(username, password etc.) needed to connect to the database. However, I get this prompt sometimes:
SQL Login Prompt
When I click "OK", everything goes fine, so I need the Excel to just click OK because the username and password are already there. I want to suppress this window by clicking OK. Otherwise the models stop running and wait for me to click OK manually.
I checked for the solutions on the web but did not encounter a working one. I tried to change the connection string in various ways but it did not work.
Does anyone have any idea on how to solve this issue?
Thanks
Edit: Here is the code I am running on VBScript for Task Scheduler.
'Input Excel File's Full Path
ExcelFilePath = "MyExcelFile"
'Input Module/Macro name within the Excel File
MacroPath = "Macro1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Input Module/Macro name within the Excel File
MacroPath = "Macro2"
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
The excel VBA code for Macro1 is just
Sub UpdateModel()
ActiveWorkbook.RefreshAll
End Sub
command for pivot tables which are getting data from the database.

EXCEL 2nd Enable Editing Button appears after already Enabling

I have an excel workbook that I download using simple HTML portal that I coded. When I download the workbook it automatically renames it as "myWorkbook (27)" or whatever number of times it has been put in the downloads folder.
I have some code that changes the Save As location of the workbook to the original location that it was downloaded from. See:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
'Check to see if another workbook with the same name is open -- close if so.
If IsOpenWorkbook("myWorkbook.xlsm", True) Then
Workbooks("myWorkbook.xlsm").Close saveChanges = False
End If
ThisWorkbook.SaveAs Filename:="\\999.9.9.99\folder\anotherFolder\myWorkbook.xlsm"
Application.DisplayAlerts = True
End Sub
This has worked fine for almost a month but recently has been performing erratically. When I first download the sheet, it has the regular "Enable Editing" prompt which I accept. After accepting, the name of the workbook changes suggesting the code as run, but then has another "Enable Editing" prompt which, if I accept, will crash Excel.
Any insights appreciated.

Auto clicking on a message box using VBA

I am automating testcases for my company's addin.
In one of the testcases, after saving my workbook on my local machine when I try closing the workbook, I am required to click on 'Yes'- saves edited data in our database, 'No'- discards the edits or 'cancel'- simply closes the message box.
I have tried using sendKeys but that didn't work for me.
Eg: For clicking 'No' through my VBA code, I tried to send tab and enter keys like this:
Application.Wait (Now() + TimeValue("00:00:10"))
Call SendKeys("{TAB}", True)
Call SendKeys("{ENTER}", True)
I have tried with various time values still didn't get any success. I would be glad if someone can help me with this.
Thanks in advance! :)
UPDATE: As of now, I have been doing this:
'Some tasks...
ActiveWorkbook.Save
Call Application.Run("workbook.xlsm!Discard")
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True
But is there any way to get the window object reference for the message box? Or anyway to stick to the workflow: edit workbook -> save -> try closing -> opt to discard/commit changes to database?
NOTE: By discarding/committing changes to database, what I mean is a server where the data gets updated. Excel is just an interface for the user. I don't want just the excel to be saved but the operation of calling discard/save changes on server as well...
if you have already saved the workbook, you only need to close the workbook. Please try the below code
Activeworkbook.close false
This will close your workbook and you wont be promted for selecting any options.

Catching FileFormat property of .XLAM without confusion with .XLSM

Context
I have developed a .xlam add-in that contains user-data inside. In other words, the user can decide to show the add-in file through a ThisWorkbook.IsAddIn = False to edit the content, which is functional to the add-in itself.
However, the user should not be able to perform some operations when he's/she's working on the add-in's spreadsheets rather than on the normal workbook where the Add-In is running.
Need to check for file extension
From here, it comes my need of checking for the file extension and validate it when some specific "forbidden" procedures might get called. I have made the following tests:
If ThisWorkbook.IsAddIn = True, then ThisWorkbook.FileFormat = 55;
If ThisWorkbook.IsAddIn = False, then ThisWorkbook.FileFormat = 52;
The source of confusion
This is not what I was expecting. By simply executing a FullName request when the Add-In is set visible:
ThisWorkbook.IsAddIn = False
MsgBox ThisWorkbook.FullName
I can read that my file is still named C:\myFile.xlam, even if in that moment is visible to the user. So, I would expect ThisWorkbook.FileFormat to raise a 55 even if visible at run-time. But it doesn't do that, apparently.
The question
I need to make sure to distinguish between modifications on the Add-In (.xlam) and modifications on a possible .xlsm file that the user created, from which is using my Add-In.
Why is the FileFormat of my add-in being equal to the one of an xlsm, if the file is clearly xlam to which is associated a 55 instead of a 52? Where am I being wrong?
EDIT - Example of the action to forbid
On the ribbon there's a button created and added from the add-in, which is connected to a macro that cannot be run into the Add-In. So the check I had in mind was something like this:
If ActiveWorkbook.FileFormat = 55 Then
Exit Sub
End If
However, as said above, this check will not be performed because the Add-In has FileFormat = 52 in the moment in which is set to .IsAddIn = False; hence, even if the ActiveWorkbook is the add-in where I do not want to run the macro, the check will fail and the macro will run anyway.
The .IsAddIn workbook property simply indicates whether the file is being run as an Add-in. It does not change the file format. From the documentation:
When you set this property to True, the workbook has the following characteristics:
You won’t be prompted to save the workbook if changes are made while the workbook is open.
The workbook window won’t be visible.
Any macros in the workbook won’t be visible in the Macro dialog box (displayed by pointing to Macro on the Tools menu and clicking Macros).
Macros in the workbook can still be run from the Macro dialog box even though they’re not visible. In addition, macro names don’t need to be qualified with the workbook name.
Holding down the SHIFT key when you open the workbook has no effect.
I sense that this is the real problem you're trying to tackle:
However, the user should not be able to perform some operations when he's/she's working on the add-in's spreadsheets rather than on the normal workbook where the Add-In is running.
Perhaps it will be best if you can specify what actions you're trying to restrict? There may be a better way to solve this.
For the moment I have found four possible solutions, that I'm going to post here just in case someone would have my same issue:
Comparing the full names - credit to Tim Williams
The "special code" cannot run if the full names are different:
If ActiveWorkbook.FullName = ThisWorkbook.FullName Then
Exit Sub
End If
'"special code"
Comparing the isAddIn property - credit to David Zemens
The "special code" cannot run if this workbook is not currently an add-in:
If ThisWorkbook.IsAddIn = False Then
Exit Sub
End If
'"special code"
Comparing the two objects
The "special code" cannot run if the active workbook is the add-in workbook:
If ActiveWorkbook Is ThisWorkbook Then
Exit Sub
End If
'"special code"
Checking for "xlam" extension
The "special" code will not be run if the extension of the file is xlam:
If Right(ActiveWorkbook.FullName,4) = "xlam" Then
Exit Sub
End If
The four solutions above work fine for the purpose, but the question is still opened : why the FileFormat property changes over the same file depending on ThisWorkbook.IsAddIn being False rather than True?

VBA Dialogs.Show doesn't display warning message

Have Excel (2010 in my case but I think it will be the same also in other versions) with two workbooks. Save first one with name "1.xlx" (example) via Save As dialog. Save second one with the same name "1.xlx" to different location. Excel will not allow it with following warning message:
"You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name, or close the other workbook or add-in before saving."
So far so good. But my problem is that I need to invoke dialog via VBA. I am using following code:
Sub test()
Application.Dialogs(XlBuiltInDialog.xlDialogSaveAs).Show
End Sub
Now I am trying to save second workbook (with the same name to different location) but when I click to 'Save' button nothing happen, no warning message. If I wouldn't know what is wrong it would be very difficult to tell. I didn't change any setting (there is nothing as DisplayAlerts set to true or so). Any idea how make SaveAs dialog invoked via VBA to display similar warnings?
I'm not sure why that doesn't give you an error, but it doesn't me either. If you use Application.FileDialog, you can get that error.
Sub testts()
With Application.FileDialog(msoFileDialogSaveAs)
.Show
.Execute
End With
End Sub
Or you could use GetSaveAsFileName and check the names of all the open workbooks and generate the error yourself.
Can you try with the below code on starting on your code.
Application.DisplayAlerts = True