finding text inside html page (1 Viewer)

megatronixs

Registered User.
Local time
Today, 07:08
Joined
Aug 17, 2012
Messages
719
Hi all,

I need to fix a part of a function that takes a piece of text from html page.
The current function will only take the first string of numbers that come after a 0 href=javascript:LinkCase(0,123456,'Daltonia Limited',0,654321)
so I will get only 123456.
I'm trying to get now the company name too , but I get empty string.

This is the part of the code that gets me 123456:
Code:
CaseNumber = GetPartSTR(ie.document.body.innerHTML, "javascript:LinkCase(0,", ",'") 'find the case number in the body of the web page
from this html body: href=javascript:LinkCase(0,123456,'Daltonia Limited',0,654321)

How to get "Daltonia Limited" ?

This is the function used:
Code:
Function GetPartSTR(ByRef FullSTR As String, ByVal FromSTR As String, ByVal ToSTR As String, Optional ByVal StartText As String = vbNullString, Optional StartNum As Long = 1) As String
    Dim vSTART      As Long
    Dim vEND        As Long
    Dim StartChar   As Long
    Dim Effect      As String
    Dim regex       As New RegExp
    Dim previous    As Long
    
    On Error GoTo What
    If IsNumeric(StartText) Then
      StartNum = StartText
      StartText = vbNullString
    End If
    If Len(StartText) < 2 Then
        StartChar = StartNum
    Else
        StartChar = InStr(StartNum, FullSTR, StartText, vbTextCompare)
    End If
    vSTART = InStr(StartChar, FullSTR, FromSTR, vbTextCompare)
    vEND = InStr(vSTART + Len(FromSTR), FullSTR, ToSTR, vbTextCompare)
    If vSTART > 0 And vEND > 0 Then
        If ToSTR = vbNullString Then
            Effect = Mid(FullSTR, CLng(vSTART + Len(FromSTR)))
        Else
            Effect = Mid(FullSTR, CLng(vSTART + Len(FromSTR)), vEND - vSTART - Len(FromSTR))
        End If
    Else
        Effect = vbNullString
    End If
    Do
      previous = Len(Effect)
      While Right(Effect, 2) = vbNewLine
        Effect = Left(Effect, Len(Effect) - 2)
      Wend
      While Left(Effect, 2) = vbNewLine
        Effect = Right(Effect, Len(Effect) - 2)
      Wend
      While Right(Effect, 1) = " "
        Effect = Left(Effect, Len(Effect) - 1)
      Wend
      While Left(Effect, 1) = " "
        Effect = Right(Effect, Len(Effect) - 1)
      Wend
    Loop While previous > Len(Effect)
    
    GetPartSTR = Effect
    Exit Function
What:
    GetPartSTR = vbNullString
End Function
Greetings.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:08
Joined
May 7, 2009
Messages
19,245
try this, make sure the html is consistent
to have those info in your Post#1.
supposed you did get 'Daltona Limited' (3rd Position on)
comma separated value, you can use GetNextPartSTR (still
getting on the 3rd position) to get the next value, if there
is another value.
Code:
Option Compare Database
Option Explicit

Dim mLastPosToStart As Long
Dim mSource As String
Dim mPosition As Long
Dim mPrefix As String
Dim mSuffix As String

Public Function GetPartSTR(ByVal Source As String, _
                           ByVal Position As Integer, _
                           Optional ByVal PrefixString As String = "LinkCase(", _
                           Optional ByVal SuffixString As String = ")") As Variant
' Parameters:
'
'   Source          = The source string, your HTML
'   Position        = the position on the comma separated value you want to retrieve
'   PrefixString    = a substring where the search will begin
'   SuffixString    = a substring where the search will end
'
' example:
'
'   GetPartSTR("href=javascript:LinkCase(0,123456,'Daltona Limited',0,654321)", 3)
'       this will return 'Daltona Limited'
'
    Dim var As Variant
    Dim StartPos As Long
    Dim EndPos As Long
    Dim StringToProcess As String
    
    mPrefix = PrefixString
    mSuffix = SuffixString
    StartPos = InStr(Source, PrefixString)
    If StartPos > 0 Then
        StartPos = StartPos + Len(PrefixString)
        mSource = Source
        mLastPosToStart = 0
        mPosition = Position
        'find the tail
        EndPos = InStr(StartPos, Source, SuffixString)
        If EndPos > 0 Then
            StringToProcess = Mid(Source, StartPos, EndPos - StartPos)
            var = Split(StringToProcess, ",")
            GetPartSTR = var(Position - 1)
        End If
        mLastPosToStart = EndPos
    End If
End Function

Public Function GetNextPartStr()
    Dim StartPos As Long
    Dim EndPos As Long
    Dim StringToProcess As String
    Dim var As Variant
    If mLastPosToStart > 0 Then
        StartPos = InStr(mLastPosToStart, mSource, mPrefix)
        If StartPos > 0 Then
            StartPos = StartPos + Len(mPrefix)
            'find the tail
            EndPos = InStr(StartPos, Source, mSuffix)
            If EndPos > 0 Then
                StringToProcess = Mid(mSource, StartPos, EndPos - StartPos)
                var = Split(StringToProcess, ",")
                GetNextPartStr = var(mPosition - 1)
            End If
            mLastPosToStart = EndPos
        End If
    End If
End Function
 

megatronixs

Registered User.
Local time
Today, 07:08
Joined
Aug 17, 2012
Messages
719
Hi,

I don't get it to work. no clue why.
I should copy this to a new module and then run it by using the call of the function like this: CompanyName = GetPartSTR.

I'm simply lost wit this, kind of over my head.

Greetings.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:08
Joined
May 7, 2009
Messages
19,245
you use the function just as you did with your
previous function but with different parameter:

Dim strHTML As String
strHTML = ie.document.body.innerHTML
CaseNumber = GetPartSTR(strHTML, 2)
Customer = GetPartSTR(strHTML, 3)

or

CaseNumber = GetPartSTR(ie.document.body.innerHTML, 2)
Customer = GetPartSTR(ie.document.body.innerHTML, 3)

remember CaseNumber is the First (1) position on
the comma separated list:

(0,123456,'Daltonia Limited', ....)
(1),(2), (3)

in the function i gave there is an example.
 

Users who are viewing this thread

Top Bottom