簡體   English   中英

僅從文件夾(路徑)打開帶有HTML擴展名的文件,而沒有提供文件名

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

打開所有在特定路徑中只有1extension的文件而不給出文件名。因為我在1個文件夾中有很多html文件,它們的文件名不同但擴展名相同(.html)

我可以直接使用此代碼打開,但需要提供文件名。 我有多個具有不同名稱的文件,具有相同的擴展名(.html),這使得打開這些文件1by1並提供要提交的條目非常困難

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

使用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

或者您可以讓用戶選擇想要的文件並對其進行一個接一個的處理:

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