I have an excel template that uses a query tied to access to update data. (Data/Import External Data/New Database Query). Usually I have to open up excel and manually update the worksheet before emailing off. So I decided to try and automate it. Below is my code, everything works fine until the "Selection.QueryTable.Refresh BackgroundQuery:=False" row.
I get the message "Object variable or With Block Variable not set". I am setting the the worksheet to work from. (I got the code by recording a macro in excel when I do the "manual" update.) Any thoughts would be appreciated. Thank you. Marie
Private Sub cmdVDSLFTTH_Click()
'On Error GoTo Err_cmdVDSLFTTH_Click
Dim I As Integer
Dim xlapp As Excel.Application
Dim wkbTemplate As Excel.Workbook
Dim wksRpt As Excel.Worksheet
Dim wksFTTH As Excel.Worksheet
Dim wksVDSL As Excel.Worksheet
'Set Object references in Excel
Set wkbTemplate = GetObject(strfilepath & "VDSLFTTH.xlt")
Set xlapp = wkbTemplate.Parent
Set wksRpt = wkbTemplate.Worksheets("DataSheet")
Set wksFTTH = wkbTemplate.Worksheets("FTTH")
Set wksVDSL = wkbTemplate.Worksheets("VDSL")
xlapp.Visible = True
wkbTemplate.Windows(1).Visible = True
wksRpt.Range("A2").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
I get the message "Object variable or With Block Variable not set". I am setting the the worksheet to work from. (I got the code by recording a macro in excel when I do the "manual" update.) Any thoughts would be appreciated. Thank you. Marie
Private Sub cmdVDSLFTTH_Click()
'On Error GoTo Err_cmdVDSLFTTH_Click
Dim I As Integer
Dim xlapp As Excel.Application
Dim wkbTemplate As Excel.Workbook
Dim wksRpt As Excel.Worksheet
Dim wksFTTH As Excel.Worksheet
Dim wksVDSL As Excel.Worksheet
'Set Object references in Excel
Set wkbTemplate = GetObject(strfilepath & "VDSLFTTH.xlt")
Set xlapp = wkbTemplate.Parent
Set wksRpt = wkbTemplate.Worksheets("DataSheet")
Set wksFTTH = wkbTemplate.Worksheets("FTTH")
Set wksVDSL = wkbTemplate.Worksheets("VDSL")
xlapp.Visible = True
wkbTemplate.Windows(1).Visible = True
wksRpt.Range("A2").Select
Selection.QueryTable.Refresh BackgroundQuery:=False