VBA extract multiple file names and file paths from a string? (1 Viewer)

rex.withers

New member
Local time
Tomorrow, 06:39
Joined
Aug 6, 2013
Messages
7
Hi everyone, wondering if anyone has some help on this I'm trying to extract multiple file names and file paths from a string. In the sample string below (from an imported email that has an auto attachment save and link add-on) detail in the image below because the forum wouldn't allow my post due to it looking like spam??

Question.jpg


Thanks!
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 23, 2006
Messages
15,379
PLease include some sample records in a post--mock ups if you want.
 

rex.withers

New member
Local time
Tomorrow, 06:39
Joined
Aug 6, 2013
Messages
7
PLease include some sample records in a post--mock ups if you want.

Just the pure VBA is all that I need for example button 1 below (cmdExtractFiles) needs to extract the file names from A (txtSubject) and put it in B (txtFiles) once I the the code that does the extraction I can perhaps build on it in a sub table with links etc but I cant get past the first step (THANKS for your help)

Moreinfo.jpg
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 23, 2006
Messages
15,379
I understand but it's a bit of test data --not a picture of data--- that is needed to set it up.

Are the filenames structured in a similar pattern
File:///D:\EmailAttachments\2023\..................docx/png/jpg/pdf
 

rex.withers

New member
Local time
Tomorrow, 06:39
Joined
Aug 6, 2013
Messages
7

Here you go, attached
Yes always always the same path structure (as above) [Note I cant post any sort of path in the text here it gets blocked]

Thanks
 

Attachments

  • Sample.accdb
    452 KB · Views: 71

jdraw

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 23, 2006
Messages
15,379
It's late here.
Here's a mockup that may get you started. It only deals with 1 file per email at the moment.
Run the function in module1. Use f8 to step through the code. You'll need to check that there are startPosn and endposn values, if either or both are 0 then bypass/go to next record.
Good luck.
 

Attachments

  • DatabaseREX.accdb
    408 KB · Views: 66

KitaYama

Well-known member
Local time
Tomorrow, 05:39
Joined
Jan 6, 2022
Messages
1,541
Try this on your button's onclick event:

Code:
    Dim Lines As Variant
    Dim itm As Variant
    Dim result As String

    Lines = Split(txtJobActioncompleted, vbCrLf)
    For Each itm In Lines
        If InStr(itm, "Attachment saved to file:///") > 0 Then
            result = result & "," & Replace(itm, "Attachment saved to file:///", "")
        End If
    Next

    result = Right(result, Len(result) - 1)
    txtFiles = Replace(result, ",", vbCrLf)
 

Attachments

  • Sample.accdb
    480 KB · Views: 74

ebs17

Well-known member
Local time
Today, 22:39
Joined
Feb 7, 2020
Messages
1,946
With regular expressions and their Execute method you can read all paths in one fell swoop. With the very similar path names, creating the necessary pattern is not that difficult.
 

rex.withers

New member
Local time
Tomorrow, 06:39
Joined
Aug 6, 2013
Messages
7
It's late here.
Here's a mockup that may get you started. It only deals with 1 file per email at the moment.
Run the function in module1. Use f8 to step through the code. You'll need to check that there are startPosn and endposn values, if either or both are 0 then bypass/go to next record.
Good luck.
Thanks JDraw, that is great the way that you did it allows for it to be done as part of the import process very smart better than my idea. How would you amend that to handle multi files in the same string?
 

rex.withers

New member
Local time
Tomorrow, 06:39
Joined
Aug 6, 2013
Messages
7
With regular expressions and their Execute method you can read all paths in one fell swoop. With the very similar path names, creating the necessary pattern is not that difficult.
Ebs17 Do you have any example code for that I have never used regular expressions, brief investigation looked... tricky... (gulp)
 

jdraw

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 23, 2006
Messages
15,379
Thanks JDraw, that is great the way that you did it allows for it to be done as part of the import process very smart better than my idea. How would you amend that to handle multi files in the same string?
My mockup was to show the use of Instr and Mid when parsing generally. I thought that was your basic concern.
I like KitaYama's approach (y) and, if your data wasn't so organized, you might have to parse the array elements to remove extraneous characters.
 

ebs17

Well-known member
Local time
Today, 22:39
Joined
Feb 7, 2020
Messages
1,946

jdraw

Super Moderator
Staff member
Local time
Today, 16:39
Joined
Jan 23, 2006
Messages
15,379
Rex,

Here is a sample, based on previous, that uses KitaYama's split (sort of) and additional parsing.
It demos handling multiple file references within each record.

' ----------------------------------------------------------------
' Procedure Name: parseEmail4File
' Purpose: Custom proc to parse text an isolate specific file names and path
' Procedure Kind: Function
' Procedure Access: Public
' Author: Jack
' Date: 11-Aug-23

' Note: Element(0) will not contain the pattern since it is the separating value
' Other elements will contain 1 file with some extension
' Only extensions in fExt will be parsed
' ----------------------------------------------------------------
 

Attachments

  • DatabaseREXJJTest.accdb
    392 KB · Views: 73

Users who are viewing this thread

Top Bottom