Results 1 to 2 of 2

Thread: error 5151

  1. #1
    New Lounger
    Join Date
    Jul 2013
    Thanked 0 Times in 0 Posts

    Post error 5151

    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.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Melbourne, Victoria, Australia
    Thanked 295 Times in 267 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

    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'
          .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
        MsgBox "Nothing selected"
      End If
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts