Code modification requested

tenda

New Member
Joined
Dec 17, 2014
Messages
37
The following code is cited from: How to open a list of URLs and save a screenshot of each on my secondary monitor using Excel VBA

It captures a screenshot for a list of urls which exists in Sheet1 ColA.

VBA Code:
Option Explicit
Public Sub Screenshots()
    Dim d As WebDriver, urls(), i As Long
    urls = Application.Transpose(ThisWorkbook.Worksheets("Sheet1").Range("A1:A200").Value) '<change this
    Set d = New ChromeDriver

    With d
        .AddArgument "--headless"
        .Start "Chrome"
        .Window.Maximize

        For i = LBound(urls) To UBound(urls)
            If InStr(urls(i), "http") > 0 Then
                .get urls(i)
                .TakeScreenshot.SaveAs ThisWorkbook.Path & "/screenshot" & str(i) & ".jpg"
            End If
        Next
        .Quit
    End With
End Sub

The outcome is a list of jpg files named sequentially "screenshot + number".

Is it possible to give every jpg file the name of its url instead of "/screenshot"?

Appreciating any valuable assistance with this request.

Many thanks in advance.

T.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about this- Untested...

VBA Code:
Public Sub Screenshots()
    Dim d As WebDriver, urls(), i As Long
    urls = Application.Transpose(ThisWorkbook.Worksheets("Sheet1").Range("A1:A200").Value) '<change this
    Set d = New ChromeDriver

    With d
        .AddArgument "--headless"
        .Start "Chrome"
        .Window.Maximize

        For i = LBound(urls) To UBound(urls)
            If InStr(urls(i), "http") > 0 Then
                .get urls(i)
                .TakeScreenshot.SaveAs ThisWorkbook.Path & "/" & urls(i) & ".jpg"
            End If
        Next
        .Quit
    End With
End Sub
 
Upvote 0
What is a "url name"? I know url and domain name, but can't find the definition of url name. I take it that you don't want the image name itself, as in
some-image-name.png without the png part. I also assume you don't want the entire url for an image name. I think you could easily exceed the name character limit with that.
 
Upvote 0
How about this- Untested...

VBA Code:
Public Sub Screenshots()
    Dim d As WebDriver, urls(), i As Long
    urls = Application.Transpose(ThisWorkbook.Worksheets("Sheet1").Range("A1:A200").Value) '<change this
    Set d = New ChromeDriver

    With d
        .AddArgument "--headless"
        .Start "Chrome"
        .Window.Maximize

        For i = LBound(urls) To UBound(urls)
            If InStr(urls(i), "http") > 0 Then
                .get urls(i)
                .TakeScreenshot.SaveAs ThisWorkbook.Path & "/" & urls(i) & ".jpg"
            End If
        Next
        .Quit
    End With
End Sub
Thank you very much igold for such prompt response. Testing the code, it runs for 2 seconds and stops without any issues or results. Hence, no screenshots were processed. I regret I don't have the knowledge to trace the reason why no screenshots weren't taken. I do appreciate any suggestions from your side.

Once again, thank you.
 
Upvote 0
Ok, thanks for the feedback. I do find it weird as the only line I changed was the "SaveAs" line. Perhaps as suggested by @Micron, it may stem from an illegal "Name" use. Perhaps you could provide us with some additional samples of your data. Real or factitious, but with a close resemblance to what you are dealing with.
 
Upvote 0
My apologies igold, it was my mistake that I didn't change "Sheet1" to "Sheet2" in my workbook. Nevertheless, running the code after this change yielded an error message:

Run-time error '0':
ImageError
The given path's format is not supported.

Debugging it, the following line was highlighted in yellow:
VBA Code:
.TakeScreenshot.SaveAs ThisWorkbook.Path & "/" & urls(i) & ".jpg"

Thank you very much ...
 
Upvote 0
Did you check the path that the code is now producing. Is it valid...

VBA Code:
MsgBox ThisWorkbook.Path & "/" & urls(i) & ".jpg"
.TakeScreenshot.SaveAs ThisWorkbook.Path & "/" & urls(i) & ".jpg"
 
Upvote 0
Did you check the path that the code is now producing. Is it valid...

VBA Code:
MsgBox ThisWorkbook.Path & "/" & urls(i) & ".jpg"
.TakeScreenshot.SaveAs ThisWorkbook.Path & "/" & urls(i) & ".jpg"
Sure it is valid, where the original code runs and produces the "screenshot 1" "screenshot 2" .. etc.
As I added MsgBox and ran the code, the message for the first url was:

c:\git_work\https://www.otr.om.au.jpg

followed by the error message (Run-time error'0') mentioned in my previous post.

In fact, I expect the path to be without http:// or https:// - thus the previous path would look like:

c:\git_work\www.otr.om.au.jpg

I'm more than appreciating for your great help, thank you very much.
 
Upvote 0
FWIW, this is what I had in mind (did it a while ago so not sure if it will fit the requirement)

VBA Code:
Dim intSlash As Integer, intDot As Integer
Dim strImageName As String

intSlash = InstrRev(urls(i),"/") 'this will include / so no need to concatenate it later
intDot = InstrRev(urls(i),".")
strImageName = Mid(urls(i),intSlash,intDot - intSlash) 'this drops off the file extension

.TakeScreenshot.SaveAs ThisWorkbook.Path & strImageName & ".jpg"
From this
httpsWasHere://www.verisign.com/en_US/resources/img/htgo_whaturl-diagram.png
I get
/htgo_whaturl-diagram

so you'd concatenate that to your directory path portion and concatenate the file extension to the end.
I altered the http part to prevent the forum from converting it to a url.
 
Upvote 0
How about this...

VBA Code:
urls(i) = WorksheetFunction.Substitute(urls(i), "https://", "")
urls(i) = WorksheetFunction.Substitute(urls(i), "http://", "")
.TakeScreenshot.SaveAs ThisWorkbook.Path & "\" & urls(i) & ".jpg"
 
Upvote 0
Solution

Forum statistics

Threads
1,215,339
Messages
6,124,381
Members
449,155
Latest member
ravioli44

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