VBA open a csv file with unknow file name - vba

I have got a rather simple question as i think but i couldnt find out it myself.
I want to open a csv File in a defined binder but with an unknow filename. I would asume that it should work with simply "path/*.csv" however it is not :( The errormessage says "Wrong Filename". Do i need to use something else in VBA.
path = ActiveWorkbook.path & "\input\"
Open path & "*.csv" For Binary As #1
The above code does not work for me :( The CSV is called xyz.csv
path = ActiveWorkbook.path & "\input\"
Open path & "xyz.csv" For Binary As #1
The code above is working however i have fix added the csv filename, which in this case is xyz.
Somebudy knows how to get that thing to work?
Cheers and thx for your time
Marc

Dim path As String
Dim csvFiles As String
path = ActiveWorkbook.path & "\input\"
csvFiles = Dir(path & "*.csv")
Do While Len(csvFiles) > 0
Debug.Print csvFiles
csvFiles = Dir
Loop
You can use the Dir() Function to check the files in your folder if you don't know the filename.

Related

VBA FileExists and Sharepoint

I'm running into issues trying to pull info from files stored in Sharepoint.
Namely, FileExists isn't working and Overwrite file doesn't seem to be working either.
There was a discussion here, but few answers -> posting this question again in hopes some things have changed
My code runs like this:
strFileExists = Dir(Filepath & Filename)
And returns: File path not found -> I checked the path and even opened a file and recorded the macro to make sure it was the same file path without issue, but it appears DIR() is the issue.
The business dept I'm working with is entirely switching over to Sharepoint so hoping there's a straightforward solution without setting up network shares or doing C/personal/OneDrive things
You can navigate and look for files on OneDrive like this
Sub check_File_Exists()
Dim path As String
Dim strType As String
Dim file As Variant
Dim yourFile As String
'replace uname with your user name
path = "C:\Users\uname\OneDrive\"
strType = "*txt"
yourFile = "test.txt"
file = Dir(path & strType)
Do While (file <> "")
If file = yourFile Then
Debug.Print ("File: " & file & " found!")
Exit Do
End If
file = Dir
Loop
End Sub
Hope it helps

Is there an argument for copyfile that will change the hidden property?

I have a database that writes data into a copied excel template. The template is hidden to keep the end user from tampering with it, however the final result is also hidden. Is there a way to change the hidden property when saving the new file?
Currently, the db copies the template and renames it.
fso.CopyFile "C:\Upload\Rebate_Upload_Files\Standard Form (Template)
protected.xlsx", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx"
After that, it transfers the appropriate table and saves the file.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml,
"export_table", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx", False, "A12:L65000"
The process works fine, except that the final file is also hidden and I'd like it to be a normal file.
Thanks
Not for CopyFile-which is a FileSystemObject method, but there is one for a File object. We'll just update it after the copy is complete.
For simplicity i've replaced your file output path to a string variable.
originalFileName = "yourStartingFile"
copyFileName = "yourCopiedFile"
set fso.CopyFile OriginalFileName, CopyFileName
--after copying, get file that was copied
--set attributes value of file to 0. 0 = Normal, 2 = Hidden
f = fso.GetFile(copyFileName)
f.attributes = 0
More reading for additional details.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/file-object
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/attributes-property
Mike's code above worked, however there was a few more steps involved for me so I wanted to provide the full code for anyone stumbling upon this.
First, in order to use FileSystemObject, you need to enable it in your VBA Editor by going to Tools->References and enabling "Microsoft Scripting Runtime". Then, using the code below, you can copy a hidden file and set the new file (via f.Attributes) to not be hidden:
' SourceFile is the full path name to my original file
' FileNAme is the full path name to my new file
Dim fso As Scripting.FileSystemObject
Dim f As File
Set fso = New Scripting.FileSystemObject
Call fso.CopyFile(SourceFile, FileName, False) ' Set to true to overwrite
Set f = fso.GetFile(FileName)
f.Attributes = 0

String search by a fixed text and * wildcard

I have some files in a folder that are named "Panel Countries_15.05" and "Non-Panel Countries_15.05", the date will change according to month, and I'm trying to target open these files regarding the month that will be. The problem is when I'm trying to open the "Panel Countries"(alone) it will open the "Non-Panel Countries". Is there a way to search after a fix text and then a wildcard?:
directory = "C:\Users\calin.lencar\Desktop\Feasibility & Capacity\Raw Data\"
fileName = Dir(directory & "*Panel countries*.xlsx")
directory = "C:\Users\calin.lencar\Desktop\Feasibility & Capacity\Raw Data\"
fileName = Dir(directory & "*Non-panel countries*.xlsx")
Thanks in advance

How to list only file name?

I have program to find files in a directory and list them in a listbox, but the following code I'm using adds the full path for the file found.
Is there something I'm missing to make it only add the file name and not the full path?
If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text) Then
For Each FoundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text)
ListBox.Items.Add(FoundFile)
Next
Else
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text)
End If
so to fix it i only had to put ListBox.Items.Add(IO.Path.GetFileName(FoundFile)) instead of ListBox.Items.Add(FoundFile)
Here is a working example to list file name individually with GetFileNameWithoutExtension, along with the way you are using GetFileName.
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String
result = Path.GetFileNameWithoutExtension(fileName)
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result)
result = Path.GetFileName(pathname)
Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result)

How to find filepath of open MS Access file

I have MS Access Switchboard created by my self and trying to figure it out how to find the filepath of the same one when it is open. For example if it is open in Desktop to get C:\Desktop and etc.
I really do not know if it is possible at all, but if someone have an idea how to do it in MS Access vba would be very helpfull.
Thanks.
Try use this to get current path file in vba
dim currentPath = CurrentProject.Path & "\"
dim currentFileName = CurrentProject.Name
dim fullPath = currentPath & currentFileName