搜索多个文件扩展名

时间:2015-08-18 01:23:34

标签: excel-vba vba excel

我有一个vba语法,只搜索pdf文件。我想知道我需要在下面的代码中做出哪些更改,以便它也可以搜索docx和类似的ext文件。

AddPet(vector<Pets> &vtnew)
{
Pets newpet;
cout << "Enter the pet's name: " << endl;
cin >> newpet.Name;
cout << "Enter the pet's race: " << endl;
cin >> newpet.Race;
cout << "Enter the owner's name: " << endl;
cin >> newpet.Owner;
cout << "Enter the owner's telephone number: " << endl;
cin >> newpet.Tel;
vtnew.push_back(newpet);
}

1 个答案:

答案 0 :(得分:2)

首先更改Dir命令以查找folder & "\*.*。虽然您忽略了代码的重要位置,但Dir列表不会返回文件掩码之外的任何内容。接下来,从右侧拉出文件扩展名,并将其与所需文件扩展名列表进行比较。

dim folder as string, pfile as string, ext as string
folder = "c:\temp"
pfile = Dir(folder & "\*.*")
do while cbool(len(pfile))
    ext = chr(32) & lcase(trim(right(replace(pfile, chr(46), space(99)), 99))) & chr(32)
    if cbool(instr(1, " pdf docx doc xls xlsx ", ext, vbTextCompare)) then
        'do something with the matching file
    end if
    pfile = Dir
loop