ตัวอย่าง VBA code ใช้สร้างบทความบนหน้าเว็บไซต์

 ตัวอย่าง VBA code ใช้สร้างบทความบนหน้าเว็บไซต์

Sub GenerateWebArticle()
    ' Define article title
    Dim title As String
    title = InputBox("Enter article title:")
    ' Define article author
    Dim author As String
    author = InputBox("Enter article author:")
    ' Define article content
    Dim content As String
    content = InputBox("Enter article content:")   
    ' Create HTML file
    Dim fileNumber As Integer
    fileNumber = FreeFile
    Open "article.html" For Output As #fileNumbe
    ' Write HTML code to file
    Print #fileNumber, "<html>"
    Print #fileNumber, "<head>"
    Print #fileNumber, "<title>" & title & "</title>"
    Print #fileNumber, "</head>"
    Print #fileNumber, "<body>"
    Print #fileNumber, "<h1>" & title & "</h1>"
    Print #fileNumber, "<p>By " & author & "</p>"
    Print #fileNumber, "<p>" & content & "</p>"
    Print #fileNumber, "</body>"
    Print #fileNumber, "</html>"
    
    ' Close file
    Close #fileNumber
    
    ' Open HTML file in default web browser
    Dim path As String
    path = ActiveWorkbook.Path & "\article.html"
    ActiveWorkbook.FollowHyperlink path
 End Sub

ในโค้ดนี้จะเริ่มต้นจากการรับข้อมูลเกี่ยวกับบทความเช่น ชื่อเรื่อง (title), ชื่อผู้เขียน (author), และเนื้อหา (content) โดยผู้ใช้จะต้องกรอกข้อมูลเหล่านี้ผ่าน InputBox ก่อนเริ่มการสร้างไฟล์ HTML

จากนั้นโค้ดจะเปิดไฟล์ "article.html" เพื่อเขียน HTML code ลงในไฟล์ โดยใช้คำสั่ง Print #fileNumber เพื่อเขียน HTML tag และข้อความต่างๆลงในไฟล์ HTML จากนั้นโค้ดจะปิดไฟล์และเปิดไฟล์ HTML ด้วยเบราว์เซอร์เริ่มต้น

เมื่อโปรแกรมสร้างไฟล์ HTML เสร็จสิ้นแล้ว ไฟล์จะถูกเปิดในเบราว์เซอร์เริ่มต้นของคุณเพื่อตรวจสอบหน้าตาของบทความ

แสดงความคิดเห็น

0 ความคิดเห็น