GetOpenFileName該函式創建一個Open公共對話框,使用戶指定驅動器、目錄和檔案名稱、或使用戶打開檔案。
基本介紹
- 中文名:GetOpenFileName
- 函式原型:BOOL GetOpenFileName
- 返回值:錯誤出現,返回值為零
- 特徵:Windows Explorer
函式原型
參數
使用方法:Nextss'TesttoseeifanyScopeFolderscollectionswereaddedto'theSearchFolderscollection.If.SearchFolders.Count>0Then'SettheLookInpropertytothepathof'thefirstScopeFolderobjectintheSearchFolders'collection.Thisisheresothatanyprevious'settingoftheLookInpropertydoesn'taffect'thesearch..LookIn=.SearchFolders.Item(1).Path'Executethesearchandtesttoseeifanyfiles'werefound.If.Execute<>0Then'Displaythenumberoffilesfound.MsgBox"Filesfound:"&.FoundFiles.Count'Loopthroughthelistoffoundfilesand'displaythepathofeachoneinamessagebox.ForlngCount=1To.FoundFiles.CountIfMsgBox(.FoundFiles.Item(lngCount),vbOKCancel,_"Foundfiles")=vbCancelThen'BreakoutofthelooplngCount=.FoundFiles.CountEndIfNextlngCountEndIfEndIfEndWithEndSub'ThissubroutineloopsthroughalloftheScopeFolderscollections'inagivenScopeFolderscollection.Itaddsanyfolder'thathasthesamenameasthevalueofstrFolder'totheSearchFolderscollection.SubOutputPaths(ByValsfsAsScopeFolders,_ByRefstrFolderAsString)'DeclareavariableasaScopeFolderobjectDimsfAsScopeFolder'LoopthrougheachScopeFolderobjectinthe'ScopeFolderscollection.ForEachsfInsfs'TesttoseeifthefoldernameoftheScopeFolder'matchesthevalueofstrFolder.UseLCasetoensure'thatcasedoesnotaffectthematch.IfLCase(sf.Name)=LCase(strFolder)Then'AddtheScopeFoldertotheSearchFolderscollection.sf.AddToSearchFoldersEndIf'IncludeaDoEventscallbecausethereisthepotentialforthis'looptolastalongtime.TheDoEventscallallowsthisprocessto'continuehandlingevents.DoEvents'TesttoseeiftheScopeFolderscollectioninthe'currentScopeFolderisempty.Ifitisn'tempty,then'thatmeansthatthecurrentScopeFolderobjectcontainssubfolders.Ifsf.ScopeFolders.Count>0Then'Thissubroutinerecursivelycallsitselfsothat'itcanaddthesubfoldersofthecurrentScopeFolderobject'totheSearchFolderscollection.CallOutputPaths(sf.ScopeFolders,strFolder)EndIfNextsfEndSubSearchScopes屬性返回SearchScopes集合。本示例顯示SearchScopes集合中所有當前可用的SearchScope對象。SubDisplayAvailableScopes()'Declareavariablethatreferencesa'SearchScopeobject.DimssAsSearchScope'UseaWith...EndWithblocktoreferencethe'FileSearchobject.WithApplication.FileSearch'LoopthroughtheSearchScopescollectionForEachssIn.SearchScopesSelectCasess.TypeCasemsoSearchInMyComputerMsgBox"MyComputerisanavailablesearchscope."CasemsoSearchInMyNetworkPlacesMsgBox"MyNetworkPlacesisanavailablesearchscope."CasemsoSearchInOutlookMsgBox"Outlookisanavailablesearchscope."CasemsoSearchInCustomMsgBox"Acustomsearchscopeisavailable."CaseElseMsgBox"Can'tdeterminesearchscope."EndSelectNextssEndWithEndSubSearchScopes集合SearchScope對象的集合。使用FileSearch對象的SearchScopes屬性返回SearchScopes集合,例如:DimsssAsSearchScopesSetsss=Application.FileSearch.SearchScopes不能向SearchScopes集合中添加或從中刪除SearchScope對象。SearchSubFolders屬性如果搜尋範圍包括LookIn屬性指定的資料夾中的所有子資料夾,則返回True。Boolean類型,可讀寫。TextOrProperty屬性返回或設定在查找檔案的過程中要搜尋的單詞或短語,它們可位於一個檔案的正文或檔案屬性中。該單詞或短語可包含*(星號)或?(問號)通配符。String類型,可讀寫。用問號通配符可匹配任意單個字元。例如,鍵入“gr?y”可找到符合如下條件的所有檔案,即檔案中至少有一處出現單詞“gray”或“grey”。用星號通配符可匹配任意數目的字元。例如,鍵入“San*”可找到符合如下條件的所有檔案,即文件中至少有一處出現以“San”開頭的單詞。本示例可實現的功能為:搜尋“C:\MyDocuments”資料夾及其子資料夾,並返回所有這樣的檔案,該檔案在檔案正文或檔案屬性中包含以“San”開頭的單詞。TextOrProperty屬性設定要查找的單詞,並將搜尋範圍限制在檔案正文或檔案屬性中。WithApplication.FileSearch.NewSearch.LookIn="C:\MyDocuments".SearchSubFolders=True.TextOrProperty="San*".FileType=msoFileTypeAllFilesEndWithExecute方法開始對指定檔案的搜尋。返回一個Long類型,如果沒有找到檔案,則返回零(0),如果找到一個或多個檔案,則返回一個正數。expression.Execute(SortBy,SortOrder,AlwaysAccurate)其中,expression必需。該表達式返回一個FileSearch對象。參數SortBy,MsoSortBy類型,可選。該方法用於對返回的檔案進行排序。MsoSortBy可以為下列MsoSortBy常量之一。msoSortByFileName默認值、msoSortByFileType、msoSortByLastModified、msoSortByNone、msoSortBySize。參數SortOrder,MsoSortOrder類型,可選。表明所返回檔案的排序順序。參數MsoSortOrder,可以為下列MsoSortOrder常量之一。msoSortOrderAscending,默認值、msoSortOrderDescending。參數AlwaysAccurate,Boolean類型,可選。設定為True使檔案搜尋包括上次更新檔案索引以來添加、修改或刪除的檔案。默認值為True。本示例在MyDocuments資料夾中搜尋以擴展名“.doc”結尾的所有檔案,然後顯示找到的每個檔案的位置和名稱。本示例還以字母升序排序返回的檔案名稱稱。Setfs=Application.FileSearchWithfs.LookIn="C:\MyDocuments".FileName="*.doc"If.Execute(SortBy:=msoSortByFileName,_SortOrder:=msoSortOrderAscending)>0ThenMsgBox"Therewere"&.FoundFiles.Count&_"file(s)found."Fori=1To.FoundFiles.CountMsgBox.FoundFiles(i)NextiElseMsgBox"Therewerenofilesfound."EndIfEndWithNewSearch方法將所有搜尋條件重置為默認設定。搜尋條件的設定在應用程式的一個會話中將保持不變。每次更改搜尋條件時可用此方法。此方法不會重新設定LookIn屬性的值。本示例可實現的功能為:在開始新一輪搜尋過程之前用NewSearch方法重新設定默認的搜尋條件。WithApplication.FileSearch.NewSearch.LookIn="C:\MyDocuments".SearchSubFolders=True.FileName="run".TextOrProperty="San*".MatchAllWordForms=True.FileType=msoFileTypeAllFilesIf.Execute()>0ThenMsgBox"Therewere"&.FoundFiles.Count&_"file(s)found."Fori=1To.FoundFiles.CountMsgBox.FoundFiles(i)NextiElseMsgBox"Therewerenofilesfound."EndIfEndWithRefreshScopes方法刷新當前可用ScopeFolder對象的列表。下面的示例將顯示“我的電腦”C:\驅動器上所有當前可用的ScopeFolder對象,並說明在對資料夾列表進行更改時需要使用RefreshScopes方法。SubTestRefreshScopesMethod()'DisplayswhathappensbeforeandaftertheRefreshScopes'methodiscalledwhenanewfolderisaddedtothelist'ofscopefolders.'Listbeforethefolderiscreated.CallListFolderNames'CreateanewfolderontheC:\driveinMyComputer.'Anerrorwilloccurifthisfolderalreadyexists.MkDirPath:="C:\Delete_After_Using"'Listafterthefolderiscreated.'Thenewly-createdfolderdoesnotappearinthelist.CallListFolderNames'Refreshthelistoffolders.Application.FileSearch.RefreshScopes'Thenewly-createdfoldernowappearsinthelist.CallListFolderNamesEndSubSubListFolderNames()DimiAsIntegerDimstrResultsAsString'Loopthroughallthetop-levelfoldernamesontheC:\drive'inMyComputerandreporttheresults.'.SearchScopes.Item(1)="MyComputer"'.ScopeFolders.Item(2)="C:\"WithApplication.FileSearch.SearchScopes.Item(1)._ScopeFolder.ScopeFolders.Item(2)Fori=1To.ScopeFolders.CountstrResults=strResults&.ScopeFolders._Item(i).Name&vbCrLfNextiMsgBox"FolderNamesonC:\...."&vbCrLf&strResultsEndWithEndSub