Home » excel » Excel VBA: Catching an external data connection that won't refresh?

Excel VBA: Catching an external data connection that won't refresh?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Hello everyone and greetings from Germany!

After searching for quite some time I am at my wits’ end and I hope someone can help me.

I try to describe my problem as clear and briefly as possible:

I am building a MS Excel 2010 Workbook that includes several (90+) external connections to SharePoint 2013 Lists & Libraries.

These connections were created by SharePoint’s integrated “Export to Excel” function (in the List/Library-Ribbon) and the connection-files were then exported to another SP farm.
(The first “source”-SP-Farm is from the customer, the second is our own intranet)

I have to refresh these connections once per day via an automated macro.

A timer-job will open the workbook at night and execute the “RefreshAllConnections”-macro,
that does a little bit more than just refreshing (such as writing the refresh date and time).
So no user is present when this happens.

And this is where my problem is:

From time to time some of those connections cannot be refreshed.
Excel displays an alert saying (translated from german):

“the following data range could not be updated: owssvr (…)
Do you wish to continue the update?
(OK) / (Cancel)

What I found out so far:

1) It’s always the Library that is the problem

2) It’s rather random which Library won’t update and when

3) The problem fixes itself after some time (that’s why I’m guessing this has something to do with the library being used/modified by someone else)

4) While a library is refusing to update, using “Export to Excel” function again will prompt an error once the new worksheet is created and the data is supposed to be filled in

Now here are the odds:

1) The alert always uses the “old/original” connection name that was already changed by me.

2) When I press OK the macro just continues in the next line, no error is thrown whatsoever

3) If I press cancel an Error 1004 occurs (which I can at least catch, so that would be okay).

And here are the probs:

Since this is happening automatically at night, there is no user sitting near by to answer these alerts. So:

1) The macro must automatically answer these alerts with “Cancel” IF they pop-up (and I have NO idea how to do that!)

2) I disable them via “Application.displayAlerts = false

HOWEVER: this will automatically answer them with the default answer, which is “OK”.
This however is not throwing an error I could catch, so my macro won’t now whether the update acutally worked or not.

Well that’s about it. Sorry for the long post and thanks for reading.

Hopefully someone of you has an idea.


EDIT:

Could it be that the automatically by SharePoint generated connections are the problem?

(How) can I build them myself?

How to&Answers:

Well. I solved it myself.

The answer was stupidly simple:

Instead of refreshing the Connection, I now refresh the corresponding QueryTable via “ActiveSheet.ListObjects(1).QueryTable.Refresh”

If the Connection is not responding, an error is thrown that I can now catch properly.

Ugh, finally!