Performing Web Queries in Excel 2003
Pages: 1, 2
How does it work?
If you record the preceding web query, you'll get code that looks something like this:
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/ecn?s=SNDK", _
Destination:=Range("C2"))
.Name = "Real-Time Quote"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "22"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://finance.yahoo.com/q/
hp?a=01&b=5&c=2003&d=01&e=5&f=2004&g=d&s=sndk" _
, Destination:=Range("A9"))
.Name = "Price History"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "30"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Some key properties and methods above shown in bold bear mention here:
- The
Addmethod creates the query and adds it to the worksheet. - The
RefreshStyleproperty tells Excel to overwrite existing data rather than to insert new cells each time the query is refreshed. - The
WebTablesproperty identifies which item from the page to import. Excel assigns a index to each item on the page and you can import one or more items or the entire page ifWebSelectionTypeis set toxlEntirePage. - The
Refreshmethod imports the data onto the worksheet. Without this method, the query results are not displayed.
The query itself is consists of the Connection, WebTables, and formatting properties. If you save the web query to a query file (.iqy), the data looks like this:
WEB
1
http://finance.yahoo.com/q/
hp?a=01&b=5&c=2003&d=01&e=5&f=2004&g=d&s=sndk
Selection=30
Formatting=None
PreFormattedTextToColumns=True
ConsecutiveDelimitersAsOne=True
SingleBlockTextImport=False
DisableDateRecognition=False
DisableRedirections=False
When Excel updates a web query, a small, green globe is displayed in the status bar at the bottom of the screen, as shown in Figure 6. This symbol indicates that the query is being refreshed from the Internet.
Jeff Webb is a SharePoint consultant and trainer who has written about computers and technology for 20 years. Among his published O'Reilly titles are Essential SharePoint, SharePoint Office Pocket Guide, Programming Excel with VBA and .NET, and Excel 2003 Programming: A Developer's Notebook. Jeff was an original member of Microsoft's Visual Basic team.
Return to WindowsDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 8 of 8.
-
Web queries - non-interactive
2005-02-06 06:29:35 dir [Reply | View]
the interesting thing about this is that there are seemingly no integrated ways to share a workbook over the web. Not just publish the results of a query, but actually share it. While Microsoft doesn't have a solution for this yet, there are a couple of third-party tools like BadBlue's Excel Web product and Soft Writer that seem to do the trick. It's just bizarre that no MSFT solution for this exists yet (just read-only tools - without collaboration capabilities). -
Web queries - non-interactive
2005-02-07 04:48:55 Jeff Webb |
[Reply | View]
Are you sure about that? I seem to be able to share workbooks containing web queries through SharePoint workspaces and document libraries.
I haven't noticed any problems with that approach.
-- Jeff
-
Mac support
2004-08-11 11:59:44 Marc Hedlund |
[Reply | View]
Unless I'm missing it, Web queries aren't supported in Mac Excel 2004 (in case anyone is wondering...) -
Mac support
2004-08-12 05:03:12 Jeff Webb |
[Reply | View]
I focused specifically on Excel 2003 for this book. I did find this comment in one Microsoft newsgroup:
"There's almost no difference in XLv.X and XL 97/00/02 as far as XL
itself is concerned - all functions are cross-platform."
So I'd assume that Web Queries are probably supported, but I don't have a Mac to test them and I couldn't find a comprehensive feature Win/Mac comparison online anywhere.
At one point, Microsoft used the same source code to generate Win/Mac versions of Excel. I don't know if that's still the case (I kinda doubt it, there were performance complaints).
-- Jeff -
Mac support
2005-09-10 06:13:23 blunt [Reply | View]
I am having trouble running a query table macro in mac that I created for a file in excel in windows. In one machine it gives me a problem with the Refresh method. I think the error number is '1004'. In that machine I have run it under mac os 9 and excel 2001, and mac OS X and excel 2003. In both cases I get the same error.
Then I tried in a different machine under MAC OS 9 and Excel 2001 and it ran well.
What I'm thinking is that something in the machine configuration is stopping excel from accessing the web query (in my case a web service) but I cannot think exactly what.
Can you help???? Thanks.





