Solved

vb6 and WebBrowser Control

Posted on 2014-03-25
2
611 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
ID: 39955308
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
ID: 39955489
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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 a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

831 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