sure, first add references to Microsoft.Office.Core (Microsoft Office 14.0 Object Library) and Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).
Also you need to incorporate the packages as well.
Ans then try the below code afterwards, I have to follow some tricks (using MS excel application) to open the dialog though the code is not opening the excel but the folder selection window.
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Core.FileDialog fileDialog = app.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFolderPicker);
fileDialog.InitialFileName = "c:\\Temp\\"; //something you want
int nres = fileDialog.Show();
if (nres == -1)
{
Microsoft.Office.Core.FileDialogSelectedItems selectedItems = fileDialog.SelectedItems;
string[] selectedFolders = selectedItems.Cast<string>().ToArray();
if (selectedFolders.Length > 0)
{
string selectedFolder = selectedFolders[0];
pdfFolderPath=selectedFolder;
}
}
Also create an argument and incorporate it in the invoke code.
Working code for me vb.net
(Studio 21.10.4, System.Activities 22.10.4, Windows-Legacy project)
Invoke code activity:
Dim xl As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim fd As Microsoft.Office.Core.FileDialog
fd = xl.FileDialog(MsoFileDialogType.msoFileDialogFolderPicker)
fd.Title="Select folder you want to process"
fd.AllowMultiSelect=False
fd.ButtonName="Choose folder"
fd.Show()
result = fd.SelectedItems(0).ToString
fd = Nothing
xl = Nothing
(the Invoke code starts with next line)
’ *****************************************************
’ Windows/Legacy - Dont Forget to Import the System.Windows.Forms.OpenFileDialog
’
’ Windows - Dont Forget to Import the Microsoft.Win32
’ *****************************************************
’ For Windows/Legacy
’ Dim OpenFileDialog As New System.Windows.Forms.OpenFileDialog
Dim OpenFileDialog As New Microsoft.Win32.OpenFileDialog
Outstanding! Which simple activity allows you to specify a default directory?
Forcing the bot user, when using the File Select Dialog box to perform all of the clicks to move to the desired destination folder locn (when we have it stored as a setting) was not acceptable.
I checked and Default Directory is not a property for the Ui’s FileSelect activity.
Yes the File Select dialog has limitations. Not everyone needs that. You could pass a default value to the text object on the form, and use that with some very simple UI automation to make the dialog automatically go to that folder.
What would be excellent would be to create a custom “browse for file” activity that does incorporate your code and then you could have a default folder property.
In your code, what’s the format expected by OpenFileDialog.Filter?
Ok this is awesome. I took your code, added a few additional properties I think would be useful, and made a custom activity out of it. Here are the files. Thanks for leading me down this path!
You can just add those to any existing library, or create a new library and copy them in. I used UiPath.System.Activities 23.4.3 but I would imagine any recent version will work.
Good trick. But just wondering, how do you make it topmost? At least in Debug, if I have a bunch of opened windows, this dialog will be hidden down below somewhere.
The same thing happens with the official activity. It appears there is no way to control it, including with the vb.net script, because the Win32.OpenFileDialog simply doesn’t have any relevant properties nor methods to do this.
Yes that’s where I found them. There are a ton of them, but some of them didn’t seem useful in an automation context. I also considered adding an option to make it a “select file” or “save file” dialog but I couldn’t see the automation value of a “save file” dialog.
Also, it’s worth noting that their documentation says RestoreFolder is not implemented, so I took that out.