[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


VB6 Grab text string from web site

Posted on 2009-12-26
Medium Priority
Last Modified: 2012-05-08
Dear Experts,

I want to grab a text string from inside a text file (text .txt) from my web site. The file contains only the string, nothing else. How can I do this?

If in the future I need to store and retrieve more than one string, is it possible to do this in a fashion similar to ini files?

Question by:ttobin333
  • 2
LVL 76

Expert Comment

ID: 26126031
You could use the Inet control to download the file and then use the Windows API functions such as GetPrivateProfileString if the file conforms to the .ini file format.

Author Comment

ID: 26126499
I would prefer not to download the file. Is there another way?
LVL 29

Accepted Solution

nffvrxqgrcfqvvc earned 2000 total points
ID: 26126911
It's possible. The example will read 1024 KB from the web file and truncate the unused bytes. It's hard to give good example without knowing the file structure but this should get you started.
Option Explicit
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const WININET_API_FLAG_SYNC = &H4&
Private Const INVALID_HANDLE_VALUE = (-1)

Private Declare Function InternetOpenW Lib "wininet" (ByVal lpszAgent As Long, ByVal dwAccessType As Long, ByVal lpszProxyName As Long, ByVal lpszProxyBypass As Long, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrlW Lib "wininet" (ByVal hInternet As Long, ByVal lpszUrl As Long, ByVal lpszHeaders As Long, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal lpBuffer As Long, ByVal dwNumberOfBytesToRead As Long, lpdwNumberOfBytesRead As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInternet As Long) As Long

Public Function ReadWebBytes( _
  ByVal szUrl As String, _
  Optional ByVal BufferSize As Long = 1024) As String

  Dim Buffer()    As Byte
  Dim hOpen       As Long
  Dim hConn       As Long
  Dim dwReadBytes As Long

  ' Open connection force sync download.
  hOpen = InternetOpenW(0, 1, 0, 0, WININET_API_FLAG_SYNC)
  ' Establish connection to URL.
  hConn = InternetOpenUrlW(hOpen, StrPtr(szUrl), 0, 0, _
  If hConn = 0 Then
    Debug.Print Err.LastDllError
    InternetCloseHandle hOpen
    Exit Function
  End If
  ' allocate buffer
  ReDim Buffer(BufferSize) As Byte
  If InternetReadFile(hConn, VarPtr(Buffer(0)), 1024, dwReadBytes) Then
    ReDim Preserve Buffer(dwReadBytes - 1) As Byte
    ReadWebBytes = StrConv(Buffer, vbUnicode)
  End If

  ' cleanup
  InternetCloseHandle hConn
  InternetCloseHandle hOpen
  ' free memory
  Erase Buffer
End Function

Private Sub Command1_Click()

  Debug.Print ReadWebBytes("http://somesite.com/test.txt")

End Sub

Open in new window


Author Closing Comment

ID: 31670129
Perfect. Thanks!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question