View Full Version : Resfreshing an excel sheet from Access


Amileaux
04-15-2004, 06:33 AM
The following is code associated with a form that opens up an excel template. This template is tied to a table in access and I want to refresh the table. I recorded a macro in excel to do just that and the code came recorded as follows:

Range("A2").Select
Selection.QueryTable.Refresh BackgroundQuery:=False

However, I can't seem to get it to work from Access. Following is my code: I have two tabs in Excel (Report and Summary). It is the Report tab that I want to refresh. Everything else is working fine. Thank you.

Private Sub cmdPrepExcel_Click()
Dim xlapp As Excel.Application
Dim wkbtemplate As Excel.Workbook
Dim wksRpt As Excel.Worksheet
Dim boolXL As Boolean
Dim i As Integer

'Open up template in excel
If fIsAppRunning("excel") Then
Set wkbtemplate = GetObject(strFILEPATH & "PercentUtilization.xlt")
Set xlapp = wkbtemplate.Parent
boolXL = False
Else
Set xlapp = CreateObject("Excel.Application")
Set wkbtemplate = xlapp.Application.Workbooks.Open(strFILEPATH & "PercentUtilization.xlt")
boolXL = True
End If
xlapp.Visible = True

'Open template and save
Set wksRpt = wkbtemplate.Worksheets("Report")
wksRpt.PageSetup.RightHeader = Now()
wksRpt.Range.Select ("a2")
wksRpt.QueryTable.Refresh BackgroundQuery:=False
Set wksRpt = wkbtemplate.Worksheets("Summary")
wksRpt.PageSetup.RightHeader = Now()
For i = 1 To wksRpt.PivotTables.Count
wksRpt.PivotTables(i).RefreshTable
Next