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 / Understanding the Func...
Power Automate
Answered

Understanding the Functionality of a VBA Script (using variables)

(0) ShareShare
ReportReport
Posted on by 25

I have a VBA script that I’m trying to use in Power Automate Desktop, but I’m encountering some error. I would appreciate it if someone could help me identify any potential issues.

 

Here’s my script:

 

Dim objFSO, objFile

Dim OrderNumber, MatchedFile

Dim Files

OrderNumber = CStr("%varOrderNumber%")

Files = "%varCardsListInShopFolder%"

Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each File In Files

    Set objFile = objFSO.GetFile(File)

    If InStr(objFile.Name, OrderNumber) > 0 Then

        MatchedFile = objFile.Name

        Exit For

    End If

Next

 

If MatchedFile <> "" Then

    Result = MatchedFile

End If

WScript.Echo Result

 

**

 

The script is supposed to do the following:

  1. Set the value of OrderNumber to the value of %varOrderNumber% (it is my variable set in the flow).
  2. Set the value of Files to the value of %varCardsListInShopFolder%
  3. Create a FileSystemObject which allows for manipulation of the file system.
  4. Search through the list of files specified by Files.
  5. For each file in the list, check if the file name contains the order number specified by OrderNumber.
  6. If the file name contains the order number, save the file name to MatchedFile and exit the loop.
  7. At the end, if MatchedFile is not empty, display a message that the matched file was found. Otherwise, display a message that no matched file was found.

I’m replacing %varOrderNumber% and %varCardsListInShopFolder% with actual values or variables available in my script environment before running the script. The varOrderNumber is a number consisting of several digits, and varCardsListInShopFolder is a list of file paths containing full file names with the path (e.g., C:\Users\BP 45\OneDrive - MY\POWER AUTOMATE\xxxxx\yyyy\1517\1517 xxxxx XXXXXXXX_aaaaa Bbbbbbbb_07.06.2024_02.05.10_22708.pdf).

The error I’m getting is: (9, 203) Microsoft VBScript - compilation error: Lack of end of character constant

 

Any help would be greatly appreciated!

  • cwischnewski Profile Picture
    162 on at

    Looks like you are setting Files to the string of the file path, then looping on that string.  I am pretty sure you need to tie that FSO to the string of the path to get the list of files before you can loop on them.  But I don't do a lot of VB Scripting work, so there may be more issues here.

  • Steveg48 Profile Picture
    28 on at

    I believe you can only have a string as an input to the script. So you have to convert it to a string using a join action before calling the script. Inside the script you can split it.

  • Verified answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @IzabelaZarnowsk ,

     

    The error message you’re seeing, (9, 203) Microsoft VBScript - compilation error: Lack of end of character constant, typically indicates that there’s a syntax error in your VBScript code.

     

    Please modify your VBscript to as given below.Copy the below code and paste it into your VBscript action and try it.It will fix your issue.

     

    Dim objFSO, objFolder, objFile
    Dim OrderNumber, MatchedFile
    Dim Files
    
    OrderNumber = CStr("YourOrderNumber") ' replace with your actual order number
    FolderName = "YourFolderPath" ' replace with your actual folder path
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(FolderName)
    
    For Each objFile In objFolder.Files
     If InStr(objFile.Name, OrderNumber) > 0 Then
     MatchedFile = objFile.Name
     Exit For
     End If
    Next
    
    If MatchedFile <> "" Then
     Result = MatchedFile
    End If
    
    WScript.Echo Result

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

  • IzabelaZarnowsk Profile Picture
    25 on at

    Thank you very much for your help!

    I have one more question. I now want to use the VBScriptOutput variable in the ‘Extract text from PDF file’ action, but when I use the variable as the file path, the program displays an error suggesting a problem with the path. This error also appears in several other actions where I tried to use variables to determine the file path. What could be the reason? The path definitely does not contain errors (it may contain special Polish characters, but the program has no problem with them when I enter the path to the file or folder as text). I would be grateful for any help

  • VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @IzabelaZarnowsk ,

     

    can you send the path what VBscript is generating so i will check and get back to you.

     

    Regards,

    Vishnu Reddy

  • IzabelaZarnowsk Profile Picture
    25 on at

    Hello,

    I sent the path in a private message.

    I appreciate your help.

    Regards,

    Iza Żarnowska

  • Verified answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @IzabelaZarnowsk ,

     

    Please follow this steps.

    1. Just manually check the path of PDF.Copy the path of pdf manually and compare with VBscript generated path.just check If Both are same.

    2. Just trim the VBscript output as shown in below image.

    VishnuReddy1997_0-1718340293091.png

    VishnuReddy1997_1-1718340314762.png

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

  • IzabelaZarnowsk Profile Picture
    25 on at

    Hello, @VishnuReddy1997 

    Trimming was a good direction. Thank you very much for your help!

    Best regards!

    Izabela Żarnowska

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

Leaderboard > Power Automate

#1
11manish Profile Picture

11manish 273 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 235

#3
David_MA Profile Picture

David_MA 172 Super User 2026 Season 2

Last 30 days Overall leaderboard