I am trying to refresh an excel sheet with access. The excel sheet contains a connection string to a database and has SQL as the Command Text. Now below is my vba code in access, when I run it, the excel file opens and I am presented with the box to enter the connection parameters. After entering, a message pops up saying that "A pending refresh operation will be cancelled. What am I doing wrong? All I want is for the workbook to auto-refresh, then save and close.
Code:
Public Function refreshMedData()
Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
Set xlWrkBk2 = xlapp.Workbooks.Open("M:\BSC Benefits\PMO\Reports\Med Data Extract PROD.xlsx")
xlWrkBk2.Unprotect
xlWrkBk2.UnprotectSharing
xlWrkBk2.Activate
ActiveWorkbook.RefreshAll
DoEvents
xlWrkBk2.Save
xlWrkBk2.Close True
MsgBox "Done. Med Data Refreshed"
End Function