0

I have a VBA function that opens a file, refreshes all the data connections, saves the file and the returns "true" if the refresh was successful. Here is the code:

Public Function RefreshFile(ByVal wbName As String, ByVal FilePath As String, ByVal pword As String)

Dim blTmp As Boolean
Dim wbRefresh As Workbook

blTmp = Not (IsWorkBookOpen(FilePath))

If blTmp Then
    Workbooks.Open Filename:=FilePath, ReadOnly:=False, Notify:=False, WriteResPassword:=pword, IgnoreReadOnlyRecommended:=True
Else
    RefreshFile = False
    Exit Function
End If

Set wbRefresh = Workbooks(wbName & ".xlsx")

wbRefresh.Activate
ActiveWorkbook.RefreshAll
DoEvents

'MsgBox "complete"


wbRefresh.Close savechanges:=True

RefreshFile = True

End Function

The list opens a series of files and some are password protected and read-only suggested (hence the parameters passed). With the read-only suggested files though, files get saved with only one of the connections refreshed. Basically it refreshes the first connection and then saves the file.

I've tested this a number of times and if I stop the function after 'DoEvents' (notice the MsgBox to stop the code), then all the connections are in fact refreshed. If I don't stop the code and reopen the saved file later on, only one of the connections get refreshed. Is this an issue with 'RefreshAll' or when it goes to save the file? Perhaps I'm not opening the file with the full, appropriate permissions??

AGryckiewicz
  • 109
  • 3
  • 13
  • 1
    You are not alone ;) [Wait until ActiveWorkbook.RefreshAll finishes - VBA](https://stackoverflow.com/questions/22083668/wait-until-activeworkbook-refreshall-finishes-vba). As I am not an Excel expert. I wonder why you use `ActiveWorkbook.RefreshAll` instead of `wbRefresh.RefreshAll` which is the Workbook object to refresh? – BitAccesser Jul 01 '17 at 03:37

2 Answers2

0

The solution is that 'enable background refresh' needs to be unchecked on each data table. Don't ask me what exactly this means, but after a lot of trial/error, I finally got it to work by unchecking the 'enable background refresh' option on the data connections for the data tables.

AGryckiewicz
  • 109
  • 3
  • 13
0

I looked at this myself, and when looking at the "enable background refresh", I noticed there is a button for "Refresh this connection on Refresh All". So I had to tick this for it to work for me.