Добавить текст из Excel в файл HTML - PullRequest
2 голосов
/ 13 июня 2019

Я пытаюсь добавить ячейки из Excel (диапазон c3: c14 в листе Testimonial_HTML) в 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, без запятых и некоторых других незначительных форматов.

Ответы [ 2 ]

2 голосов
/ 13 июня 2019

Как сказал Джо в этом вопросе: почему мой код VBA видит запятую как новую строку?

вы можете использовать Линейный ввод #filenum, MyString вместо Введите #filenum, MyString.

надеюсь, это поможет!

1 голос
/ 13 июня 2019

Это просто улучшение вашего кода (не может помочь вам с вашим вопросом), поэтому ваш код выглядит лучше и проще для кодирования:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...