Is there any method to check type of file before open it with wdDialogFileOpen?












-2















I use wdDialogFileOpen to let user open file. I want to allow user to only open .docx file. Is there any method to check type of file before open it with wdDialogFileOpen (after user choose it with wdDialogFileOpen) ?
I use following code:



With Dialogs(wdDialogFileOpen)
.Name = "."
.Show
End With










share|improve this question




















  • 1





    Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

    – Variatus
    Jan 2 at 4:58











  • I have edited my question to include the code.

    – fairy elf
    Jan 3 at 1:22
















-2















I use wdDialogFileOpen to let user open file. I want to allow user to only open .docx file. Is there any method to check type of file before open it with wdDialogFileOpen (after user choose it with wdDialogFileOpen) ?
I use following code:



With Dialogs(wdDialogFileOpen)
.Name = "."
.Show
End With










share|improve this question




















  • 1





    Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

    – Variatus
    Jan 2 at 4:58











  • I have edited my question to include the code.

    – fairy elf
    Jan 3 at 1:22














-2












-2








-2








I use wdDialogFileOpen to let user open file. I want to allow user to only open .docx file. Is there any method to check type of file before open it with wdDialogFileOpen (after user choose it with wdDialogFileOpen) ?
I use following code:



With Dialogs(wdDialogFileOpen)
.Name = "."
.Show
End With










share|improve this question
















I use wdDialogFileOpen to let user open file. I want to allow user to only open .docx file. Is there any method to check type of file before open it with wdDialogFileOpen (after user choose it with wdDialogFileOpen) ?
I use following code:



With Dialogs(wdDialogFileOpen)
.Name = "."
.Show
End With







vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 1:22







fairy elf

















asked Jan 2 at 1:38









fairy elffairy elf

11




11








  • 1





    Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

    – Variatus
    Jan 2 at 4:58











  • I have edited my question to include the code.

    – fairy elf
    Jan 3 at 1:22














  • 1





    Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

    – Variatus
    Jan 2 at 4:58











  • I have edited my question to include the code.

    – fairy elf
    Jan 3 at 1:22








1




1





Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

– Variatus
Jan 2 at 4:58





Yes. There are filters you can apply to the dialog box if it is called using VBA. Amend your question to include the code you use to call the dialog box.

– Variatus
Jan 2 at 4:58













I have edited my question to include the code.

– fairy elf
Jan 3 at 1:22





I have edited my question to include the code.

– fairy elf
Jan 3 at 1:22












2 Answers
2






active

oldest

votes


















0














For example:



With Dialogs(wdDialogFileOpen)
.Format = "*.docx"
If .Display = True Then
If InStrRev(.Name, ".docx") > 0 Then
.Execute
End If
End If
End With





share|improve this answer































    0














    I wouldn't use the FileOpen dialog at all. Consider a FilePicker: The user selects the file and then you decide if you want to open it. Here is some code to play with.



    Private Sub TestFileOpenName()

    Dim Fn As String
    Dim Sp() As String

    ' the Flt argument = 1 which results in Word documents being filtered
    Fn = FileOpenName("Select a file", 1, "C:My Dopcuments")

    ' the Flt argument = "Word documents|*.doc*" which also results in
    ' Word documents being filtered (modify filter as required)
    ' Fn = FileOpenName("Select a file", "Word documents|*.doc*", "D:My Dopcuments")

    ' the Flt argument = 1 or 2 which results in Word documents being filtered
    ' but type drop-down allows changing to Excel.
    ' Specify "2||1" to make Excel the default and Word the alternative
    ' Fn = FileOpenName("Select a file", "1||2", "C:My Dopcuments")

    If Len(Fn) Then
    MsgBox "The selected file is" & vbCr & Fn
    Sp = Split(Fn, ".")
    If InStr(1, Sp(UBound(Sp)), "doc", vbTextCompare) = 1 Then
    MsgBox "I will now open the document"
    Else
    MsgBox "Please select a Word document." & vbCr & _
    "Sorry, I can't proceed."
    End If
    Else
    MsgBox "No file was selected"
    End If
    End Sub

    Function FileOpenName(ByVal Title As String, _
    Optional ByVal Flt As Variant = 0, _
    Optional ByVal Pn As String) As String
    ' SSY 050 ++ 14 Dec 2018

    ' ==================================================
    ' Parameters:
    ' Title = Form's title
    ' Flt = Specify filters by ID or string specs
    ' separated by || (= 2 x Chr(124))
    ' in sequence of position assignment.
    ' Default = no filter [=All files]
    ' Pn = Initial path: [=Last used]
    ' ==================================================
    ' Note: The ButtonName is "Open" by default. Another setting
    ' doesn't take effect until a file has been selected.
    ' ==================================================

    Const FltDesc As Long = 0, FltExt As Long = 1

    Dim Fod As FileDialog ' File Open Dialog
    Dim Fts() As String ' all filters
    Dim Sp() As String ' split filter
    Dim i As Long

    ' ==================================================

    Fts = Split(Flt, "||")
    ReDim Sp(3)
    Sp(1) = "Word documents|*.doc*"
    Sp(2) = "Excel workbooks|*.xls*"
    Sp(3) = "Image file|*.png, *.tif"
    For i = 0 To UBound(Fts)
    If IsNumeric(Fts(i)) Then Fts(i) = Sp(Fts(i))
    Next i

    Set Fod = Application.FileDialog(msoFileDialogFilePicker)
    With Fod
    .Filters.Clear
    For i = 0 To UBound(Fts)
    If Len(Fts(i)) Then
    Sp = Split(Fts(i), "|")
    .Filters.Add Sp(FltDesc), Sp(FltExt), i + 1
    .FilterIndex = 1
    End If
    Next i
    .Title = Title
    .AllowMultiSelect = False
    .InitialFileName = Pn
    If .Show Then FileOpenName = .SelectedItems(1)
    End With
    End Function





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000322%2fis-there-any-method-to-check-type-of-file-before-open-it-with-wddialogfileopen%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      For example:



      With Dialogs(wdDialogFileOpen)
      .Format = "*.docx"
      If .Display = True Then
      If InStrRev(.Name, ".docx") > 0 Then
      .Execute
      End If
      End If
      End With





      share|improve this answer




























        0














        For example:



        With Dialogs(wdDialogFileOpen)
        .Format = "*.docx"
        If .Display = True Then
        If InStrRev(.Name, ".docx") > 0 Then
        .Execute
        End If
        End If
        End With





        share|improve this answer


























          0












          0








          0







          For example:



          With Dialogs(wdDialogFileOpen)
          .Format = "*.docx"
          If .Display = True Then
          If InStrRev(.Name, ".docx") > 0 Then
          .Execute
          End If
          End If
          End With





          share|improve this answer













          For example:



          With Dialogs(wdDialogFileOpen)
          .Format = "*.docx"
          If .Display = True Then
          If InStrRev(.Name, ".docx") > 0 Then
          .Execute
          End If
          End If
          End With






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 1:37









          macropodmacropod

          2,8242311




          2,8242311

























              0














              I wouldn't use the FileOpen dialog at all. Consider a FilePicker: The user selects the file and then you decide if you want to open it. Here is some code to play with.



              Private Sub TestFileOpenName()

              Dim Fn As String
              Dim Sp() As String

              ' the Flt argument = 1 which results in Word documents being filtered
              Fn = FileOpenName("Select a file", 1, "C:My Dopcuments")

              ' the Flt argument = "Word documents|*.doc*" which also results in
              ' Word documents being filtered (modify filter as required)
              ' Fn = FileOpenName("Select a file", "Word documents|*.doc*", "D:My Dopcuments")

              ' the Flt argument = 1 or 2 which results in Word documents being filtered
              ' but type drop-down allows changing to Excel.
              ' Specify "2||1" to make Excel the default and Word the alternative
              ' Fn = FileOpenName("Select a file", "1||2", "C:My Dopcuments")

              If Len(Fn) Then
              MsgBox "The selected file is" & vbCr & Fn
              Sp = Split(Fn, ".")
              If InStr(1, Sp(UBound(Sp)), "doc", vbTextCompare) = 1 Then
              MsgBox "I will now open the document"
              Else
              MsgBox "Please select a Word document." & vbCr & _
              "Sorry, I can't proceed."
              End If
              Else
              MsgBox "No file was selected"
              End If
              End Sub

              Function FileOpenName(ByVal Title As String, _
              Optional ByVal Flt As Variant = 0, _
              Optional ByVal Pn As String) As String
              ' SSY 050 ++ 14 Dec 2018

              ' ==================================================
              ' Parameters:
              ' Title = Form's title
              ' Flt = Specify filters by ID or string specs
              ' separated by || (= 2 x Chr(124))
              ' in sequence of position assignment.
              ' Default = no filter [=All files]
              ' Pn = Initial path: [=Last used]
              ' ==================================================
              ' Note: The ButtonName is "Open" by default. Another setting
              ' doesn't take effect until a file has been selected.
              ' ==================================================

              Const FltDesc As Long = 0, FltExt As Long = 1

              Dim Fod As FileDialog ' File Open Dialog
              Dim Fts() As String ' all filters
              Dim Sp() As String ' split filter
              Dim i As Long

              ' ==================================================

              Fts = Split(Flt, "||")
              ReDim Sp(3)
              Sp(1) = "Word documents|*.doc*"
              Sp(2) = "Excel workbooks|*.xls*"
              Sp(3) = "Image file|*.png, *.tif"
              For i = 0 To UBound(Fts)
              If IsNumeric(Fts(i)) Then Fts(i) = Sp(Fts(i))
              Next i

              Set Fod = Application.FileDialog(msoFileDialogFilePicker)
              With Fod
              .Filters.Clear
              For i = 0 To UBound(Fts)
              If Len(Fts(i)) Then
              Sp = Split(Fts(i), "|")
              .Filters.Add Sp(FltDesc), Sp(FltExt), i + 1
              .FilterIndex = 1
              End If
              Next i
              .Title = Title
              .AllowMultiSelect = False
              .InitialFileName = Pn
              If .Show Then FileOpenName = .SelectedItems(1)
              End With
              End Function





              share|improve this answer




























                0














                I wouldn't use the FileOpen dialog at all. Consider a FilePicker: The user selects the file and then you decide if you want to open it. Here is some code to play with.



                Private Sub TestFileOpenName()

                Dim Fn As String
                Dim Sp() As String

                ' the Flt argument = 1 which results in Word documents being filtered
                Fn = FileOpenName("Select a file", 1, "C:My Dopcuments")

                ' the Flt argument = "Word documents|*.doc*" which also results in
                ' Word documents being filtered (modify filter as required)
                ' Fn = FileOpenName("Select a file", "Word documents|*.doc*", "D:My Dopcuments")

                ' the Flt argument = 1 or 2 which results in Word documents being filtered
                ' but type drop-down allows changing to Excel.
                ' Specify "2||1" to make Excel the default and Word the alternative
                ' Fn = FileOpenName("Select a file", "1||2", "C:My Dopcuments")

                If Len(Fn) Then
                MsgBox "The selected file is" & vbCr & Fn
                Sp = Split(Fn, ".")
                If InStr(1, Sp(UBound(Sp)), "doc", vbTextCompare) = 1 Then
                MsgBox "I will now open the document"
                Else
                MsgBox "Please select a Word document." & vbCr & _
                "Sorry, I can't proceed."
                End If
                Else
                MsgBox "No file was selected"
                End If
                End Sub

                Function FileOpenName(ByVal Title As String, _
                Optional ByVal Flt As Variant = 0, _
                Optional ByVal Pn As String) As String
                ' SSY 050 ++ 14 Dec 2018

                ' ==================================================
                ' Parameters:
                ' Title = Form's title
                ' Flt = Specify filters by ID or string specs
                ' separated by || (= 2 x Chr(124))
                ' in sequence of position assignment.
                ' Default = no filter [=All files]
                ' Pn = Initial path: [=Last used]
                ' ==================================================
                ' Note: The ButtonName is "Open" by default. Another setting
                ' doesn't take effect until a file has been selected.
                ' ==================================================

                Const FltDesc As Long = 0, FltExt As Long = 1

                Dim Fod As FileDialog ' File Open Dialog
                Dim Fts() As String ' all filters
                Dim Sp() As String ' split filter
                Dim i As Long

                ' ==================================================

                Fts = Split(Flt, "||")
                ReDim Sp(3)
                Sp(1) = "Word documents|*.doc*"
                Sp(2) = "Excel workbooks|*.xls*"
                Sp(3) = "Image file|*.png, *.tif"
                For i = 0 To UBound(Fts)
                If IsNumeric(Fts(i)) Then Fts(i) = Sp(Fts(i))
                Next i

                Set Fod = Application.FileDialog(msoFileDialogFilePicker)
                With Fod
                .Filters.Clear
                For i = 0 To UBound(Fts)
                If Len(Fts(i)) Then
                Sp = Split(Fts(i), "|")
                .Filters.Add Sp(FltDesc), Sp(FltExt), i + 1
                .FilterIndex = 1
                End If
                Next i
                .Title = Title
                .AllowMultiSelect = False
                .InitialFileName = Pn
                If .Show Then FileOpenName = .SelectedItems(1)
                End With
                End Function





                share|improve this answer


























                  0












                  0








                  0







                  I wouldn't use the FileOpen dialog at all. Consider a FilePicker: The user selects the file and then you decide if you want to open it. Here is some code to play with.



                  Private Sub TestFileOpenName()

                  Dim Fn As String
                  Dim Sp() As String

                  ' the Flt argument = 1 which results in Word documents being filtered
                  Fn = FileOpenName("Select a file", 1, "C:My Dopcuments")

                  ' the Flt argument = "Word documents|*.doc*" which also results in
                  ' Word documents being filtered (modify filter as required)
                  ' Fn = FileOpenName("Select a file", "Word documents|*.doc*", "D:My Dopcuments")

                  ' the Flt argument = 1 or 2 which results in Word documents being filtered
                  ' but type drop-down allows changing to Excel.
                  ' Specify "2||1" to make Excel the default and Word the alternative
                  ' Fn = FileOpenName("Select a file", "1||2", "C:My Dopcuments")

                  If Len(Fn) Then
                  MsgBox "The selected file is" & vbCr & Fn
                  Sp = Split(Fn, ".")
                  If InStr(1, Sp(UBound(Sp)), "doc", vbTextCompare) = 1 Then
                  MsgBox "I will now open the document"
                  Else
                  MsgBox "Please select a Word document." & vbCr & _
                  "Sorry, I can't proceed."
                  End If
                  Else
                  MsgBox "No file was selected"
                  End If
                  End Sub

                  Function FileOpenName(ByVal Title As String, _
                  Optional ByVal Flt As Variant = 0, _
                  Optional ByVal Pn As String) As String
                  ' SSY 050 ++ 14 Dec 2018

                  ' ==================================================
                  ' Parameters:
                  ' Title = Form's title
                  ' Flt = Specify filters by ID or string specs
                  ' separated by || (= 2 x Chr(124))
                  ' in sequence of position assignment.
                  ' Default = no filter [=All files]
                  ' Pn = Initial path: [=Last used]
                  ' ==================================================
                  ' Note: The ButtonName is "Open" by default. Another setting
                  ' doesn't take effect until a file has been selected.
                  ' ==================================================

                  Const FltDesc As Long = 0, FltExt As Long = 1

                  Dim Fod As FileDialog ' File Open Dialog
                  Dim Fts() As String ' all filters
                  Dim Sp() As String ' split filter
                  Dim i As Long

                  ' ==================================================

                  Fts = Split(Flt, "||")
                  ReDim Sp(3)
                  Sp(1) = "Word documents|*.doc*"
                  Sp(2) = "Excel workbooks|*.xls*"
                  Sp(3) = "Image file|*.png, *.tif"
                  For i = 0 To UBound(Fts)
                  If IsNumeric(Fts(i)) Then Fts(i) = Sp(Fts(i))
                  Next i

                  Set Fod = Application.FileDialog(msoFileDialogFilePicker)
                  With Fod
                  .Filters.Clear
                  For i = 0 To UBound(Fts)
                  If Len(Fts(i)) Then
                  Sp = Split(Fts(i), "|")
                  .Filters.Add Sp(FltDesc), Sp(FltExt), i + 1
                  .FilterIndex = 1
                  End If
                  Next i
                  .Title = Title
                  .AllowMultiSelect = False
                  .InitialFileName = Pn
                  If .Show Then FileOpenName = .SelectedItems(1)
                  End With
                  End Function





                  share|improve this answer













                  I wouldn't use the FileOpen dialog at all. Consider a FilePicker: The user selects the file and then you decide if you want to open it. Here is some code to play with.



                  Private Sub TestFileOpenName()

                  Dim Fn As String
                  Dim Sp() As String

                  ' the Flt argument = 1 which results in Word documents being filtered
                  Fn = FileOpenName("Select a file", 1, "C:My Dopcuments")

                  ' the Flt argument = "Word documents|*.doc*" which also results in
                  ' Word documents being filtered (modify filter as required)
                  ' Fn = FileOpenName("Select a file", "Word documents|*.doc*", "D:My Dopcuments")

                  ' the Flt argument = 1 or 2 which results in Word documents being filtered
                  ' but type drop-down allows changing to Excel.
                  ' Specify "2||1" to make Excel the default and Word the alternative
                  ' Fn = FileOpenName("Select a file", "1||2", "C:My Dopcuments")

                  If Len(Fn) Then
                  MsgBox "The selected file is" & vbCr & Fn
                  Sp = Split(Fn, ".")
                  If InStr(1, Sp(UBound(Sp)), "doc", vbTextCompare) = 1 Then
                  MsgBox "I will now open the document"
                  Else
                  MsgBox "Please select a Word document." & vbCr & _
                  "Sorry, I can't proceed."
                  End If
                  Else
                  MsgBox "No file was selected"
                  End If
                  End Sub

                  Function FileOpenName(ByVal Title As String, _
                  Optional ByVal Flt As Variant = 0, _
                  Optional ByVal Pn As String) As String
                  ' SSY 050 ++ 14 Dec 2018

                  ' ==================================================
                  ' Parameters:
                  ' Title = Form's title
                  ' Flt = Specify filters by ID or string specs
                  ' separated by || (= 2 x Chr(124))
                  ' in sequence of position assignment.
                  ' Default = no filter [=All files]
                  ' Pn = Initial path: [=Last used]
                  ' ==================================================
                  ' Note: The ButtonName is "Open" by default. Another setting
                  ' doesn't take effect until a file has been selected.
                  ' ==================================================

                  Const FltDesc As Long = 0, FltExt As Long = 1

                  Dim Fod As FileDialog ' File Open Dialog
                  Dim Fts() As String ' all filters
                  Dim Sp() As String ' split filter
                  Dim i As Long

                  ' ==================================================

                  Fts = Split(Flt, "||")
                  ReDim Sp(3)
                  Sp(1) = "Word documents|*.doc*"
                  Sp(2) = "Excel workbooks|*.xls*"
                  Sp(3) = "Image file|*.png, *.tif"
                  For i = 0 To UBound(Fts)
                  If IsNumeric(Fts(i)) Then Fts(i) = Sp(Fts(i))
                  Next i

                  Set Fod = Application.FileDialog(msoFileDialogFilePicker)
                  With Fod
                  .Filters.Clear
                  For i = 0 To UBound(Fts)
                  If Len(Fts(i)) Then
                  Sp = Split(Fts(i), "|")
                  .Filters.Add Sp(FltDesc), Sp(FltExt), i + 1
                  .FilterIndex = 1
                  End If
                  Next i
                  .Title = Title
                  .AllowMultiSelect = False
                  .InitialFileName = Pn
                  If .Show Then FileOpenName = .SelectedItems(1)
                  End With
                  End Function






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 2:14









                  VariatusVariatus

                  5,7251524




                  5,7251524






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000322%2fis-there-any-method-to-check-type-of-file-before-open-it-with-wddialogfileopen%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      Npm cannot find a required file even through it is in the searched directory