首页 > 解决方案 > 用vba打开html文件

问题描述

我有一些我想用 vba 阅读的 .html 文件。我写了这段代码来做我想做的事,但我得到了

对象变量或未设置块变量

错误。

Dim arrListATA() As String
    Dim arrListTaskNo() As String
    Dim arrListDesc() As String
    Dim arrIssueNo() As String
    Dim arrIssueDate() As String
    Dim arrPartNo() As String
    Dim arrDMC() As String
    Dim arrApplicability() As String
    Dim arrDMCModelCode() As String
    Dim DMCs As String
    Dim arrSubTask() As String
    Dim subTasks As String
    Dim subs() As Variant
    Dim subs1 As String

   k = 0

   Dim objFile As Scripting.File
   Dim objFolder As Scripting.Folder

   Set objFSO = CreateObject("Scripting.FileSystemObject")
w = 0
m = 0
b = 0
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    With fd
    fd.Filters.Clear
     If fd.Show = -1 Then
     myTopFolderPath = fd.SelectedItems(1)
    Set objFolder = objFSO.GetFolder(myTopFolderPath)
    Dim arrSplitedDMC As Variant
    Dim arrSubTasks As Variant
        For Each objFile In objFolder.Files
        Debug.Print myTopFolderPath & "\" & objFile.Name
        If Right(objFile.Name, 4) = "html" And Len(objFile.Name) = 33 And Left(objFile.Name, 8) <> "V2500-00" Then
            Debug.Print myTopFolderPath & "\" & objFile.Name
            Workbooks.Open Filename:=myTopFolderPath & "\" & objFile.Name
            Debug.Print "Opened"

            lastrow = Cells(Rows.Count, 1).End(xlUp).Row
            taskCheckFlag = False
            myTemp = ""
            partNoFlag = False
            mySubTask = ""

            For i = 1 To lastrow
                txt = Cells(i, 1)
            Next i

我的文件夹路径和我的对象名称是这样的

C:\Users\ftk1187\Desktop\V2500 - 复制\V2500-00-70-72-02-00A-363A-D.html

它没有打开我的 .html 文件。我怎么解决这个问题?

标签: vba

解决方案


下面的代码实际运行。

Option Explicit

Private Sub Test()

    Dim arrListATA() As String
    Dim arrListTaskNo() As String
    Dim arrListDesc() As String
    Dim arrIssueNo() As String
    Dim arrIssueDate() As String
    Dim arrPartNo() As String
    Dim arrDMC() As String
    Dim arrApplicability() As String
    Dim arrDMCModelCode() As String
    Dim DMCs As String
    Dim arrSubTask() As String
    Dim subTasks As String
    Dim subs() As Variant
    Dim subs1 As String
    Dim objFSO As FileSystemObject
    Dim Fd As FileDialog
    Dim objFile As Scripting.File
    Dim objFolder As Scripting.Folder
    Dim arrSplitedDMC As Variant
    Dim arrSubTasks As Variant
    Dim myTopFolderPath As String



    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set Fd = Application.FileDialog(msoFileDialogFolderPicker)
'    k = 0
'    w = 0
'    m = 0
'    b = 0

    With Fd
        .Filters.Clear
        If .Show = -1 Then
            myTopFolderPath = .SelectedItems(1)
            Set objFolder = objFSO.GetFolder(myTopFolderPath)
            For Each objFile In objFolder.Files
                Debug.Print myTopFolderPath & "\" & objFile.Name
                Debug.Print myTopFolderPath
                Debug.Print objFile.Name
                Debug.Print Right(objFile.Name, 4), Len(objFile.Name), Left(objFile.Name, 8)

'                If Right(objFile.Name, 4) = "html" And Len(objFile.Name) = 33 And Left(objFile.Name, 8) <> "V2500-00" Then
'                    Debug.Print myTopFolderPath & "\" & objFile.Name
'                    Workbooks.Open Filename:=myTopFolderPath & "\" & objFile.Name
'                    Debug.Print "Opened"
'
'                    lastrow = Cells(Rows.Count, 1).End(xlUp).Row
'                    taskCheckFlag = False
'                    myTemp = ""
'                    partNoFlag = False
'                    mySubTask = ""
'
'                    For i = 1 To lastrow
'                        txt = Cells(i, 1)
'                    Next i
            Next objFile
        End If
    End With
End Sub

你会看到我Option Explicit在顶部添加了一些缺少的声明。变量 k、w、m 和 b 也没有声明,但如果它们是数字,它们的值在代码的那个点应该已经是 0。根据我的研究,Excel 应该能够打开一个 HTML 文件,但我想知道它会显示什么。

作为一般建议,我建议您将代码构建为一个Main 调用其他子程序和函数的子程序,每个子程序不超过 10 到 25 行代码。在您的代码中,您已经超过了声明中的那个数字。效果是您无法控制的构造。


推荐阅读