ลบช่องว่างเกินในเอกสาร Word ด้วย Macro คลิกเดียว

 

ลบช่องว่างเกินในเอกสาร Word ด้วย Macro คลิกเดียว

เอกสารที่เว้นวรรคไม่เท่ากัน เคาะ 2 ครั้งบ้าง 5 ครั้งบ้าง ไม่ต้องไล่แก้ทีละจุดอีกต่อไป คัดลอกโค้ดด้านล่างไปใช้ได้ทันที แม้ไม่เคยเขียนโค้ดมาก่อน

ก่อน···คู่มือ····ฉบับนี้··จัดทำขึ้นเพื่อ·····พนักงาน··
หลังคู่มือ·ฉบับนี้·จัดทำขึ้นเพื่อ·พนักงาน

จุด · คือช่องว่างจากการเคาะ Spacebar ซึ่งจะเห็นเมื่อกดปุ่ม ¶ (Show/Hide) ในแท็บ Home

Macro นี้ทำอะไรให้บ้าง

  • ช่องว่างที่เคาะติดกันตั้งแต่ 2 ครั้งขึ้นไป จะเหลือ 1 ช่อง
  • ช่องว่างที่เกินมาหน้าข้อความและท้ายข้อความของทุกบรรทัด จะถูกลบออก
  • ทำงานกับทั้งเอกสารในคลิกเดียว ไม่ว่าเอกสารจะยาวกี่หน้า

วิธีใช้งาน

  1. เปิดแท็บ Developer
    ถ้ายังไม่มีแท็บนี้ ไปที่ File > Options > Customize Ribbon ติ๊กช่อง Developer ด้านขวา แล้วกด OK
  2. สร้าง Macro ใหม่
    ไปที่ Developer > Macros พิมพ์ชื่อ CleanDocument แล้วกด Create
  3. วางโค้ด
    หน้าต่าง VBA จะเปิดขึ้นพร้อมบรรทัด Sub CleanDocument() และ End Sub ให้ลบทั้งสองบรรทัดออก แล้วกดปุ่มคัดลอกโค้ดด้านล่างมาวางแทน
  4. บันทึกและปิด
    กด Ctrl+S แล้วปิดหน้าต่าง VBA กลับมาที่เอกสาร
  5. รัน Macro
    ไปที่ Developer > Macros เลือก CleanDocument แล้วกด Run เมื่อขึ้นข้อความ Done ให้กด OK เป็นอันเสร็จ

โค้ด Macro

CleanDoc.vba
Sub CleanDoc()
    Dim p As Paragraph   ' Holds each paragraph in the loop
    Dim r As Range       ' Holds the range of spaces to delete

    Application.ScreenUpdating = False   ' Turn off screen updating to run faster

    '==================================================
    ' Step 1: Replace 2 or more spaces with 1 space
    '==================================================
    With ActiveDocument.Content.Find
        .ClearFormatting                 ' Clear old search settings
        .Replacement.ClearFormatting     ' Clear old replace settings
        .Text = " {2,}"                  ' Find 2 or more spaces in a row
        .Replacement.Text = " "          ' Replace with 1 space
        .MatchWildcards = True           ' Turn on wildcards so {2,} works
        .Wrap = wdFindStop               ' Stop at the end of the document
        .Execute Replace:=wdReplaceAll   ' Replace all at once
        .MatchWildcards = False          ' Turn wildcards off again
    End With

    '==================================================
    ' Step 2: Remove spaces at the start and end of each paragraph
    '==================================================
    For Each p In ActiveDocument.Paragraphs

        ' Remove spaces at the start
        Set r = p.Range.Duplicate                ' Copy the paragraph range
        r.Collapse wdCollapseStart               ' Move to the start of the paragraph
        r.MoveEndWhile Cset:=" "                 ' Select all spaces going right
        If r.End > r.Start Then r.Delete         ' Delete them if found

        ' Remove spaces at the end
        Set r = p.Range.Duplicate                ' Copy the paragraph range again
        r.MoveEnd wdCharacter, -1                ' Step back before the paragraph mark
        r.Collapse wdCollapseEnd                 ' Move to the end of the text
        r.MoveStartWhile Cset:=" ", Count:=wdBackward   ' Select all spaces going left
        If r.End > r.Start Then r.Delete         ' Delete them if found

    Next p

    Application.ScreenUpdating = True    ' Turn screen updating back on
    MsgBox "Document has been cleaned.", vbInformation, "Done"
End Sub

ข้อควรรู้

  • Macro ลบเฉพาะช่องว่างจาก Spacebar ช่องว่างจากการกด Tab (แสดงเป็นลูกศร → เมื่อเปิด ¶) จะไม่ถูกลบ
  • ใช้ได้กับทุกไฟล์ Macro ถูกบันทึกไว้ใน Word ของเครื่องเรา เปิดเอกสารไหนก็รันได้ ไม่ต้องวางโค้ดใหม่
  • ไม่พอใจผลลัพธ์ กด Ctrl+Z เพื่อย้อนกลับ หรือทดลองกับไฟล์สำเนาก่อนใช้กับเอกสารจริง
  • ใช้ Word บน Mac เปิดแท็บ Developer ได้ที่ Word > Settings > Ribbon & Toolbar




Leave a Reply