アルバムのデータをExcelで管理して,マクロでHTML,XMLを生成する

アルバムデータの書き出し

 私は持っているアルバムのデータ(というほどのものでもないですが)を, Excel97に入力して管理しています.

Excel画面

で,このデータを出力するときに使っているVBAマクロを紹介します.


HTMLファイルの書き出し

 これは,HTMLのTABLEタグをファイルに出力するためのマクロです. Excelの「HTMLファイルに保存」機能が,あまりにも無駄の多いTABLEタグを出力したので作りました.

この出力結果を,アルバム紹介のHTMLファイル内に貼り付けて使っています.

Public Sub outHtml()
    Dim lngRow As Long
    Dim strOldBand As String
    Dim intFileNum As Integer
    Dim strBand As String
    Dim strTitle As String
    Dim strComment As String
    Dim strWork As String

    intFileNum = FreeFile
    strOldBand = ""

    Open "album.html" For Output As #intFileNum

    Print #intFileNum, "<TABLE BORDER=1>"

    lngRow = 1
    Do While Sheet1.Cells(lngRow, 1) <> ""
        strBand = Sheet1.Cells(lngRow, 1)
        strTitle = Sheet1.Cells(lngRow, 2)
        strComment = Sheet1.Cells(lngRow, 3)

        If strBand = "" Then strBand = "&nbsp;"
        If strComment = "" Then strComment = "&nbsp;"

        If strBand <> strOldBand Then
            strWork = "<TR>" & _
                "<TD BGCOLOR=" & Chr$(34) & "#e0d0d0" & Chr$(34) & ">" & strBand & _
                "<TD>" & strTitle & _
                "<TD>" & strComment
            strOldBand = strBand
        Else
            strWork = "<TR>" & _
                "<TD>" & _
                "<TD>" & strTitle & _
                "<TD>" & strComment
        End If

        Print #intFileNum, strWork

        lngRow = lngRow + 1
    Loop

    Print #intFileNum, "</TABLE>"

    Close #intFileNum

End Sub

XMLファイルの書き出し

 まだお遊び段階なのですが,XMLファイルとして書き出すためのマクロです.

とりあえずDTDはありません.

Public Sub outXml()
    Dim lngRow As Long
    Dim intFileNum As Integer
    Dim strBand As String
    Dim strTitle As String
    Dim strComment As String

    intFileNum = FreeFile

    Open "albums.xml" For Output As #intFileNum

    Print #intFileNum, "<albums>"

    lngRow = 1
    Do While Sheet1.Cells(lngRow, 1) <> ""
        strBand = Sheet1.Cells(lngRow, 1)
        strTitle = Sheet1.Cells(lngRow, 2)
        strComment = Sheet1.Cells(lngRow, 3)

        Print #intFileNum, "<album>"
        Print #intFileNum, "<band>" & strBand & "</band>"
        Print #intFileNum, "<title>" & strTitle & "</title>"
        Print #intFileNum, "<comment>" & strComment & "</comment>"
        Print #intFileNum, "</album>"

        lngRow = lngRow + 1
    Loop

    Print #intFileNum, "</albums>"

    Close #intFileNum

End Sub


© 2024 KMIソフトウェア