简体   繁体   中英

Open only files with HTML extension from folder(path) without giving File names

Open all files of only 1extension in particular path without giving file names.Since i have lot of html files in 1 folder with different file names but same extension(.html)

I can open directly with this code but need to give file name. I have multiple files with different names with same(.html) extension which makes very difficult to open those files 1by1 and giving entries to submit

Set web = CreateObject("Shell.Application")
web.Open ("C:\Users\hp\Desktop\mani\hi.html")
Set web = Nothing

Use the Dir command to cycle through all *.html files in that folder.

dim fn as string, fp as string, fm as string
dim web as object

Set web = CreateObject("Shell.Application")
fp = "C:\Users\hp\Desktop\mani\"
fm = "*.html"
fn = Dir(fp & fm)
do while cbool(len(fn))
    web.Open fp & fn
    'do something to the file here
    'close the open file here
    fn = Dir
loop

or you could have the user pick the wanted file(s) and process them one by one:

Sub PickHTMLFile()
    With Application.FileDialog(msoFileDialogFilePicker)
        .Filters.Add "Html files", "*.html", 2 ' add a filter #2
        .FilterIndex = 2 ' set initial file filter to the added one #

        .Show
        If .SelectedItems.Count > 0 Then
            Dim f As Variant
            For Each f In .SelectedItems 'loop through selected items
                'do your job here, as for example:
                MsgBox "Selected item's path: " & f 'show the current item path
                With CreateObject("Shell.Application")
                    .Open f
                End With
            Next
        Else
            MsgBox "user canceled file picker!"
        End If
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM