VB6 Grab text string from web site

Posted on 2009-12-26
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
    LVL 76

    Expert Comment

    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

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

    Accepted Solution

    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("")
    End Sub

    Open in new window


    Author Closing Comment

    Perfect. Thanks!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now