相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to find an Excel file. However, the extension of the file I"m looking for could be .xls or .xlsx. I was considering using FileExists but I can't use a wildcard with that. Here's my attempt at using GetFiles, however, the .xls* part of my code does not work. I've never used GetFiles before, can anyone give me some guidance on what I'm doing wrong?

Dim InputFormPath As String = "W:\TOM\ERIC\NET Dev\"
Dim wbNameXLSInputForm As String = StatVar.xlApp.Sheets("New Calculator Input").Range("D15").Text & ".xls*"
Dim XLSInputForm As String = wbNameXLSInputForm
Dim dirs As String() = Directory.GetFiles(InputFormPath, wbNameXLSInputForm)
If dirs.Length <> 0 Then
'do something
End If
                Apparently, you are doing everything fine. Both options should work in your situation: InputFormPath & StatVar.xlApp.Sheets("New Calculator Input").Range("D15").Text & ".xls*" and "*" & InputFormPath & StatVar.xlApp.Sheets("New Calculator Input").Range("D15").Text & ".xls" With these two filters you should get both the xls and xlsx versions (and any other one like xlsm).
– varocarbas
                Aug 18, 2013 at 15:46
                Take a look at this documentation. msdn.microsoft.com/en-us/library/8he88b63.aspx. Specifically it says The following list shows the behavior of different lengths for the searchPattern parameter: "*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.
– Sean Carroll
                Aug 18, 2013 at 16:05
                @SeanCarroll you are right. As said, his code should work without any problem. What is not right is your actual answer, please delete it or just update it with the comment you have written right now.
– varocarbas
                Aug 18, 2013 at 16:07
                I've done that, thanks, but it still doesn't seem to be finding the file. I'm going to to keep tinkering, but do you think it could it be my "If dir.Length" line? The syntax for the GetFiles line seems correct
– Eric J
                Aug 18, 2013 at 16:22
                If I hard-code ".xlsx" it finds and opens the file. However, using the wildcard as suggested does not find the file. The documentation says it should work, but I'm not having any luck here
– Eric J
                Aug 18, 2013 at 16:43
        

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.