【发布时间】:2019-06-13 08:30:12
【问题描述】:
我正在尝试将 Excel 中的单元格(Testimonial_HTML 表中的范围 c3:c14)添加到 html 文件中,并将文件保存为 HTML 格式。
代码(有效地)将文本插入到 HTML 文件的第 92 行,这正是我想要的。
代码运行,但是,它没有以 HTML 格式保存。它从原始 html 文件中删除了一些格式。例如,它会删除逗号。
Public Sub FiletoAppend()
Dim inFilePath As String
Dim outFilePath As String
Dim inFile As Integer
Dim outFile As Integer
Dim lineCount As Long
Dim fileLine As String
inFilePath = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\testimonia
ls.html" ' directory of file to append
outFilePath = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\" &
"\MyHTML.html" 'fileName2 = Environ("temp file") & "\MyHTML.html"
inFile = FreeFile
Open inFilePath For Input As #inFile
outFile = FreeFile
Open outFilePath For Output As #outFile
lineCount = 0
Do Until EOF(inFile)
Line Input #inFile, fileLine
lineCount = lineCount + 1
If lineCount < 400 Or lineCount > 401 Then
Print #outFile, fileLine
End If
Loop
Close #inFile
Close #outFile
UpdateHTMLFile
End Sub
Sub UpdateHTMLFile()
' inserts new Testimonial to testimonials.html file at line 92 *** see
below to change line number
'
Sheets("Testimonial_HTML").Activate
Dim line12 As String
Dim line11 As String
Dim line10 As String
Dim line9 As String
Dim line8 As String
Dim line7 As String
Dim line6 As String
Dim line5 As String
Dim line4 As String
Dim line3 As String
Dim line2 As String
Dim line1 As String
Dim MyString As String
Dim fileName As String, fileName2 As String
Dim filenum As Long, filenum2 As Long
Dim i As Long
fileName2 = "C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\testimonia
ls.html" ' directory of file to append
fileName =
"C:\Users\user\Documents\Websites\Clients\AJAYS_WebDesign\joesland\" &
"\MyHTML.html" 'fileName2 = Environ("temp") & "\MyHTML.html"
line1 = Range("c3:c3") ' the cell that all new jobs are appended to and code copied from
line2 = Range("c4:c4") ' the cell that all new jobs are appended to and code copied from
line3 = Range("c5:c5") ' the cell that all new jobs are appended to and code copied from
line4 = Range("c6:c6") ' the cell that all new jobs are appended to and code copied from
line5 = Range("c7:c7") ' the cell that all new jobs are appended to and code copied from
line6 = Range("c8:c8") ' the cell that all new jobs are appended to and code copied from
line7 = Range("c9:c9") ' the cell that all new jobs are appended to and code copied from
line8 = Range("c10:c10") ' the cell that all new jobs are appended to and code copied from
line9 = Range("c11:c11") ' the cell that all new jobs are appended to and code copied from
line10 = Range("c12:c12") ' the cell that all new jobs are appended to and code copied from
line11 = Range("c13:c13") ' the cell that all new jobs are appended to and code copied from
line12 = Range("c14:c14") ' the cell that all new jobs are appended to and code copied from
filenum2 = FreeFile()
Open fileName2 For Output As #filenum2
filenum = FreeFile()
Open fileName For Input As #filenum
Do While Not EOF(filenum)
i = i + 1
j = j + 1
k = k + 1
l = l + 1
m = m + 1
n = n + 1
o = o + 1
p = p + 1
q = q + 1
r = r + 1
s = s + 1
t = t + 1
Input #filenum, MyString
Print #filenum2, MyString
If i = 92 Then Print #filenum2, line1 ' i = the line number in html file
If j = 92 Then Print #filenum2, line2 ' j = the line number in html file
If k = 92 Then Print #filenum2, line3 ' k = the line number in html file
If l = 92 Then Print #filenum2, line4 ' l = the line number in html file
If m = 92 Then Print #filenum2, line5 ' m = the line number in html file
If n = 92 Then Print #filenum2, line6 ' n = the line number in html file
If o = 92 Then Print #filenum2, line7 ' o = the line number in html file
If p = 92 Then Print #filenum2, line8 ' p = the line number in html file
If q = 92 Then Print #filenum2, line9 ' q = the line number in html file
If r = 92 Then Print #filenum2, line10 ' r = the line number in html file
If s = 92 Then Print #filenum2, line11 ' s = the line number in html file
If t = 92 Then Print #filenum2, line12 ' t = the line number in html file
Loop
Close #filenum
Close #filenum2
FileCopy fileName2, fileName
Kill fileName
End Sub
如上所述,上述模块运行,但生成的文件不是 HTML 格式,并且缺少逗号和其他一些次要格式。
【问题讨论】:
-
您好,安迪,我似乎找不到您的
Loop的Do While Not EOF(filenum)它在哪里?我正在尝试净化您的代码(与您的问题完全无关,但仍然有帮助),我看不到循环在哪里结束。 -
嗨达米安,不知道那里发生了什么,我在粘贴代码时以某种方式删除了循环....循环在 2 关闭行之前...从底部开始的第 5 行! - 刚刚更新...