a picture web Address goes to different place

drom

Well-known Member
Joined
Mar 20, 2005
Messages
528
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and Thanks in advance!


I few days ago I posted the following question:
Already solved​
now I am having a different problem:

I am trying to isert a picture in a cell (like in my previous question)
The picture exists:
But I do not know why when I copy and paste the previous address in google the previous address becomes:
So When I am trying to get the picture from the website using the macro (I get the err 1004, because the picture does not exit with the 97 address


VBA Code:
Sub mToInsertURLpicture()
  On Error Resume Next:                         Application.ScreenUpdating = False
Dim WKBvba As Workbook:                         Set WKBvba = ThisWorkbook
Dim WKBactive As Workbook:                      Set WKBactive = ActiveWorkbook
  Dim wActSht As String:                        wActSht = ActiveSheet.Name
    Dim rCell As Range:                         Set rCell = ActiveCell
Dim wURL As String:                             wURL = rCell
  Dim Pic As Picture
Dim Shp As Shape

    wURL = "https://casdocs.com/CASAS/01758-97.jpg"

    Err.Clear

    'rCell.Select

    If wURL <> "" Then
      If Left(LCase(wURL), 4) = "http" Then '://" Or Left(LCase(wURL), 8) = "https://" Then
        Err.Clear
        Set Pic = Nothing: Set Pic = Sheets(wActSht).Pictures.Insert(wURL)
        If Pic Is Nothing Then
          Set Pic = Nothing: Set Pic = Sheets(wActSht).Pictures.Insert(UCase(wURL))
        End If
        If Pic Is Nothing Then
          Set Pic = Nothing: Set Pic = Sheets(wActSht).Pictures.Insert(Left(LCase(wURL), Len(wURL) - 4) & UCase(Right(wURL, 4)))
        End If
        If Pic Is Nothing Then
          MsgBox "Wrong" & vbNewLine, vbCritical, "Thanks in advance!"
        Else
          MsgBox "OK" & vbNewLine, vbInformation, "Thanks in advance!"
        End If
      End If
    End If

End Sub

How can I solve this??
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Your code will need to check whether the location has moved. If so, it will need to retrieve the new location. Therefore, first add the following function to your code module...

VBA Code:
Private Function url_redirected(ByRef url As String, ByRef location As String, ByRef status_code As Long) As Boolean

    Dim req As Object
    Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
   
    With req
        .option(6) = False 'No redirects
        .Open "GET", url, False
        .send
        If .status = 301 Or .status = 302 Then 'moved or re-directed
            location = .getResponseHeader("Location")
            status_code = .status
            url_redirected = True
            Exit Function
        End If
    End With
   
    url_redirected = False

End Function

Then replace the following line...

VBA Code:
wURL = "https://casdocs.com/CASAS/01758-97.jpg"

with

VBA Code:
    wURL = "https://casdocs.com/CASAS/01758-97.jpg"
   
    Dim location As String
    Dim status_code As Long
    If url_redirected(wURL, location, status_code) = True Then
        wURL = location
    End If

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top