I am trying to write a function in my code that allows me to search for a specific file based on it's name. The function is working but now the structure of the name has changed and it has caused trouble for me.
My code is in the form of macros that control a word document. This document gets information from another program and puts it in formfields on the word document. My code then takes the "value" of the formfields and uses them to search a specific folder for a .txt with a name that matches the stored value from the formfields.
Now the name of the .txt has been altered, the name will have some extra information that i do not have access to through the word document.
Example: W_X_Y_Z.txt Where Y and Z are available in the formfields of the document and can be accessed, but W and X are unknown to me.
Need to solve: So what i need to do is to figure out a way to ignore W and X and base the search on Y and Z only.
Ideas: One idea i have is to search for a file with the structre such as ??_??_Y_Z.txt but i am uncertain how this will work as in does "??" include numbers if it is in a string and does "??" mean only 2 unknown characters or any amount? If only two is there a way to define an unknown string where both the characters are unknown and the amount of characters?
Second idea is to use a delimiter on the _
and somehow figure out how i can tell the macro how to ignore everything after the second _
.
Any ideas or recommended investigations i should make are welcome.
Thank you.
I will try to add some code now, but keep in mind i inheritated alot of the code in this document and therefore i do not know exactly what every detail does thats why i have avoided typing the code.
Code as of now:
ActiveDocument.FormFields("Remissnummer").Select
Remiss = Selection.Text
Selection.Collapse Direction:=wdCollapseStart
ActiveDocument.FormFields("Personid").Select
strTmpPersonnr = Selection.Text
intNumCharsPersNr = Len(strTmpPersonnr)
Selection.Collapse Direction:=wdCollapseStart
strFileName = strTmpPersonnr
p_strFileReportPath = "G:\HMC\Fysiologiska kliniken\Eko\Export\"
strImportFile = p_strFileReportPath & "*" & strFileName & "_" & Remiss & strFileExt
If Len(Dir$(strImportFile)) = 0 Then
MsgBox "Error"
Else
'starts importing values from the file
This method has worked when the file name had a know structure i.e only Y_Z.txt Now though the structure has changed as i explained above, the two new variables are unknown, can be anything from letters to numbers.
i tried with:
Ignore = "*"
strImportFile = p_strFileReportPath & Ignore & "_" & Ignore & "_" & strFileName & "_" & Remiss & strFileExt
but it just froze and when going to task manager you could see more and more memory was allocated to word... I stoped it after 10 minutes.
So im guessing i cant account for the unknown parts of the file name with "*" but im kinda lost now.