Cast Exception...type System.String to type System.String[]?

Hello Everyone, I’m sure I did something incredibly silly to get this error, cause I’m 5 days brand new to uipath and I can’t find this type of situation anywhere in the forum. Any help to point me in the right direction? So grateful for any help here! I’ve enjoying learning so much from all of you!

So far I have a workflow that will open emails and save attached pdfs to a dedicated folder. That’s all working great.

Now I’m trying to open all the files, extract text from the pdfs, and input to excel file.

I chose the “for each file in folder” and now getting this error.


Source: For Each

Message: Unable to cast object of type ‘System.String’ to type ‘System.String’.

Exception Type: System.InvalidCastException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidCastException: Unable to cast object of type ‘System.String’ to type ‘System.String’.
at UiPath.Core.Activities.ForEach1.InternalExecute(NativeActivityContext context, IEnumerator valueEnumerator) at UiPath.Core.Activities.ForEach1.Execute(NativeActivityContext context)
at System.Activities.NativeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Tried to figure out how to include my .xaml, but not sure how to do that? If needed, could you give me the details of how to include my workflow here? Thank you!

Hi @Poptart90,

Can you send me the screenshot of th code what u did in the xaml file.

Arivu :slight_smile:

Thank you, arivu96! Really appreciate you looking at this for me. Let me know if you need anything else.



If it’s possible can you send the xaml I can check and update you.

Arivu :slight_smile:

ReadAllPDFs.xaml (13.9 KB)

Here you are! So the goal is to eventually extract 8 items from each PDF and export to an Excel file. I was starting small here. Just trying to get one item (Vendor No) to extract for several files and display in Message Box to make sure all pathways were good, etc. Please let me know if I’ve made a big error somewhere…really appreciate your time. Thank you!

The ‘For each File in folder’ command returns an output in a type ‘Array’ format.

Check your variable for the command ‘For each file in folder’ - it’s probably created as a string type. Change it to an ‘Array’ and the error will be gone.


grabFiles = Directory.GetFiles(xxx)

What you did:
grabFiles = String type

What you should do:
grabFiles = String of an array type

Anyway I was bored so I took a look at your xaml file and made some comments and minor changes into it.

Here it is! You can edit the eventual pdf output text into an excel that you want (just use excel app scope then write range/append text depending on what you need)

Test_ReadPdfFiles.xaml (12.9 KB)

AH! I missed that somewhere- that the output was an “array” type. Thank you!

Ugh, Slap forehead! Yes, the looping action was what I was going for…I see that I did not choose it specifically. I thought the “for each” was a loop. Thank you for correcting me! And for the notes in the file, too. Really appreciate it. Question, there was a missing field? Not sure if that was intentional? I’ll post a screenshot.

Final question (for now! :wink: ) the append text is what will make it possible to move text items, like “description” (which is a part of the invoice document) into an excel file, correct? Is it necessary to build a data table first (as many in the forum suggest) or will write range/append be sufficient? For reference, 7 out of the 8 columns are numbers (one integer, 6 whole numbers) the last column will be a short text “description”.

Again, many thanks!


Also, with “Select Folder”…is there anyway to designate the folder where the pdf’s are directly? Without having to “select” from a prompt? That’s what I was hoping to accomplish with the “Start Process”, but maybe I got that wrong too?

The missing field is the ‘Read pdf’ activity, you’re missing it because you didn’t have that package installed.

Simply go to ‘Manage packages’ by pressing the keyboard button (Ctrl + P) and look for the pdf package by UiPath.

There are various functions of excel - 2 of them which are appendtext and write range.

What append text does is that it adds another row of text below the existing text!
Write range will overwrite all existing data in the excelsheet with new data.

Generally, for speed operations, adding data into a DT bfore a one-shot writing excel operation is essential. (better than write, open, write open write open…etcra)

If I am correct about you wanting to grab certain ‘text’ from the pdf file, you can perform a variable regex search (Matches activity) on the body of pdf text (it should be in a variable) and store the resulting output into your data table, then oneshot write it all in excel

No, you can hardcode the filepath as it is, there’s nothing wrong with what you’ve done, lol

Hurray! Something right! lol

Gotcha. ok, going to do lots more reading and learning about the “regex” search. What you described is what I was hoping for - grab 8 text items from each pdf invoice in the folder and write to excel. Probably too much to hope I could use the anchor base for all eight? :wink: Selectors and I have a “challenging” relationship! ha

Yes, that is correct.

The benefits of using regex search is that you can perform all operations in the ‘background’ - rather than anchor-image identification (foreground pdf). Background operations are way, way faster than foreground!