web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Extracting rows of ima...
Power Automate
Unanswered

Extracting rows of images from Excel in PAD using Run VBScript

(0) ShareShare
ReportReport
Posted on by 6

Hi All,

Stuck on this scenario for ages. I'm creating a "Run VBscript" action to loop through various rows of images present in an Excel worksheet, then name them and save them to a folder. Hoping to use the variables I've got in my PAD flow
ImageRows represent the rows of images, each row of images is separated by empty cells. 

Any help would be appreciated, as this current script keeps giving me wierd syntax errors like:
\AppData\Local\Temp\Robin\vvcrshifp1j.tmp(17, 11) Microsoft VBScript compilation error: Expected identifier


Here are the PAD variables:
%ImagesPath% = string = C:\Users\Automation\Images
%FilePath% = string = C:\Users\Automation\File1.xlsx
%ImageRowCount% = integer = 8
%ImageRowList% = list = ["ImageRow1", "ImageRow2"]
%ImageNameList% = list = ["image1", "image2, "image3"]


Here's my script:

Option Explicit

Dim xlApp, xlBook, sh, pic, i, path, ImageRowCount, row, startRow, endRow
Dim rng, intersectRange, excelFilePath, ImageRowList, imageNames

' Get the path where you want to save the images from the Power Automate variable
path = "%ImagesPath%"

' Get the path to the Excel file from the Power Automate variable
excelFilePath = "%FilePath%"

' Get the names of the image rows from the Power Automate variable
ImageRowList = %ImageRowList%

' Get the names of the images from the Power Automate variable
ImageNameList = %ImageNameList%

i = 0 ' Initialize the counter

' Create a new instance of Excel
Set xlApp = CreateObject("Excel.Application")

' Open the workbook
Set xlBook = xlApp.Workbooks.Open(excelFilePath)

' Set the worksheet that contains the images
Set sh = xlBook.Sheets("Sheet1")

' Loop through each image row
For imagerow = 1 To %ImageRowCount%
    ' Calculate the start and end rows for the current imagerow
    startRow = 7 + (imagerow - 1) * 38
    endRow = startRow + 30

    ' Loop through each picture in the worksheet
    For Each pic In sh.Pictures
        ' Check if the picture is within the specified range
        Set rng = sh.Range("C" & startRow & ":HA" & endRow)
        Set intersectRange = xlApp.Intersect(pic.TopLeftCell, rng)
        If Not intersectRange Is Nothing Then
            ' Save the picture as a .png file
            pic.Select
            pic.CopyPicture
            With xlApp.Charts.Add
                .Paste
                    .Export path & ImageRowList(Row - 1) & "_" & ImageNameList(i Mod 13) & ".png"
                .Delete
            End With
            i = i + 1 ' Increment the counter
        End If
    Next
Next imagerow

I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    I am pretty sure that VBscript will not accept a PAD variable of type list as an actual list due to different syntax. I suggest storing it into a string variable and splitting it in the script.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • UserProfile123 Profile Picture
    6 on at

    Still no luck.

    I used the "Join Text" action in PAD to store the lists as a string seperated by ','

    Then I updated the VB script to include:
    ImageRowList = Split("%ImageRowListString%", ",")
    ImageNameList = Split("%ImageNameListString%", ",")

    Now I get the error:
    ...\AppData\Local\Temp\Robin\lbj5epkmnu0.tmp(52, 6) Microsoft VBScript compilation error: Expected end of statement

    No idea what's going on.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    This usually indicates some sort of a mistake in punctuation. Can you share the full script?

  • UserProfile123 Profile Picture
    6 on at

    Here's the current script:


    Option Explicit

    Dim xlApp, xlBook, sh, pic, i, path, ImageRow, startRow, endRow
    Dim rng, intersectRange, excelFilePath, ImageRowList, ImageNameList

    ' Get the folder path where you want to save the images from the Power Automate variable
    path = "%ImagesPath%"

    ' Get the path to the Excel file from the Power Automate variable
    excelFilePath = "%FilesPath%"

    ' Get the names of the image rows from the Power Automate variable
    ImageRowList = Split("%ImageRowListString%", ",")

    ' Get the names of the images from the Power Automate variable
    ImageNameList = Split("%ImageNameListString%", ",")

    i = 0 ' Initialize the counter

    ' Create a new instance of Excel
    Set xlApp = CreateObject("Excel.Application")

    ' Open the workbook
    Set xlBook = xlApp.Workbooks.Open(excelFilePath)

    ' Set the worksheet that contains the images
    Set sh = xlBook.Sheets("Sheet1")

    ' Loop through each image row, using the Power automate variable for the image row count
    For ImageRow = 1 To %ImageRowCount%
        ' Calculate the start and end rows for the current row of images
        startRow = 7 + (ImageRow - 1) * 38
        endRow = startRow + 30

        ' Loop through each picture in the worksheet
        For Each pic In sh.Pictures
            ' Check if the picture is within the specified range
            Set rng = sh.Range("C" & startRow & ":HA" & endRow)
            Set intersectRange = xlApp.Intersect(pic.TopLeftCell, rng)
            If Not intersectRange Is Nothing Then
                ' Save the picture as a .png file
                pic.Select
                pic.CopyPicture
                With xlApp.Charts.Add
                    .Paste
                        .Export path & Trim(ImageRowList(ImageRow - 1)) & "_" & Trim(ImageNameList(i Mod 13)) & ".png"
                    .Delete
                End With
                i = i + 1 ' Increment the counter
            End If
        Next
    Next ImageRow

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    I don't think you need the "ImageRow" on the last "Next" statement at the end of your script. It should simply say "Next" and that's it. Like in this snippet:

    Option Explicit
    
    Dim xlApp, xlBook, sh, pic, i, path, ImageRow, startRow, endRow
    Dim rng, intersectRange, excelFilePath, ImageRowList, ImageNameList
    
    ' Get the folder path where you want to save the images from the Power Automate variable
    path = "%ImagesPath%"
    
    ' Get the path to the Excel file from the Power Automate variable
    excelFilePath = "%FilesPath%"
    
    ' Get the names of the image rows from the Power Automate variable
    ImageRowList = Split("%ImageRowListString%", ",")
    
    ' Get the names of the images from the Power Automate variable
    ImageNameList = Split("%ImageNameListString%", ",")
    
    i = 0 ' Initialize the counter
    
    ' Create a new instance of Excel
    Set xlApp = CreateObject("Excel.Application")
    
    ' Open the workbook
    Set xlBook = xlApp.Workbooks.Open(excelFilePath)
    
    ' Set the worksheet that contains the images
    Set sh = xlBook.Sheets("Sheet1")
    
    ' Loop through each image row, using the Power automate variable for the image row count
    For ImageRow = 1 To %ImageRowCount%
     ' Calculate the start and end rows for the current row of images
     startRow = 7 + (ImageRow - 1) * 38
     endRow = startRow + 30
    
     ' Loop through each picture in the worksheet
     For Each pic In sh.Pictures
     ' Check if the picture is within the specified range
     Set rng = sh.Range("C" & startRow & ":HA" & endRow)
     Set intersectRange = xlApp.Intersect(pic.TopLeftCell, rng)
     If Not intersectRange Is Nothing Then
     ' Save the picture as a .png file
     pic.Select
     pic.CopyPicture
     With xlApp.Charts.Add
     .Paste
     .Export path & Trim(ImageRowList(ImageRow - 1)) & "_" & Trim(ImageNameList(i Mod 13)) & ".png"
     .Delete
     End With
     i = i + 1 ' Increment the counter
     End If
     Next
    Next

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • UserProfile123 Profile Picture
    6 on at

    Thanks,

    The script actually started running, which is a good sign. However, it went on an endless loop for 20minutes and I had to manually stop it.
    A few rows of images should have been finished in 3 minutes or less.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 274 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 175

#3
Haque Profile Picture

Haque 166

Last 30 days Overall leaderboard