Vba 在使用wdDialogFileOpen打开文件之前,是否有任何方法检查文件类型?

Vba 在使用wdDialogFileOpen打开文件之前,是否有任何方法检查文件类型?,vba,ms-word,Vba,Ms Word,我使用wdDialogFileOpen让用户打开文件。我只允许用户打开.docx文件。在使用wdDialogFileOpen打开文件之前(用户使用wdDialogFileOpen选择文件之后),是否有任何方法检查文件类型? 我使用以下代码: 带对话框(wdDialogFileOpen) .Name=“” .表演 以结尾,例如: With Dialogs(wdDialogFileOpen) .Format = "*.docx" If .Display = True Then If

我使用wdDialogFileOpen让用户打开文件。我只允许用户打开.docx文件。在使用wdDialogFileOpen打开文件之前(用户使用wdDialogFileOpen选择文件之后),是否有任何方法检查文件类型? 我使用以下代码:

带对话框(wdDialogFileOpen) .Name=“” .表演 以

结尾,例如:

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

我根本不会使用FileOpen对话框。考虑文件选择器:用户选择文件,然后您决定是否要打开它。这里有一些代码可以使用

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

对。如果使用VBA调用对话框,则可以对其应用过滤器。修改您的问题以包含用于调用对话框的代码。我已编辑我的问题以包含代码。