locked
Message Box to alert user not to use nonallowed characters in filename. RRS feed

  • Question

  • How do I code a message box to promp user not to use any of these " \ / : * ? " <> " characters in the filename. I tried a simple msgbox "yada yada yada and the quoted these in a string" but all that displayed in message box was the word True.

    Using Word VBA (Office 2010).

    Thanks,

    Chevyman57

    Wednesday, July 4, 2012 5:56 AM

Answers

  • Hello Chevyman,

    The following is an example from my library. It uses an InputBox to get the file name and loops with an error message if invalid characters used.

    The User can terminate with the Cancel button on the InputBox.

    Hope you can adapt it to your needs.

    The double test in the If statement is because the Right square bracket cannot be tested for a match in the other string that is enclosed in square brackets. The left square bracket, asterisk and question mark are enclosed in standard brackets in the test string.  See "Like" in Help for more info on this test.

    Edited since original post so it works in Word. My apologies for the error in that the original code was for Excel.

    Sub EnterFilerName()
         Dim strTitle As String
         Dim strPrompt As String
         Dim strFileName As Variant  'Use Variant so Cancel can be used
         Dim i As Long
         Dim bolInValid As Boolean
        
        strTitle = "Enter File Name"
         strPrompt = "Enter the required file name."
        
        Do
             strFileName = InputBox(Title:=strTitle, Prompt:=strPrompt)
            
            If strFileName = False Then     'When User clicks Cancel
                 MsgBox "User Cancelled. Processing terminated."
                 Exit Sub
             End If
            
            bolInValid = False
             For i = 1 To Len(strFileName)
                 If Mid(strFileName, i, 1) Like "[([)\/:(*)(?)<>]" Or _
                            Mid(strFileName, i, 1) Like "]" Then
                     strPrompt = "Error!  " & Mid(strFileName, i, 1) _
                                 & "  is invalid file name character " _
                                 & vbCrLf & "Re-enter valid file name."
                     bolInValid = True   'Invalid character encountered
                     Exit For
                 End If
             Next i
        
        Loop While bolInValid
        
        MsgBox strFileName & " is a valid string."
     End Sub


    Regards, OssieMac


    • Edited by OssieMac Wednesday, July 4, 2012 8:31 AM
    • Marked as answer by Chevyman57 Thursday, July 5, 2012 9:26 PM
    Wednesday, July 4, 2012 6:45 AM

All replies

  • Hello Chevyman,

    The following is an example from my library. It uses an InputBox to get the file name and loops with an error message if invalid characters used.

    The User can terminate with the Cancel button on the InputBox.

    Hope you can adapt it to your needs.

    The double test in the If statement is because the Right square bracket cannot be tested for a match in the other string that is enclosed in square brackets. The left square bracket, asterisk and question mark are enclosed in standard brackets in the test string.  See "Like" in Help for more info on this test.

    Edited since original post so it works in Word. My apologies for the error in that the original code was for Excel.

    Sub EnterFilerName()
         Dim strTitle As String
         Dim strPrompt As String
         Dim strFileName As Variant  'Use Variant so Cancel can be used
         Dim i As Long
         Dim bolInValid As Boolean
        
        strTitle = "Enter File Name"
         strPrompt = "Enter the required file name."
        
        Do
             strFileName = InputBox(Title:=strTitle, Prompt:=strPrompt)
            
            If strFileName = False Then     'When User clicks Cancel
                 MsgBox "User Cancelled. Processing terminated."
                 Exit Sub
             End If
            
            bolInValid = False
             For i = 1 To Len(strFileName)
                 If Mid(strFileName, i, 1) Like "[([)\/:(*)(?)<>]" Or _
                            Mid(strFileName, i, 1) Like "]" Then
                     strPrompt = "Error!  " & Mid(strFileName, i, 1) _
                                 & "  is invalid file name character " _
                                 & vbCrLf & "Re-enter valid file name."
                     bolInValid = True   'Invalid character encountered
                     Exit For
                 End If
             Next i
        
        Loop While bolInValid
        
        MsgBox strFileName & " is a valid string."
     End Sub


    Regards, OssieMac


    • Edited by OssieMac Wednesday, July 4, 2012 8:31 AM
    • Marked as answer by Chevyman57 Thursday, July 5, 2012 9:26 PM
    Wednesday, July 4, 2012 6:45 AM
  • Sub SaveMyWord()
                            
            With Application.FileDialog(msoFileDialogSaveAs)
                If .Show Then
                    .Execute
                Else
                    Exit Sub
                End If
            End With
    End Sub

    In Any Open Word Doc->Press Alt+F11->Select Normal Project->Insert Module->Paste the baove.

    The macro will run from any document and help you saving.

    ---------------------------------------------------------------------------------------------

    Please do not forget to click “Vote as Helpful” if any post helps you and mark as Answer if it solves the issue.

    Wednesday, July 4, 2012 7:12 AM
    Answerer
  • I ended up with this embedded in my code. Works Great. Thanks.

    Sub CheckFileName()

         Dim strFileName As String
         Dim i As Long
         Dim bolInValid As Boolean
        Do
        strFileName = ActiveDocument.TextBox1
             If strFileName = "Please Type Project Title Here" Then
                 MsgBox "Please Enter a Valid Project Title"
                 FileNameOK = False
                 Exit Sub
             End If
             If strFileName = "" Then
                 MsgBox "Please Enter a Valid Project Title"
                 FileNameOK = False
                 Exit Sub
             End If
              If ActiveDocument.TextBox3 = "1 thru 10" Then
                 MsgBox "Please Pick a Priority Number"
                 FileNameOK = False
                 Exit Sub
             End If
             If ActiveDocument.TextBox4 = "XX0000" Then
                 MsgBox "Please Enter a Valid Tracking Number"
                 FileNameOK = False
                 Exit Sub
             End If
             If ActiveDocument.ComboBox1 = "" Then
                 MsgBox "Please Enter Your Name or Pick From List"
                 FileNameOK = False
                 Exit Sub
             End If
             If ActiveDocument.ComboBox2 = "" Then
                 MsgBox "Please Enter Your Department or Pick From List"
                 FileNameOK = False
                 Exit Sub
             End If
            
        bolInValid = False
             For i = 1 To Len(strFileName)
                 If Mid(strFileName, i, 1) Like "[([)\/:""(.)(*)(?)<>]" Or _
                            Mid(strFileName, i, 1) Like "]" Then
                                MsgBox "Error! " & Mid(strFileName, i, 1) _
                                & "  Is An Invalid FileName Character " _
                                & vbCrLf & "Please Correct As Required and Re-Save"
                            bolInValid = True
                            FileNameOK = False
                            Exit Sub
             Exit For
                 End If
                 If bolInValid = False Then
                     FileNameOK = True
                     End If
             Next i
        Loop While bolInValid
      
    End Sub

    Thursday, July 5, 2012 9:29 PM