Show files only (not folders) in a Office.FileDialog window? - vba

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),")

Related

Save file without specifying Drive Location

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

Default Filename for GetOpenFilename

I'm looking to set a default filename in GetOpenFilename. I'm using GetOpenFilename because it was in an example for using UNC paths (which I require) and from what I've read you cannot do that with ChDir or ChDrive using FileDialog. Is there anything that exists that will allow presetting of the filename and work with UNC paths?
I've tried sticking the filename into the FileFilter section of GetOpenFilename and that does not work. From what I have found it looks like this may not be possible but my limited knowledge of VBA may be the issue as well.
I'm stuck with using UNC because the data is located on a network and not everyone maps it to the same drive or even maps it at all.
In Excel if you look in Application.Dialogs() you'll find a long list of predefined dialog boxes used in Excel that you can call upon. GetOpenFilename is the same situation, because it is predefined, the customization options are minimal.
To use the generic file dialog box (i.e. not custom within Excel) you can use Application.FileDialog(msoFileDialogOpen), this will allow for further customisation including the initial filename text.
Public Sub Sample()
Dim Dlg As FileDialog
Set Dlg = Application.FileDialog(msoFileDialogOpen)
Dlg.InitialFileName = "Sample"
Dlg.Show
Set Dlg = Nothing
End Sub

Finding paths to files using vba

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.

How do I specify the file path in vba code in powerpoint 2011 for mac?

I want to access an image file in my VBA code for PowerPoint 2011 for Mac.
I have used both "/" and ":" as separator but it didn't work.
It works only when I save both .pptm file and the image file in the same folder and specify the relative path for the image file as below:
Dim strPath As String
strPath = "image.png"
But if I try saving the file at a different location than the .pptm file and provide the full path for the StrPath as:
strPath = "Users:Me:Desktop:image.png"
it throws a runtime error saying "the specified file wasn't found".
I have kept in mind the case sensitivity in case of Mac but nothing seems to be working for me.
Can anyone please help me in finding the possible workaround so that I can save the .pptm file and the image file at different locations on Mac?
Try:
Presentations.Open "Macintosh HD:Users:Me:Desktop:filename.ext"
Substitute appropriate strings for the name of the HD, your user name and the file.

Delete PDF files only from folder using Applescript on MAC

I need to delete the PDF files only from a folder on a network drive. There are other file types in the folder I can not touch. I am assuming I have to choose or identify the PF files first and then move them to the trash or delete them.
What I have so far:
tell application "Finder"
set theFolder to "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB"
set destFolder to "Server/JOBS/TRANSFER_TRASH/"
set thePDFs to every file of theFolder whose name extension is "pdf"
move thePDFs to destFolder
end tell
What I get for an error:
error "Can’t get every file of \"Macintosh
HD:Users:Kathlene:Desktop:ABC123_JOB:\"." number -1728 from every file
of "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB"
Try:
tell application "Finder" to delete (files of folder "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB" whose name extension is "pdf")