Solved

vb6 and WebBrowser Control

Posted on 2014-03-25
2
602 Views
Last Modified: 2014-03-26
Hi Guys

Using vb6 and the Web Browser Control.

I load a word document into the web browser control.

I try to get the filname of the currently opened file using the following code:

Function GetDocumentName()
    Dim szFullFileName$
    szFullFileName = wb.LocationURL
    MsgBox szFullFileName & vbCrLf & wb.LocationURL
End Function

Open in new window


szFullFilename will refer to the url of the file in the following format:

file:///E:/Code/TimeKeeper/tmp_%20160616.doc

This is URL Encoded.

I need to decode the LocationURL to return the actual file name, including a filename which might be UNC.

Any and all help appreciated.

MTIA


DWE

(PS I note that in the wb_DocumentComplete event, the URL value is the actual filename, not the URLEncoded version)
0
Comment
Question by:dwe0608
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
you will need the following things:
1) a urldecode function like this one:
http://www.vb-helper.com/howto_url_decode_string.html
' Convert a URL safe encoding into its original text.
Private Function URLDecode(ByVal txt As String) As String
Dim txt_len As Integer
Dim i As Integer
Dim ch As String
Dim digits As String
Dim result As String

    SetSafeChars

    result = ""
    txt_len = Len(txt)
    i = 1
    Do While i <= txt_len
        ' Examine the next character.
        ch = Mid$(txt, i, 1)
        If ch = "+" Then
            ' Convert to space character.
            result = result & " "
        ElseIf ch <> "%" Then
            ' Normal character.
            result = result & ch
        ElseIf i > txt_len - 2 Then
            ' No room for two following digits.
            result = result & ch
        Else
            ' Get the next two hex digits.
            digits = Mid$(txt, i + 1, 2)
            result = result & Chr$(CInt("&H" & digits))
            i = i + 2
        End If
        i = i + 1
    Loop

    URLDecode = result
End Function

Open in new window

2) remove the file:// prefix
3) eventually replace the / by \  in the resulting string.
0
 
LVL 1

Author Comment

by:dwe0608
Comment Utility
Thanks Guy

Attached is the code - can be made better of course but it does the job - no doubt someone here will find it useful or make it better .... I have left the original credits in the zip file which contains a small project which demonstrates the use of the function ....

Regards

DWE

' Convert a URL safe encoding into its original text.
Private Function URLDecode(ByVal txt As String) As String
Dim txt_len As Integer
Dim i As Integer
Dim ch As String
Dim digits As String
Dim result As String

    result = ""
    txt_len = Len(txt)
    i = 1
    Do While i <= txt_len
        ' Examine the next character.
        ch = Mid$(txt, i, 1)
        If ch = "+" Then
            ' Convert to space character.
            result = result & " "
        ElseIf ch <> "%" Then
            ' Normal character.
            result = result & ch
        ElseIf i > txt_len - 2 Then
            ' No room for two following digits.
            result = result & ch
        Else
            ' Get the next two hex digits.
            digits = Mid$(txt, i + 1, 2)
            result = result & Chr$(CInt("&H" & digits))
            i = i + 2
        End If
        i = i + 1
    Loop

    URLDecode = ReFormatFilePart(result)
End Function

Function ReFormatFilePart(s As String) As String

    s = Replace(s, "file:///", "")
    s = Replace(s, "/", "\")
    
    ReFormatFilePart = s

End Function

Function GetDocumentName()
    Dim szFullFileName$
    szFullFileName = URLDecode(wb.LocationURL)
    Debug.Print szFullFileName & vbCrLf & wb.LocationURL
    
End Function

Open in new window

URLDecode.zip
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

771 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

10 Experts available now in Live!

Get 1:1 Help Now