I have a workbook with several PowerQuery queries to other workbooks.
This workbook is updated annually at each FY - meaning the queries need to point to new workbooks every year. This can of course be done manually, but there are a lot of queries and the average user does not know how (and also, much of the PowerQuery functionality is intentionally hidden from the user).
So I have written some code to allow the user to select a new workbook to point to, and then this code updates the PQ formula with the new source workbook. This all works fine.
The problem is, I need to ensure the user has selected a valid workbook so that the queries still work. My plan was to simply update the formula with the new workbook name, and then run a Refresh, and capture any errors (if the refresh works fine then my assumption is the workbook selected is a valid one).
But here's the problem. When I use the .Refresh method in VBA, no error is reported. There IS a user dialog reporting an error, but nothing is captured by VBA - the code continues running as if all was OK, and no error number is raised.
And moreover, the dialog does not react to Application.DisplayAlerts = False. It still shows, then the VBA code continues as if nothing has happened.
So does anyone know if there is any way I can capture this error? FYI the code I am using for the reresh is as follows (simplified):
Application.DisplayAlerts = False ' This does not stop the user dialog!
Dim conn as WorkbookConnection
Set conn = ThisWorkbook.Connections("MyQueryName")
conn.Refresh 'No error raised here, even if the Refresh fails
(there is however a user error dialog)
(NB the issue is the same if I use conn.OLEDBConnection.Refresh)
FYI according the MS documentation, I SHOULD be able to stop the dialog boxes, but it is not working for me (see https://msdn.microsoft.com/VBA/Excel-VBA/articles/workbookconnection-refresh-method-excel)
Thanks!