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
Related
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
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.
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)
I have a user form to browse an excel file & a text file into 2 different textboxs (Selectionfile say ABC.xlsx & relationshipfile say GHJ.txt resp.) from local folders. On clicking the proceed button, the selected file must be copied from the path to the project location used by the program with same filenames with date(ABC_27092016.xlsx & GHJ_27092016.txt resp. as backup files) and in case if the file already exists with the same filename (if user selects the same filename in the user form) then the message should show "if to continue with the existing file in project folder? or replace it with the new file selected". If user selects replace then it should replace with the new file & date. If user selects not to replace it should not copy but proceed to next step.
This is the code below which just copies but doe not do the process above to check if file exist already or
'back up selected files to project folder & rename with date
Private Sub Proceed_Click(sender As Object, e As EventArgs) Handles Proceed.Click
Dim selectionpath As String = selectionfilepath 'selectionpath is from the textbox1 in userform
Dim relationpath As String = relationshipfilepath 'relationpath is from the textbox2 in userform
Dim Timestamp As Date = Now
Dim destination1 As String = "C:\Users\UserA\Documents\Visual Studio 2015\Projects\NetSoftware\Files\Example_selectionfile_" & Format(Timestamp, "yyyy-mm-dd") & ".xlsx"
Dim destination2 As String = "C:\Users\UserA\Documents\Visual Studio 2015\Projects\NetSoftware\Files\Example_relationfile_" & Format(Timestamp, "yyyy-mm-dd") & ".txt"
If (SelectionFIleName.Text = "" Or RelationshipFileName.Text = "") Then
MessageBox.Show("Please select one of the file")
Else
FileCopy(selectionpath, destination1) 'copying the excel file to project location
FileCopy(relationpath, destination2) 'copying text file to project location
Rename(destination1, selectionpath) 'renaming the excel file since it stores as Example_selectionfile instead of the exact name of the user slected filename and also missing the date
Rename(destination2, relationpath) 'renaming the excel file since it stores as Example_relationfile instead of the exact name of the user slected filename and also missing the date
End If
End Sub
I have a program for which I have developed a user guide. I have placed this user guide within the project directory. I created a MenuStrip Item by which to open the user guide in Word on the user's machine. I was successfully able to do this with the following code:
Try
userGuide = MSWord.Documents.Open("C:Users\administrator\Documents\VisualStudio2010\Project3\UserGuide.doc")
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
Catch ex As Exception
MsgBox("An error has prevented the document from opening. The document may not be available." & vbCrLf & vbCrLf & _
"Please try one of the following options:" & vbCrLf & _
"- Check to see if the document is already open" & vbCrLf & _
"- Restart the program")
End Try
The problem is, the path used to open the file will not exist on the users machine. This is a standalone system, so no file share can be created in which to place the document, therefore no common path can be coded.
Is there a way to code dynamic paths? Perhaps something like:
userGuide = MSWord.Documents.Open("%windir%\UserGuide.doc")
Thanks!
if the document will be stored relative to the install path of the application executable, then start with the path of the exe:
Dim path As String
path = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim docPath as String;
docPath = Path.Combine(path,"UserGuide.doc");