vba - 用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 文件。我怎么解决这个问题?
解决方案
下面的代码实际运行。
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 行代码。在您的代码中,您已经超过了声明中的那个数字。效果是您无法控制的构造。
推荐阅读
- c - Ncurses waddch 不在光标位置打印字符
- javascript - 如何在 Javascript 中存储 $_SESSION 变量以用于 XMLHttpRequest 目的?
- html - IE11 Grid 自身崩溃
- javascript - React w/hooks:防止使用作为 prop 的函数重新渲染组件
- node.js - 护照认证后快递不会重定向
- android-studio - Google Play 广告库中的版本不匹配
- azure - dotnet 发布有时会因“元数据生成失败”而失败
- python - 使用多索引 Pandas 和样式进行过滤
- python - 从 numpy argsort 得到奇怪的结果
- javascript - 如何使图像显示在容器 Div 内?