I have written a function which opens a dialogue box and lets the user select a folder. This function returns a string which is simply the path of the folder selected.
FolderPath = BrowseForFolder()
Now the idea is that this FolderPath refers to a special folder which has tons of excel (.xml) files in it. What I wish to do is get specific location (which has their names) for each of these .xml files in this special folder. I don't want the user to select all the filer, since there will be a lot of them. So in short, i want lots of strings denoting paths to these .xml files in FolderPath
Sahil, I would do this with a scripting object.
Dim FileCollection as Collection
Dim FSO as Scri[ting.FileSystemObject
Dim FLD as Scripting.Folder
Dim FIL as Scripting.File
You have to set a reference to the Microsoft Scripting Runtime library, or declare them all as Objects and use CreateObject to build them on the fly. Use Set FLD = FSO.GetFolder(FolderPath) to connect the object with your path string. Then use For Each FIL in FLD.Files to loop through the contents looking for xml files, and put them into the collection with FileCollection.Add File.Path.
Related
I have problem searching folder and find all files with specific name.
With this method I am able to browse my folder and find all files. But only Word files.
Dim fi As FileInfo() = path.GetFiles(fileName & "*.doc*", SearchOption.AllDirectories)
Now I would like to search Word and PDF. Is there a way how I can find it?
If I just write a dot instead of .doc it searches the folder but finds all files. Excel, txt etc.
Dim fi As FileInfo() = path.GetFiles(fileName & "*.*", SearchOption.AllDirectories)
The goal would be to find only certain types of files.
It is not possible to use the GetFiles method to enumerate files with multiple extensions. To do that, you'll have to write your own code to do the filtering. I also recommend you use the EnumerateFiles method as it provides better performance over the GetFiles method in most cases.
Here is sample code to do the filtering as you require
Dim filterExtensions = {".docx", ".pdf"}
Dim files = From f In System.IO.Directory.EnumerateFiles("C:\Path", "*.*", _
SearchOption.AllDirectories)
Let fi = New FileInfo(f)
Where filterExtensions.Contains(fi.Extension.ToLower())
Select fi
I would like to specify where my new Excel file is saved in terms of the folder (e.g. the folder name is Input Data). However, I do not want to specify the drive that it is contained in (e.g. C:\Input Data), how should I go about it?
I saw that other people typically would specify the full file path to where the folder is e.g. C:\Input Data. However, I would like to drop the C:\ portion.
The expected result would be where the output file is saved in the folder called "Input Data"
Rather than trying to specify an unknown location you could use a system location. Not only will this be consistent (for each user) but it should be a location where files can be saved. Many systems have been set up not to allow folders to be created in the root of the C:\ drive.
Dim fso As Object
Dim pth As String
Set fso = CreateObject("Scripting.FileSystemObject") 'create a filesystemobject
pth = fso.buildpath(Environ("userprofile"), "Input Data") 'get the path
If Not fso.FolderExists(pth) Then fso.CreateFolder pth 'create the folder if required
pth = fso.buildpath(pth, "My Workbook Name.xlsm") 'get the save path
ThisWorkbook.SaveAs pth 'save
Set fso = Nothing
To see the other system folders see https://pureinfotech.com/list-environment-variables-windows-10/ You might prefer to use %TEMP%.
You will need to change the Save statement to suit
It's been years since I've used Visual Basic. I downgraded from 2017 to 2010 (The version I was using while I was in school). I figured VB would be the best way to attempt a solution. (Although I'm sure there are other languages that would do it as well.)
I'm looking to get back into programming. Let me get to the problem.
My friend has an ever growing amount of text documents in a folder, and he wants a program to choose one at random, and open it.
I thought I'd put a TextBox with a Button that would let him open the folder where he stores his files. Then this program would read the number of text files in that folder, and randomly generate a number between one and that number, select, and open the document with its default program (if it's text, notepad; if it's DocX then word.)
I've been sitting at a blinking cursor for 45 minutes. I've gone on YouTube for help with this project.
Any advice, or help you guys can give me? Does this need to be simplified?
That sounds like a reasonable strategy to me.
It might be worth displaying some sort of progress to the user, say by putting the name of current file name being read into the status bar, in case there's a long delay reading the file names due to the large number of files in the folder, and/or a slow-running network drive. If you do this, remember to put a DoEvents into your loop to allow screen updates to display.
There's a separate thread on how to open files in their native handler here.
Hope this helps - good luck!
Option Explicit
Public oFSO As Object
Public arrFiles()
Public lngFiles As Long
Sub Main()
Dim sPath As String
sPath = InputBox("Enter folder path", "Folder path")
' clear starting point
lngFiles = 0
Erase arrFiles
Set oFSO = CreateObject("Scripting.FileSystemObject")
Call recurse(sPath)
Randomize
Dim lngRandomFileNumber As Long
lngRandomFileNumber = CLng(lngFiles * Rnd) + 1
MsgBox "This is random file, that will be opened: " & arrFiles(lngRandomFileNumber)
Call CreateObject("Shell.Application").Open(arrFiles(lngRandomFileNumber))
End Sub
Sub recurse(sPath As String)
Dim oFolder As Object
Dim oSubFolder As Object
Dim oFile As Object
Set oFolder = oFSO.GetFolder(sPath)
'Collect file information
For Each oFile In oFolder.Files
lngFiles = lngFiles + 1
ReDim Preserve arrFiles(lngFiles + 1)
arrFiles(lngFiles) = sPath & "\" & oFile.Name
Next oFile
'looking for all subfolders
For Each oSubFolder In oFolder.SubFolders
'recursive call
Call recurse(oSubFolder.path)
Next oSubFolder
End Sub
You can paste this code in any VBA supporting application (MS Access, MS Excel, MS Word), call VBA editor (Shift + F11) and paste this code. After that press F5 and select Main() function. You'll see prompt to enter folder path, and after that you would get random file path.
I think it should be understandable in practice to see what program do
Updated: #Belladonna mentioned it clearly, to open file in default program.
NB: This code is passes through subfolders also, if you want to exclude subfolders, you should comment the recursive call block in recurce function
I have a procedure using the Office.FileDialog object that goes to a remote folder, sets a FileDialog.Filter to search for a specific file in that folder (i.e., .Filter = "\\PATH\MYFILE*.pdf"), and then uses FileDialog.Show to display a window containing only that target file. When the window is displayed, it contains not only my target file but also all of the sub-folders in the target folder. How can I display only the files, and not the sub-folders?
As filtering applies only to files, and there's nothing of the kind in fileDialogType - I don't think what you're asking for is possible in VBA.
This will show only .pdf Files in the FileDialog. Just Add the specific path to the folder you want to open the FileDialog to in the quotations after ChDir "YourFile_Path".
Dim myFile As String
ChDir "C:\Users\Documents"
myFile = Application.GetOpenFilename(Title:="Import pdf File", FileFilter:="Pdf Files *.pdf (*.pdf),")
I have a problem with declaring Folder objects.
I'm trying to type plik_zip Folder, but VBA editor (in Excel 2013) converts it to folder
Sub Submitt()
Dim plik_zip As folder
Reference changing makes no effect.
folder is not a type in your case and besides VBA is not case sensitive.
Perhaps you want to do something like this:
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the folder object
Set objFolder = objFSO.GetFolder("C:\Temp")
The VBE will do this if you declared a variable named folder anywhere else in your code. You can "correct" (VBA is not case sensitive) the case it uses by typing Dim Folder on an empty line, move the cursor off the line, then delete it. It will use the case of the last variable you declared, regardless of scope.
Note that having objects declared as 'folder' makes no difference to the compiler, but in general I avoid using the names of types as variable for this very reason.
In normal mode without any other code in your Excel you after you type Dim f As FolDer it will never change.
Now add a new line like this:
Dim folder As Object
Then, It will change that line to:
Dim f As folder
Now, Delete all codes! now add this code again: Dim f As FolDer you will see it changes to Dim f As folder !!!
This will be repeated until you close the Excel.
This is a cause of caching variable names in VBE.