【问题标题】:Add text from Excel to HTML file将文本从 Excel 添加到 HTML 文件
【发布时间】: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 格式,并且缺少逗号和其他一些次要格式。

【问题讨论】:

  • 您好,安迪,我似乎找不到您的LoopDo While Not EOF(filenum) 它在哪里?我正在尝试净化您的代码(与您的问题完全无关,但仍然有帮助),我看不到循环在哪里结束。
  • 嗨达米安,不知道那里发生了什么,我在粘贴代码时以某种方式删除了循环....循环在 2 关闭行之前...从底部开始的第 5 行! - 刚刚更新...

标签: html excel vba


【解决方案1】:

正如乔在这个问题中所说: why does my vba code see comma as new line?

您可能想使用 行输入#filenum, MyString 代替 输入#filenum,MyString。

希望这会有所帮助!

【讨论】:

  • 非常感谢 - 它有效!这么快就回复的太好了——我已经搜索过了,但也没有看到你引用的帖子!
  • 很高兴它有帮助:)
【解决方案2】:

这只是对您的代码的改进(无法帮助您解决问题),因此您的代码看起来更好,更容易编码:

Option Explicit
    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
    '
    Dim Lines(1 To 12) As String
    Dim MyString As String
    Dim fileName As String, fileName2 As String
    Dim filenum As Long, filenum2 As Long
    Dim j 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"

    With ThisWorkbook.Sheets("Testimonial_HTML")
        For i = 1 To 12
            Lines(i) = .Cells(i + 2, 3)
        Next i
    End With

    filenum2 = FreeFile()
    Open fileName2 For Output As #filenum2
    filenum = FreeFile()
    Open fileName For Input As #filenum
    i = 0

    Do While Not EOF(filenum)
        Input #filenum, MyString
        Print #filenum2, MyString
        If i = 92 Then
            For j = 1 To 12
                Print #filenum2, Lines(j)
            Next j
        End If
        i = i + 1
    Loop

    Close #filenum
    Close #filenum2

    FileCopy fileName2, fileName
    Kill fileName

End Sub

【讨论】:

  • thx Damian - 我有点知道代码并不像它可能的那样好 - 我几乎是一个 vba hack,但愿意尝试获得解决方案..
  • 问题已修复 - 行输入 #filenum, MyString 而不是输入 #filenum, MyString
  • 嗨达米安,有机会看看这段代码 - 类似于你上面清理的内容,但是这个在不同的点有 3 行要插入......Sub跨度>
  • @AndyWelch 代码没问题,除了.Activatething 等,不需要数组或循环,因为只有 3 个项目并且没有相关性。所以你应该只调整代码以避免像我在代码中那样使用.Activate
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多