i've been wondering if there's a way to to use the "GetFileName" to get the name from a path that exists in a cell instead of using a direct path eg below
Filename = fso.GetFileName(Worksheets("Setup").Cells(8, 2).Value)
instead of
FileName = FSO.GetFileName("C:\ExamplePath\ExampleFile.xls")
Thank you in advance for any hints
Related
This question already has answers here:
How to extract file name from path?
(16 answers)
Extract filename from path [duplicate]
(9 answers)
Closed 1 year ago.
Background:
I have written some VB to search through a directory for a file (the date of the file will be variable, hence the wildcard) the code will then open the file, copy a list of names to another spreadsheet, store the names in an array so I can return their relative position in the other spreadsheet - pos, I need to do this as the next part of the code writes a link to that spreadsheet.
My filename search code is as follows:
Dim lcFound As String
lcFound = Dir("C:\Users\KDelaney\Desktop\*XYZ Select XYZ List.xlsm")
If lcFound <> "" Then
Workbooks.Open Filename:=lcFound
End If
Not going to add all the code as I don't think it's relative to the question. The writing a link formula to each cell in a f loop is as below, the below code was fine before I was using the file search, as I was testing it with the full file name and path:
Cells(f, 4).Formula = "=" & "'" & lcPath & "[" & lcFile & "]" & "Template" & "'!" & "AB" & pos
Question:
Basically the question is what would be the best way to convert C:\Users\KDelaney\Desktop\*XYZ Select XYZ List.xlsm into lcPath and lcFile, or is there a way round separating the strings? (Have briefly tested it unseparated but it seems like the square brackets around the filename is required).
I'm guessing the best course of action would be using InStr function? to return the position of the end of the file path? e.g. I know the filepath will always end with "Desktop\" so if I use InStr to return that position I will then know the filepath is between 1 and the returned value of InStr. However this is where I have stalled, not sure where to go from here, in terms of separating the two strings. I also know the start of the file name will always begin with a number and end with the file extension. I am probably making a meal of this problem and someone will come back with a very simple solution (hopefully)
Any help would be appreciated. Apologies if the question was too in depth just wanted to give you the full picture.
Thanks.
Sorry for:
a) Not researching enough if the question has already been asked/answered
b) answering my own question
Link contains two good answers, a function and using file system object.
How to extract file name from path?
I am going to persevere using the split function for my own knowledge and if things don't go well I will resort to one of the other two methods in the link above.
Further edit: haven't cleaned the code up yet, but I have realised I was massively overcomplicating the problem, all it required was a bit of messy string manipulation. If anyone comes across a similar problem:
Dim WrdArray() As String
Dim lcFound As String
strPath = "C:\Users\KDelaney\Desktop\*XYZ Select XYZ List.xlsm"
newstrL = InStrRev(strPath, "\")
newstrL = newstrL - 1
strlen = Len(strPath)
lnPath = strlen - newstrL
MsgBox Left(strPath, lnPath)
Definitely not the best way of doing it, have realized there are 'multiple ways to skin a cat' in this situation.
Link below for lots of info on string manipulation:
https://www.excel-easy.com/vba/string-manipulation.html
I have a worksheet DATA with the table populated from json file through the Microsoft Query.
There're different json files so I need to create several connections to any of those files.
I also have a cell on another worksheet where I would like to indicate a parameter (for example Yesterday,Today,Tomorrow).
According to selected parameter the table in the DATA worksheet should be populated from the related data connection (yesterday.json, today.json, tomorrow.json).
Is it possible to do it? If yes, what would be the procedure?
I have an idea that it might be possible to do by changing the filename inside the query.
For example, this is my query:
let
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
FullPathToFile1 = FilePath & "\json\today.json",
Source = Json.Document(File.Contents(FullPathToFile1)),
So am thinking if there's some way to "inject" filename in the above query based on value of some cell.
Will appreciate any help, links etc.
Thanks!
UPDATE:
I have created a named cell jsonPath and put the file name in it.
Then I have modified above query as follows, but it gives me an error.
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
FullPathToFile1 = FilePath & "\json\" & [jsonPath],
Source = Json.Document(File.Contents(FullPathToFile1)),
I got it working by modifying my query as follows:
FilePath = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
FileName = Excel.CurrentWorkbook(){[Name="jsonPath"]}[Content]{0}[Column1],
FullPathToFile1 = FilePath & "\json\" & FileName,
Source = Json.Document(File.Contents(FullPathToFile1)),
so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you
You can get the file name using the Dir function and multiple character (*) wildcard.
Const Path As String = "C:\Test"
Dim filename As String
filename = Dir(Path & "\*.xlsx")
If Len(filename) > 0 Then
' Do your work
' Remember 'filename' only holds the file name
' you will need to attach the rest of the path to get the full directory.
End If
Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.
An example:
File name: daily_report_20190404.xlsx
filename = Dir(Path & "\daily_report_*.xlsx")
Hope this helps.
I am creating and saving .ini files in Excel. The files need to follow a specific naming convention that increments by 1 each time a file is created. Not all the files will be created in Excel, some will be done in a separate program. Is there a way to read the saved files in their folder to know which number is next?
For example there are files named exampleFile1.ini through exampleFile63.ini. From Excel using VBA or other means can I see that exampleFile63.ini was the last file and name the next exampleFile64.ini?
Thank you. I'm very new if thats not obvious.
This function will return the next available .INI file name:
Private Function GetNextFile() As String
Dim i As Long
Dim fileName As String
For i = 1 To 1000
' Update Path and File Name prefix below to where your .INI files will be stored.
fileName = "d:\path\exampleFile" & i & ".ini"
If Dir(fileName) = "" Then
GetNextFile = fileName
Exit Function
End If
Next
End Function
Call it like this:
Dim NextIniFile As String
NextIniFile = GetNextFile()
I have found plenty of vba for inserting images into a comment
Selection.ShapeRange.Fill.UserPicture "C:\Temp\Pictures\ewe.jpg"
How can you determine the image already used for an comment?
I would like to extract the embedded image names if possible.
Is there not a property to access that will give me this?
In the comment Fill Effects dialog box the image name somehow seems to be accessible.
Sorry, I didn't have the reputation to just comment on your question for clarification.
I made a test file, inserted a comment and image in that comment, and then extracted the base files. I then checked them all for the original file name. I also found the embedded JPEG and decoded it to get the metadata. As you've noted, the original file names are stored in xl\drawings\vmlDrawing1.vml (once you've extracted the xml files from the excel file by appending .zip to the filename and then running an unzip utility on it). I did find the file name, but not the path or file type, so I'm fairly certain that the path and file type aren't preserved.
If just the file name is sufficient for you, then that file contains information for each drawing that you have, and those will include the cell location, although they're 0 based, so you'd have to add one to get the actual row and column. My question is two part:
1) Is the file name alone sufficient, or did you need the entire path? If you needed the entire path, I think you're out of luck, since the paths are on a different computer and you can't even search for them if you do extract the file name.
2) If that is all you need, does the solution have to be VBA? In the past, I have programmatically unzipped and manipulated the xml base files, but it's a little tricky. It's simplified by the fact that you only have to read out the data, so that's a plus. I did it in .net before, but I'm sure that if it had to be VBA it could be done, but it would be simpler if you were open to the type of solution.
Let me know, I'd be happy to help you out.
====================================================================================
Try this: make a copy of the spreadsheet, append .zip (test.xlsm.zip), and then extract the files manually. Change vmlPath to the location of your xl\drawings\vmlDrawing1.vml file. Then run this. I did make some assumptions, for instance, I assumed that the order of the nodes and attributes would always be the same and so I used hardcoded indexes (shp.attributes(0), etc) instead of using logic to make sure I had the correct node or attribute, but you seem like you know your way around VBA, so I'm just going to code a barebones. This will need a reference to Microsoft XML 6.0.
Sub vmlParse()
Dim vmlPath As String: vmlPath = "C:\Users\Lenovo\Desktop\test - Copy.xlsm\xl\drawings\vmlDrawing1.vml"
Dim this As Worksheet: Set this = ActiveSheet
Dim doc As New DOMDocument, shps As IXMLDOMNodeList
Dim shp As IXMLDOMNode, n As IXMLDOMNode, a As IXMLDOMAttribute
Dim fileName As String, productID As String
Dim rng As Range, r As Long, c As Long
doc.Load vmlPath
Set shps = doc.getElementsByTagName("x:ClientData")
For Each shp In shps
If shp.Attributes(0).nodeValue = "Note" Then
r = 0: c = 0
For Each a In shp.ParentNode.FirstChild.Attributes
If a.nodeName = "o:title" Then
fileName = a.nodeValue
Exit For
End If
Next
For Each n In shp.childNodes
If n.nodeName = "x:Row" Then r = n.text
If n.nodeName = "x:Column" Then c = n.text
Next
Set rng = this.Cells(r + 1, c + 1)
productID = rng.Value
'now you have the productID, the fileName, and the cell location
End If
Next
End Sub
Let me know how that worked out for you.
If c4 contains your comment:
Set shp = Range("C4").Comment.Shape
if shp.Fill.TextureType = msoTextureUserDefined then
end if