Library tutorials & articles
A Real-Time VB6 ActiveX News Control
- Introduction
- Creating the ActiveX control
- Obtaining our news with XMLHTTP
- Displaying the link
- Compiling and using our new ActiveX control
- Conclusion
Obtaining our news with XMLHTTP
If you haven't used the XMLHTTP library before, you should have a quick read of this article first: http://www.devarticles.com/content.php?articleId=25. We will create a function named LoadNews. LoadNews will handle all of the background work (retrieving and displaying the news items), while still updating the progress bar on our actual ActiveX control.
Our LoadNews function will accept a reference to a string variable (byref), and return a Boolean value, indicating the success/failure of the function as a whole. If an error occurs, its description will be stored in the string variable passed as a reference. The declaration of the LoadNews function looks like this:
Private Function LoadNews(ByRef msg As String) As Boolean
The LoadNews function is declared as private, so that when it comes time to compile and distribute our ActiveX control, only the control itself can call the LoadNews function. Several objects from the MSXML 4 library are also created, as well as two string variables and one integer variable. These will be used to hold the details of each news item:
Dim objXMLHTTP As New MSXML2.XMLHTTP40 Dim objXML As New MSXML2.DOMDocument40 Dim objXML_Root Dim strURL As String Dim strHeadLine As String Dim counter As Integer
Next, we set the URL of the XML news page we will be retrieving. If you visit http://w.moreover.com/categories/category_list_xml.html, you will see a list of news topics. At the time of writing, there were 334 available. I have created a constant string variable in the general declarations section to hold the name of our news category, like this:
Const NewsCategory As String = "Videogame news"
This is used to form the URL of the XML page to retrieve, as shown below:
strURL = "http://p.moreover.com/cgi-local/page?c=" & NewsCategory & "&o=xml"
Next, we use the Open and Send methods of our XMLHTTP object to actually retrieve the page. We are using the "GET" HTTP protocol, and have set asynchronous mode to false, meaning that our XML document MUST be downloaded before we can continue:
objXMLHTTP.open "GET", strURL, False objXMLHTTP.send
We also update the loading message and progress bar:
lblLoading.Caption = "Loading XML news feed..." pbProgress.Value = pbProgress.Value + 1
Once the request method of the XMLHTTP object has returned, we try and load its text (available as the responseText variable) into our XML document object. If an error occurs, the function is aborted and the value of the string-referenced variable is set to the errors description:
objXML.loadXML (objXMLHTTP.responseText)
'Make sure that valid XML was returned If objXML.parseError.errorCode <> 0 Then msg = objXML.parseError.reason LoadNews = False Exit Function End If
At this point, if the function hasn't encountered any errors, then the XML
has been loaded successfully, and we are ready to extract each news item. We
obtain the root element of the XML document into the objXML_Root object, and
use its getElementsByTagName function to make sure it only contains
Set objXML_Root = objXML.documentElement
objXML_Root.getElementsByTagName ("article")
Our ActiveX control will display the top five items in the XML document (The MoreOver.com XML document returns twenty by default). We use a simple "for" loop to extract them from our XML document. Once a loop is complete, the status message and progress bar are also updated:
For counter = 1 To 5 lblLoading.Caption = "Loading news item " & counter & "..." pbProgress.Value = pbProgress.Value + 1
The URL of each item is extracted into the strURL variable. The headline and source of each news item are also extracted into the strHeadLine variable:
strURL = objXML_Root.childNodes(counter).childNodes(0).firstChild.Text
strHeadLine = objXML_Root.childNodes(counter).childNodes(1).firstChild.Text &
" [Source: " & objXML_Root.childNodes(counter).childNodes(2).firstChild.Text
& "]"
Under the general declarations section of our ActiveX control, I have created an array that will hold each of the links to the news items:
Dim arrLinks(5) As String
When a user clicks on a label containing the details of a news item, the items link (contained in the arrLinks string array) is executed using the ShellExecute API function (more on this later). We simply set the value of each items URL to its index in the arrLinks array, like this (we use "counter-1" because arrays are indexed from 0 onwards):
arrLinks(counter - 1) = strURL
We also set the caption of the link to the headline:
Select Case counter Case 1 lblLink1.Caption = strHeadLine Case 2 lblLink2.Caption = strHeadLine Case 3 lblLink3.Caption = strHeadLine Case 4 lblLink4.Caption = strHeadLine Case 5 lblLink5.Caption = strHeadLine End Select
Lastly, we make sure that all of the labels are visible, destroy all of the XML objects, and return true, indicating that the function succeeded:
'Hide the status label and progress bar lblLoading.Visible = False pbProgress.Visible = False 'Make sure the labels are visible lblLink1.Visible = True lblLink2.Visible = True lblLink3.Visible = True lblLink4.Visible = True lblLink5.Visible = True 'Kill the XML objects Set objXMLHTTP = Nothing Set objXML_Root = Nothing Set objXML = Nothing LoadNews = True Exit Function
The LoadNews function takes care of retrieving, extracting and displaying our news links. Let’s take a look at how the ActiveX control will actually display the link.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Good example.
This is the first time played with any xmlhttp feeds, and found the example whet my appetite for more! The code was not quite complete, but if you are familiar with vb then you should be able to fill in the blanks. Once you get it working, you will no doubt see the other possible uses of this technology.
Keep it real.
I've went thru the listing a few times, and it is not working for me at all....
Things I noticed, the For loop he uses has no Next statement
I'm probably overlooking something ....any suggestions?
And after the LoadNews returns a TRUE it has EXIT function, shouldn't that be end?
Thanks!
Edit = typos
hmmm... yes... it seems the author hasn't released the entire VB project for the control. The hand icon will in fact be in your VB installation directory (in something like Shared/Icons/ along with the icons/toolbar bitmaps etc).
I see the images for the article, but not any hand images.
don't you see any images on http://www.developerfusion.com/show/2273/2/ ? Try hitting refresh on your browser....
I went to the article again and found no images?
There was one crucial thing missing from the article.... the images
I have now added them, which might make it a bit easier for future readers 
This was a somwehat confusing but very cool article once you get it working.
What was frustrating was that the article indicated there was support material, but I cannot find any anywhere.
This just made me really read the article and work with the project until I had it working.
Great topic and a useful tool to add to any non-profit individual.
BTW, I wonder what it costs to use for $$ purposes?
This thread is for discussions of A Real-Time VB6 ActiveX News Control.