Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to extract title of HTML document?

Status
Not open for further replies.

teh215

MIS
Jul 2, 2002
56
US
Is there an &quot;easy&quot; way to extract the Title of an HTML document rather than opening the file and finding the <TITLE> </TITLE> tags? Reading the file would be easy but, I am hoping there is an API ...
 
These are files that are stored locally. I was hoping there was a &quot;GetDocumentTitle&quot; API rather than writing the function. It would be easy to do but I would prefer an API...
 

As far as I know there is not an API to do what you want, but as for creating a function. Your right, it would not be hard. I have created one in the past that among other things returned the title.

Good Luck
 
This is what I came up with (watch wrap)

Public Function GetHTMLDocumentName(ByVal FilePath As String) As String

On Local Error GoTo ErrTrap

Dim p_intFileHandle As Integer
Dim p_strDocumentSource As String

' Load the file into a string
p_intFileHandle = FreeFile
Open FilePath For Input Access Read Lock Read As p_intFileHandle
p_strDocumentSource = Input(LOF(p_intFileHandle), p_intFileHandle)
Close p_intFileHandle
' Extract the Title
GetHTMLDocumentName = Mid(p_strDocumentSource, InStr(1, p_strDocumentSource, &quot;<TITLE>&quot;, vbTextCompare) + 7, InStr(1, p_strDocumentSource, &quot;</TITLE>&quot;, vbTextCompare) - InStr(1, p_strDocumentSource, &quot;<TITLE>&quot;, vbTextCompare) - 7)
On Error GoTo 0

ExitNormal:
Exit Function
ErrTrap:
MsgBox &quot;Error: &quot; & Err.Description & &quot; (Occurred in modEffects.GetHTMLDocumentName)&quot;, vbCritical Or vbOKOnly Or vbMsgBoxHelpButton, &quot;Error&quot;, r.HelpFile, 1116
GetHTMLDocumentName = &quot;&quot;
Close p_intFileHandle

End Function
 

I have tested it on a couple of files and it works as long as there is the &quot;<TITLE></TITLE>&quot; tags within. I would add a check to see if the &quot;<TITLE>&quot; tags exists and if not then automatically return an empty string and I don't know if its a type-o or a bad cut and paste but in your error handler on your msgbox line you have &quot;r.HelpFile&quot;, shouldn't it be Err.HelpFile?

Good work and good luck

 
Thanks for the tip, I didn't test it without the Tags but, I did with a blank TITLE which, for some reason, returns the file path. I will have to look into that further tonight. The &quot;r.HelpFile&quot; isn't a typo. &quot;r&quot; is a instance of my Registry object, &quot;HelpFile&quot; is a property which returns or sets the path to the compiled Helpfile.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top