简体   繁体   English

仅从文件夹(路径)打开带有HTML扩展名的文件,而没有提供文件名

[英]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) 打开所有在特定路径中只有1extension的文件而不给出文件名。因为我在1个文件夹中有很多html文件,它们的文件名不同但扩展名相同(.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 我有多个具有不同名称的文件,具有相同的扩展名(.html),这使得打开这些文件1by1并提供要提交的条目非常困难

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. 使用Dir命令循环浏览该文件夹中的所有* .html文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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