Ahmet Inal
Ahmet Inal

Reputation: 11

To transfer pictures to user form from web page VBA(Excel)

I need help with this topic.

Image Html Code;

<IMG src="/SigortaliTescil/PG" width=50 height=25>

My VBA code (Excel)

Private Sub CommandButton3_Click()
If TextBox1.Text = "" Then
MsgBox "Lütfen Listeden Seçim Yapınız"
Exit Sub
End If

Dim ie As Object
Dim kontrol As String

Set ie = CreateObject("internetexplorer.application")

With ie

.Navigate "https://uyg.sgk.gov.tr/SigortaliTescil/amp/loginldap"
.Visible = True

Set tags = ie.Document.getElementsByTagName("img")
For Each tagx In tags
    If tagx.src = "/SigortaliTescil/PG" Then
    WebBrowser1.img = tagx.Doc

    End If
Next

How can I show the image in a picture box or in Webbrowser.

Thank you in advance for your help.

Upvotes: 1

Views: 697

Answers (1)

MacroMarc
MacroMarc

Reputation: 3324

Declare the URLDownloadToFile function at the top of your Module:

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Use an Image Control on your form and call it picBox. Then in your sub change to:

Private Sub CommandButton3_Click()

Const picURL = "https://uyg.sgk.gov.tr/SigortaliTescil/PG"
Const dLoadPath = "C:\Desktop\downloadedPic.jpg"  'or whatever is appropriate

....

With ie
   .Navigate "https://uyg.sgk.gov.tr/SigortaliTescil/amp/loginldap"
   .Visible = True
   While ie.readyState <> READYSTATE_COMPLETE
        DoEvents
   Wend

   Set tags = ie.Document.getElementsByTagName("img")
   If tagx.src = picURL Then
       'downloads to the dLoadPath filepath on the PC/Network - may wish to delete after??
       Dim retVal as Variant
       retVal = URLDownloadToFile(0, picURL, dLoadPath, 0, 0)  
       Me.picBox.Picture = LoadPicture(dLoadPath)
       Exit Sub
   End if

Upvotes: 1

Related Questions