Handling Live Web Content in WSH - Part 1 - The Final Code
(Page 4 of 4 )
Do you want to use this in other code? This code snippet makes for a great function as well. In the last line I used a string variable getIP for a reason. The variable you use here must match the name that you give to your function. This tells your function to return the value of strIP when it completes. Next, you enclose the entire snippet with the Function getIP() and End Function statements. You can now insert this function into any code you like. Let’s take a look at our complete code as a function.
Function getIP()
Set objxmlHTTP = _
CreateObject("Microsoft.XMLHTTP")
Call objxmlHTTP.Open("GET", "http://checkip.dyndns.org", False)
objxmlHTTP.Send()
strHTMLText = objxmlHTTP.ResponseText
Set objxmlHTTP = Nothing
If strHTMLText <> "" Then
varStart = instr(1, strHTMLText, "Current IP Address:", _
vbTextCompare) + 19
If varStart Then varStop = instr(varStart, strHTMLText, _
"</body>", vbTextCompare)
If varStart And varStop Then strIP = mid(strHTMLText, varStart, _
varStop - varStart)
Else
strIP = "Unavailable"
End If
getIP = trim(strIP)
End Function
Now a call to your function would return your IP address. Anytime you need it, simply use the following line to assign it to the string variable result.
result = getIP()
It’s that simple. You now have one very powerful piece of reusable code. With a little tweaking it could pull any information you like from whatever website you choose.
There are many other ways you can implement this same type of script. Perhaps you want a script that will grab the local headlines from your favorite news site. Or maybe you just want to create a script that tests to see if your website is up and running. There are many reasons you might want to connect to the internet and handle live web content. Whatever your reason is, you now have the tools to do it. Until next time, keep coding!

| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |