How can you find the source of an image? (i.e. <IMG SRC="x">, find the x)? Some code would be nice, I'm still new with VB...
Printable View
How can you find the source of an image? (i.e. <IMG SRC="x">, find the x)? Some code would be nice, I'm still new with VB...
You need to give more details in your question. What are you trying to do?
I think it's obvious what he want's to do. Either a kind of browser to open the image, or a HTML-editor. :)
The following code example shows how to parse the filenames from all <img> tags in a web page. You just specify the filename and the code parses the data finding all images tags, then going on to extract the filename from the tags. All results are printed onto the form.
Start a new project then add a command button named Command1. Add the following code to the form.
This code example could easily be modified to meet your own needs. You could prompt the user for a filename from a common dialogue, then parse that... Just a suggestion. Anyway, I hope this helps. Post back and let me know...Code:Option Explicit
'Written by REM on Sunday afternoon at 1.00pm GMT...
'I'm going back to bed now...
Private Sub ExtractImages(strFilename As String)
Dim strTemp As String 'Holds the data we are going to parse
Dim strTag As String 'Holds the tag we will extract
Dim intnoOfImages As Integer 'Counts the number of images in the file.
Dim intCntr1 As Integer 'Just a counter
Dim intcntr2 As Integer 'Just a counter
'Open the file for binary then get the html source.
Open strFilename For Binary As #1
strTemp = Space(LOF(1))
Get #1, , strTemp
Close #1
'First, find the starting position of the tag.
'Go through each character in the string.
For intCntr1 = 1 To Len(strTemp)
'If we find the start of the tag...
If LCase(Mid(strTemp, intCntr1, 4)) = "<img" Then
'Find the end of the tag...
For intcntr2 = intCntr1 To Len(strTemp)
'Look for the >
If LCase(Mid(strTemp, intcntr2, 1)) = ">" Then
strTag = Mid(strTemp, intCntr1, intcntr2 - intCntr1 + 1)
'tell the user you have found the following tag...
Form1.Print "Found the following tag: "
Form1.Print strTag
'Increment the amount of images found.
intnoOfImages = intnoOfImages + 1
'Now we have one of the tags, we can parse it and get the filename from it
ParseTag (strTag)
'We don't need to go any further.
Exit For
End If
Next intcntr2
End If
Next intCntr1
'Print a blank line.
Form1.Print
'Tell the user that the search was complete.
Form1.Print "Search complete."
'Tell them how many images were found.
Form1.Print "" & intnoOfImages & " images found."
End Sub
Private Sub ParseTag(strData)
Dim intCntr1 As Integer
Dim intcntr2 As Integer
Dim strFilename As String
Form1.Print "Parsing tag for file name..."
For intCntr1 = 1 To Len(strData)
If LCase(Mid(strData, intCntr1, 4)) = "src=" Then
'We now have the bit where the filename is stored.
For intcntr2 = intCntr1 To Len(strData)
'Here we check the tag for the " character. We also have to make sure
'that it isn't at the start of the filename, as we won't get a filename.
If (Mid(strData, intcntr2, 1) = Chr(34)) And (Mid(strData, intcntr2 - 4, 4) <> "src=") Then
'We have found the end of the tag
strFilename = Mid(strData, intCntr1, intcntr2 - intCntr1)
'Strip the src=" from the start
strFilename = Right(strFilename, Len(strFilename) - 5)
'Tell the user that a filename has been extracted.
Form1.Print "Filename found: "
'Print the filename.
Form1.Print strFilename
'Print a blank line.
Form1.Print
'Exit for, don't need to go any further.
Exit For
End If
Next intcntr2
End If
Next intCntr1
End Sub
'To use, call the following.
Private Sub Command1_Click()
'Get all image links from a file.
ExtractImages ("C:\Mywebpage.html")
End Sub
Later(z)
REM
Just wondering if this is what you wanted? Just wondering?
later(z)
REM
the above code worked fine but if I load bigger file to parse it's giving overflow error. I think that using strings it's not possible to parse tags from html pages of bigger ones. do you know any solution for this. If any please let me know.