K
ksminor
Hi All.
I am using access to open, refresh, and close an excel spreadsheet. The
code below works fine as far as opening and closing the spreadsheet,
but it doesn't refresh the data. Any idea why?
Thanks in advance for any help.
Private Sub Command2_Click()
Dim MyXL As Object, xlFileAttribute As String
Dim xlFile As String, xlpw As String
DoCmd.SetWarnings True
xlFile = "c:\ActualHires.xls"
'Set file attribute
xlFileAttribute = GetAttr(xlFile) ' Returns 1.
If xlFileAttribute = 1 Then
SetAttr (xlFile), vbNormal
End If
'Defer error trapping.
'If the application isn't running, an error occurs.
On Error Resume Next
Set MyXL = GetObject(, "Excel.Application")
Set MyXL = CreateObject("Excel.Application")
'Clear Err object in case error occurred.
Err.Clear
'Disarm all warnings
MyXL.Application.DisplayAlerts = False
MyXL.Application.AlertBeforeOverwriting = False
MyXL.Application.Visible = True
MyXL.Workbooks.Open FileName:=xlFile, Password:="ABCD"
'Update Pivottables and Save the File
MyXL.Application.ActiveWorkbook.Sheets("sheet1").Select
MyXL.Application.ActiveSheet.Range("A16").Value = "opened"
MyXL.Application.ActiveWorkbook.Save
MyXL.Application.ActiveWorkbook.Close
'
'
' 'Save Excel file, close & destroy all Excel objects
' 'Arm all warnings before quiting Excel
MyXL.Application.DisplayAlerts = True
MyXL.Application.AlertBeforeOverwriting = True
MyXL.Application.Quit
Set MyXL = Nothing
End Sub
I am using access to open, refresh, and close an excel spreadsheet. The
code below works fine as far as opening and closing the spreadsheet,
but it doesn't refresh the data. Any idea why?
Thanks in advance for any help.
Private Sub Command2_Click()
Dim MyXL As Object, xlFileAttribute As String
Dim xlFile As String, xlpw As String
DoCmd.SetWarnings True
xlFile = "c:\ActualHires.xls"
'Set file attribute
xlFileAttribute = GetAttr(xlFile) ' Returns 1.
If xlFileAttribute = 1 Then
SetAttr (xlFile), vbNormal
End If
'Defer error trapping.
'If the application isn't running, an error occurs.
On Error Resume Next
Set MyXL = GetObject(, "Excel.Application")
Set MyXL = CreateObject("Excel.Application")
'Clear Err object in case error occurred.
Err.Clear
'Disarm all warnings
MyXL.Application.DisplayAlerts = False
MyXL.Application.AlertBeforeOverwriting = False
MyXL.Application.Visible = True
MyXL.Workbooks.Open FileName:=xlFile, Password:="ABCD"
'Update Pivottables and Save the File
MyXL.Application.ActiveWorkbook.Sheets("sheet1").Select
MyXL.Application.ActiveSheet.Range("A16").Value = "opened"
MyXL.Application.ActiveWorkbook.Save
MyXL.Application.ActiveWorkbook.Close
'
'
' 'Save Excel file, close & destroy all Excel objects
' 'Arm all warnings before quiting Excel
MyXL.Application.DisplayAlerts = True
MyXL.Application.AlertBeforeOverwriting = True
MyXL.Application.Quit
Set MyXL = Nothing
End Sub