Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb6 and WebBrowser Control

Posted on 2014-03-25
2
Medium Priority
?
688 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses

886 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