I have a concerned regarding importing/browsing files using VBA. It is working as expected, I can import or select files from the folder. However, there is a dialog box popped out every time I processed the selected file (see image below). Is it possible to remove this dialog box? Or is it because the file is too large to handle?
Here is my code for selecting the file from the folder:
Dim FSO As Object
Dim FD As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set FD = Application.FileDialog(msoFileDialogFilePicker)
'select/browse file in folder path
If FD.Show = -1 Then
Filename = FD.SelectedItems(1)
Filename = FSO.getfile(Filename)
End If
txtBoxOld.Text = Filename
Thank you.
Setting Application.DisplayAlerts = True is a bit like just sticking your fingers in your ears and saying I CAN'T HEAR YOU. The problem is that it will mute every alert, not just the one you're concerned about.
If you want to purge the clipboard before the end of the subroutine, use Application.CutCopyMode=False and you won't get the error.
Related
My code is simple.
I have copied it off of the VBA example site: https://learn.microsoft.com/en-us/office/vba/api/office.filedialog
Additionally, every other variation I can find online has the same problem.
All I am attempting is to open a File Dialogbox (Similar to a file explorer) where a user can select a folder or files.
However, I carry on getting this error:
It then highlights this line of code:
Additionally, I have added references to Microsoft Office 16.0 Object Library, and everything I can think of:
How do I fix this or get this to Run?
Thank You
Here is the Code:
Sub Main()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "The path is: " & vrtSelectedItem
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
Disclaimer: I'm sure there is a better/more correct way to do this but it works for me in VBA for SolidWorks. I am not a real programmer. Just trying to help because I know how few resources there are on VBA programming in SW.
All I really did was add the Excel Object library and change Application to Excel.Application.
Despite what GSerg said, there can be an Excel.Application if you add the Excel object library as a reference.
Despite macropods condescension, you are not stuck with what SW provides or the generic file browser and you don't need to re-install Office or SW. The generic file browser doesn't show your Quick Access items. SolidWorks doesn't run on Mac OS. His "answer" does not work in SW VBA.
Here is my working function (started with code from here):
Function GetFolder(Title As String)
Dim folder As FileDialog
Dim selected_folder As String
Set folder = Excel.Application.FileDialog(msoFileDialogFolderPicker)
With folder
.AllowMultiSelect = False
.ButtonName = "Ok"
.Filters.Clear
.InitialFileName = Excel.Application.DefaultFilePath
.Title = Title
If .Show <> -1 Then GoTo NextCode
selected_folder = .SelectedItems(1)
End With
NextCode:
Debug.Print selected_folder
GetFolder = selected_folder
Set folder = Nothing
End Function
I added the Excel & Office Object Libraries to the default VBA SW:
If you have a way to do this that shows the Quick Access items without having to use the Excel Object Library I'm very interested!
I assume you're using a Windows system, since Application.FileDialog is not available/fully supported on Macs.
I suggest you start over, without adding a plethora of irrelevant references. The following works just fine without adding any to the default references:
Sub Demo()
Dim fd As FileDialog, vrtSelectedItem As Variant
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
MsgBox "The path is: " & vrtSelectedItem
Next
End If
End With
Set fd = Nothing
End Sub
If the code still doesn't work for you, a repair/reinstall of Office or your SolidWorks installation is most likely called for.
For code that works on Macs and PCs, see: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-msoffice_custom-mso_365hp/showing-dialogs-word-for-mac-vba/513ea974-378d-4ebe-95c3-a0221a9287ff
I'm testing out a user app that they will be accessing remotely. There is file dialog functionality that I've included using the FileDialog object. I'd like for them to initially open up their own local files using the property .InitialFileName, but I'm having trouble finding the correct path for the initial open. Here's what I have so far:
Dim fileDialog As Object 'Office.FileDialog
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = "C on " & Environ("CLIENTNAME")
....
When I do open up the dialog without setting the initial file path, there is a location that the user can access that is ""C on PCName", so I thought that I could just set the string to be the same (in above code), but it doesn't work if I set that as the path. Environ("CLIENTNAME") does get the PCName of the user, so I think I am close to a solution.
Thanks for any suggestions.
There is a list of potentially useful environment variables here
This might be a starting point
Option Explicit
Sub x()
Dim fileDialog As Object 'Office.FileDialog
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)
With fileDialog
.InitialFileName = Environ("HOMEPATH")
.Show
End With
End Sub
Never mind. I do believe I've come across a solution:
FileDialog.InitialFileName = "\tsclient\c"
I have written the below code via a user form with buttons. On the form, I have created three (3) separate command buttons that are to save different file paths to open existing excel workbooks. The idea is to have vba automatically open/close workbooks in a completely automated fashion.
Dim SCDMMRReport As FileDialog
Dim ActionClicked As Boolean
Set SCDMMRReport = Application.FileDialog(msoFileDialogFilePicker)
SCDMMRReport.InitialFileName = "J:\INS\CAS\G22 PRT Report"
SCDMMRReport.AllowMultiSelect = False
ActionClicked = SCDMMRReport.Show
If ActionClicked Then
SCDMMRReportCommandButton.Visible = False
SCDMMRReportLabel.BackColor = rgbLightGreen
SCDMMRCheckBox = True
Else
MsgBox ("No file selected!")
SCDMMRReportCommandButton.BackColor = rgbPink
SCDMMRReportLabel.BackColor = rgbPink
End If
Everything appears as it runs excellent, however when it comes time to call on this code later in the script (when I hit the execute command button), the file paths are not saved. Any advice is greatly appreciated!
Thanks,
Derek
I am using the below code, to open the Excel file from SharePoint.
I am Little confused how, I should code, for the accessing the file from an Folder in SharePoint.
The Excel file, i Need is stored in ; SharePoint>> Document >> Test >> Data >>July
and the link to my SharePoint is like this, https://forum/content/008200/default.aspx?RootFolder=%2Fcontent%2F00008200%20Documents%2DTest%2F0001%20%29&FolderCTID=0x01200083BC38D90EC5674491B520CC48282737&View={00112127-0FE6-44A4-A5FB-86BC6C4E835B}&InitialTabId=Ribbon%2EDocument&VisibilityContext=WSSTabPersistence
Could someone help me, How i should code, to acess an Excel file from a Folder in SharePoint.
I have attached a Piece of code, which i reffered it from the Forum. and would be helpful, if you could comment the lines, if you can suggest me a code.
Sub Share()
Dim SWB As Workbook
Dim WB As Variant
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "https://forum/Content/008200/default.aspx" & "\"
.AllowMultiSelect = False
.show
For Each WB In .SelectedItems
Set SWB = Workbooks.Open(WB)
Next
End With
If SWB Is Nothing Then Exit Sub
End Sub
i don't know , how can you with vb but i assume your file in Document Library if it is you can use this code to get document.
SPFile myExcelFile = SPContext.Current.Web.GetFile("/Documents/Test/Data/July/excelFile.xslx");
Stream excelFileStream = myExcelFile.OpenBinaryStream();
The aim of this project is to:
Load template from network personal drive (currently working)
Open Dialog Window, using Word (as that loads quicker for me), set to a different network folder (function achieved - just)
Choose and attach user chosen file (prefered option for multi-file selection but that was to complex and thus beyond the time I can afford to dedicate to the project)
Using previous filepath, build and apply the subject line based on the file name. (currently working)
The macro is called by a button in the Quick Access or Main Tool Bars.
The problems:
when the "Browse" window appears, it does so behind outlook/hidden UNLESS the macro code has been viewed in the editor at some time since Outlook was opened; every fix either show the entire Word program or seems to have no effect.
when you choose a file in the "Browse" and then click "OK", a second "Browse" window opens where you have to choose a file again and this time click "Open"; this second file is then the one that is attached to the email.<--FIXED
System Details (even though not all will be of importance)
Windows 7 64bit
Office Professional 2010
The Code
Sub New_Orders_Email()
Dim NewMail As Outlook.MailItem
Dim otherObject As Word.Application
Dim fd As Office.FileDialog
Dim fileaddress As String
Dim filename As String
On Error GoTo Final
'Set template to use
Set NewMail = Application.CreateItemFromTemplate("P:\Office Templates\Orders SCI - 1617.oft")
'Set to use Word for Attach File Dialog
Set otherObject = New Word.Application
otherObject.Visible = False
Set fd = otherObject.Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.InitialFileName = "R:\Science\Technician Documents\Budgets & Orders\2016 - 2017\Orders\"
.Show
End With
fd.Show
fileaddress = fd.SelectedItems(1)
'Aquire File Name in correct form for Subject Line
Dim MidStart As Long
MidStart = InStrRev(fileaddress, "\") + 16
Dim MidEnd As Long
MidEnd = InStrRev(fileaddress, ".")
filename = Mid(fileaddress, MidStart, MidEnd - MidStart)
'Load template, attach single file and apply correct Subject
NewMail.Display
NewMail.Attachments.Add (fileaddress)
NewMail.Subject = "Order No: " + filename
otherObject.Quit
Set otherObject = Nothing
Final:
End Sub
Your file Dialog is probably not shown, because of the line "otherObject.Visible = False"
Number (2) might be solved if you delete one of your "fd.Show". Looks like you call it twice...