Results 1 to 2 of 2
Thread: error 5151
2013-07-30, 17:40 #1
- Join Date
- Jul 2013
- Thanked 0 Times in 0 Posts
writing a macro in Word 2003 that opens a list of files. When it opens list of files if user doesn't choose a file it gives a error 5151 Word was unable to read the document it must be corrupt etc. Is there some code I can put in when user doesn't choose a file to open that will keep this error from showing up.
2013-07-30, 18:25 #2
- Join Date
- Jan 2001
- Melbourne, Victoria, Australia
- Thanked 271 Times in 248 Posts
Can you show us the line that is calling the file chooser?
This can be simple or as fully featured as you like depending on whether you want to allow multi-select or filter on file types or change the start directory etc. The following was modified slightly after sourcing from http://www.microsofttraining.net/pos...le-dialog.html
Sub PickAFile() Dim varItem As Variant Dim strPath As String Dim filePicker As FileDialog Set filePicker = Application.FileDialog(msoFileDialogFilePicker) With filePicker 'setup File Dialog .AllowMultiSelect = False .ButtonName = "Select" .InitialView = msoFileDialogViewList .Title = "Select File" .InitialFileName = "C:\FolderName" With .Filters 'add filter for all files' .Clear .Add "All Files", "*.*" End With .FilterIndex = 1 .Show 'display file dialog box' End With If filePicker.SelectedItems.Count > 0 Then Dim selectedFile As String selectedFile = filePicker.SelectedItems(1) MsgBox "You picked: " & selectedFile Else MsgBox "Nothing selected" End If End SubAndrew Lockton, Chrysalis Design, Melbourne Australia