Moving on, I've resigned myself to closing and re-opening Excel--which is surprisingly fast. Now another obstacle: refreshing the imported data.
From Excel (2003) I would click Data | Refresh Data, but I'd like to automate it through VBScript so I can do so from MS. I recorded a macro of the Refresh Data keystrokes. It amounts to a single line.
Code: Select all
Selection.QueryTable.Refresh BackgroundQuery:=False
So I tried adding and calling this sub.
Code: Select all
Sub RefreshSheet
Set xlSheet = xlBook.Worksheets("MyOrders").QueryTable.Refresh
End Sub
...which failed with
"Microsoft VBScript runtime error :438
Object doesn't support this property or method: 'xlBook.Worksheets(...).QueryTable'
I've also tried the lines:
Set xlSheet = xlBook.Worksheets("MyOrders").Range.QueryTable.Refresh
Set xlSheet = xlBook.Worksheets("MyOrders").Range("b2").QueryTable.Refresh
Set xlSheet = xlBook.Worksheets("MyOrders").Range(2, 2).QueryTable.Refresh
Set xlSheet = xlBook.Worksheets("MyOrders").Selection.QueryTable.Refresh
...in that same sub, but they met similar fates. As well, I tried a function (instead of a sub) with no success.
The Excel VBA help file gives an example:
Code: Select all
Worksheets(1).Range("a10").QueryTable.Refresh
...And I'm going through the
tutorial on converting VBA to VBS but I don't understand most of it because I'm extremely new to VBS and somewhat new to VBA. For instance, the "Immediate" window is open, but I can't get it to show me the value of "xlSortOnValues". I gather I should be using a sub (and VBRun) instead of a function (and VBEval) to refresh. Do I need to declare "BackgroundQuery:=False" as "BackgroundQuery = 0" before refreshing?
If anyone can steer me in the right direction for refreshing data, I'd really appreciate it. Otherwise, I'll create Excel VBA macros on each PC and call the VBA macro through VBS from MS.