ITeye BBCode編輯器快速排版技巧

每天和ITeye的編輯器打交道,發佈一篇文章時,爲了顯示規範些,需要花費一定的時間來排版。排版工作相當機械化,就考慮通過Word中的宏來實現,不在非重要的工作上浪費時間,就逐漸寫了一些。

這些宏用的是VB語法,沒什麼難度(多處用到了Word的查找替換功能),但聊勝於無,將這些分享出來,在發佈資訊或寫博客時可以用來快速排版。這些宏中,[b]大部分都是針對BBCode編輯器[/b](在可視化編輯器中調版式沒有BBCode好用)。

[align=center][img]http://dl2.iteye.com/upload/attachment/0093/1246/34a79c6c-a0d9-38bb-9183-ac558df0f4a7.jpg[/img][/align]

[b]使用方法[/b]:這些都是針對Microsoft Word,在Word中,按【Alt+F11】打開VBA環境,選擇【插入】->【模塊】菜單,在編輯器中粘貼本文後面的代碼。

[b]運行方法[/b]:將光標定位在要使用的宏代碼中,單擊工具欄中的【運行】按鈕即可。

可以將這些宏命令加入到Word的工具欄,像上圖一樣,使用時直接點擊即可。也可將常用的一些命令設置個快捷鍵,這樣效率更高。

[align=center][img]http://dl2.iteye.com/upload/attachment/0093/1249/e986b431-8800-37ea-b017-c0bfc0827fdc.jpg[/img][/align]


 Sub 自動鏈接()

'識別鏈接,提取URL,在鏈接文本前後加上[URL]標記

For Each aHyperlink In ActiveDocument.Hyperlinks
If InStr(LCase(aHyperlink.Address), "http") <> 0 Then
aHyperlink.Range.Select

With Selection
.InsertBefore "[url=" & aHyperlink.Address & "]"
End With

With Selection
.InsertAfter "[/url]"
End With

End If

Next aHyperlink

End Sub


Sub 清除格式()

Selection.ClearFormatting

End Sub


Sub 添加行號()

'在選中的每個段落前加上1. 2. 3.……

Dim parag As Paragraph
Dim nLineNum: nLineNum = 0
Dim selRge As Range
Set selRge = Selection.Range

For Each parag In Selection.Paragraphs
nLineNum = nLineNum + 1


If nLineNum > 0 Then
selRge.Paragraphs(nLineNum).Range.InsertBefore (nLineNum & ". ")

End If


'個位數前自動添加0
' If nLineNum < 10 And nLineNum > 0 Then
' selRge.Paragraphs(nLineNum).Range.InsertBefore ("0" & nLineNum & " ")
' Else
' selRge.Paragraphs(nLineNum).Range.InsertBefore (nLineNum & " ")
' End If

Next
End Sub


Sub 表格轉換()

'將表格轉換成bbcode表格格式

換表格
每段加豎線
首尾加table

End Sub


Sub 換表格()

' 將文本換爲表格

Selection.Rows.ConvertToText Separator:=wdSeparateByDefaultListSeparator, _
NestedTables:=True
End Sub

Sub 首尾加table()

'選擇區域首位加上[ table]、[ /table]

With Selection
.InsertParagraphBefore
End With

With Selection
.InsertBefore "[ table]"
End With

With Selection
.InsertAfter "[ /table]"
End With

End Sub


Sub 每段加豎線()

'選擇區域所有段落前加|

Dim parag As Paragraph
Dim nLineNum: nLineNum = 0
Dim selRge As Range
Set selRge = Selection.Range

For Each parag In Selection.Paragraphs


nLineNum = nLineNum + 1


If nLineNum > 0 Then


selRge.Paragraphs(nLineNum).Range.InsertBefore ("|")

Set myrange = selRge.Paragraphs(nLineNum).Range

myrange.End = myrange.End - 1

myrange.InsertAfter ("|")


End If

Next

End Sub


Sub 圖片居中()

' 在所有[img][/img]標記前後加上[align=center][/align]

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[img]"
.Replacement.Text = "[align=center][img]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[/img]"
.Replacement.Text = "[/img][/align]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub 刪除空白行()

'刪除空行

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 段首加空格()

'在每段段首加上4個半角空格

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^p "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 段首刪空格()

'刪除每段段首的空格

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p "
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 刪圖()

'刪除Word文檔中的所有圖片

Dim pic As InlineShape
For Each pic In ActiveDocument.InlineShapes
If pic.Width <> 0 Then
pic.Select
Selection.Delete
End If
Next

End Sub


Sub 手動換行()

'將所有段落標記替換爲手動換行標記


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub 自動換行()

'將所有手動換行標記替換爲段落標記

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub 換HTML空格()

' 將所有HTML格式空格替換爲半角空格

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


End Sub


Sub 自動縮放圖()

'將Word文檔中的可見圖片調整爲統一大小

Dim myis As InlineShape

For Each myis In ActiveDocument.InlineShapes

If myis.Width > CentimetersToPoints(2.5) Then


If myis.Width < CentimetersToPoints(0.5) Then GoTo 10
If myis.Height < CentimetersToPoints(0.5) Then GoTo 10

myis.Reset

' myis.PictureFormat.ColorType = msoPictureGrayscale

myis.LockAspectRatio = msoTrue


myis.ScaleWidth = 70

If myis.Width > CentimetersToPoints(5) Then myis.Width = CentimetersToPoints(9)

myis.ScaleHeight = myis.ScaleWidth


End If

10: Next myis
End Sub

Sub 圖居中()

'居中Word文檔中的所有可見圖片

Dim myis As InlineShape

For Each myis In ActiveDocument.InlineShapes

If myis.Width > 0 Then

myis.Select


Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter


End If

Next myis
End Sub


Sub 換全角空格()

' 將所有全角空格替換爲半角空格

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 換空格()

換HTML空格
換全角空格

End Sub


Sub 加粗()

'在選中的文字前後加上[b][/b]

With Selection
.InsertBefore "[b]"
End With

With Selection
.InsertAfter "[/b]"
End With


End Sub


Sub 加鏈接()



With Selection
.InsertBefore "[url]"
End With

With Selection
.InsertAfter "[/url]"
End With


End Sub

Sub 加鏈接2()


With Selection
.InsertBefore "[url=]"
End With

With Selection
.InsertAfter "[/url]"
End With


End Sub

Sub 列表標籤()

'選擇區域首位加上[list][/list]

With Selection
.InsertParagraphBefore
End With

With Selection
.InsertBefore "[list]"
End With

With Selection
.InsertAfter "[/list]"
End With


End Sub

Sub 列表段號()

'選擇區域所有段落前加[*]

Dim parag As Paragraph
Dim nLineNum: nLineNum = 0
Dim selRge As Range
Set selRge = Selection.Range

For Each parag In Selection.Paragraphs
nLineNum = nLineNum + 1

If nLineNum > 0 Then
selRge.Paragraphs(nLineNum).Range.InsertBefore ("[*]")
End If

Next

End Sub

Sub 加列表()

列表段號
列表標籤

End Sub


Sub 去底紋()


Selection.WholeStory

去段落底紋
去文字底紋

End Sub
Sub 去文字底紋()


With Selection.Font
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
.Borders(1).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
End Sub

Sub 去段落底紋()


With Selection.ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorAutomatic
End With
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
End Sub


Sub 標題樣式加粗()


'如果段落樣式爲指定樣式,則在首位加上[b][/b]

Dim cuti As Paragraph

For Each cuti In ActiveDocument.Paragraphs

If cuti.Style = ActiveDocument.Styles("標題 3") Then

cuti.Range.Select

With Selection
.InsertBefore "[b]"
End With

With Selection
.InsertAfter "[/b]"
End With

End If

Next


End Sub

Sub 標題長度加粗()


' 要求用戶設置長度值

Dim Message, Title, Default, MyValue

Message = "請輸入限定的段落文本字/單詞數"

Title = "限定長度"

Default = "10"

MyValue = InputBox(Message, Title, Default)

' 如果段落文字長度小於設定值,則在首位加上[b][/b]

Dim cuti As Paragraph

For Each cuti In ActiveDocument.Paragraphs


If cuti.Range.Words.Count < MyValue And cuti.Range.Words.Count > 1 Then


' Range.Characters.Count < 20 Then

cuti.Range.Select

With Selection
.InsertBefore "[b]"
End With

Selection.EndKey Unit:=wdLine
Selection.TypeText Text:="[/b]"
Selection.MoveRight Unit:=wdCharacter, Count:=1


' With Selection
' .InsertAfter "[/b]"
' End With

End If

Next

End Sub

Sub 清除加粗()

' 清除所有的加粗標記[b][/b]

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[b]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "[/b]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Sub 修復分段()
'
' 文中有不正確的分段標記,該宏可以修復此類問題
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "aaabbbccc"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ".aaabbbccc"
.Replacement.Text = ".^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "aaabbbccc"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 刪空行()


Dim kong As Paragraph

For Each kong In ActiveDocument.Paragraphs


If kong.Range.Characters.Count = 1 Then


kong.Range.Select

Selection.Delete


End If

Next

段首刪空格


End Sub
Sub 檢查鏈接()
'
' 檢查“[url=”和“http://”中是否有空格,有則刪除
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[url= http://"
.Replacement.Text = "[url=http://"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "[url= https://"
.Replacement.Text = "[url=https://"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub


Sub 取消所有超鏈接()

'清除所有的超鏈接


Dim oField As Field

For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldHyperlink Then
oField.Unlink
End If

Next
Set oField = Nothing
End Sub


Sub 選擇部分手動換行()

'將選擇部分的段落標記替換爲手動換行標記

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 週報鏈接()

'Markup語法(寫週報用):識別鏈接,提取URL,加上#

For Each aHyperlink In ActiveDocument.Hyperlinks

If InStr(LCase(aHyperlink.Address), "http") <> 0 Then

aHyperlink.Range.Select

With Selection
.InsertBefore "#[" & aHyperlink.Address & " "
End With

With Selection
.InsertAfter "]"
End With

End If

Next aHyperlink


End Sub


Sub 超級替換()

'把常見的確實可以自動替換的錯別字進行自動替換。
'第一個參數是錯別字,第二個參數是正確的字


替換常用錯別字 "惟一", "唯一"
替換常用錯別字 "帳號", "賬號"
替換常用錯別字 "圖象", "圖像"
替換常用錯別字 "登陸", "登錄"
替換常用錯別字 "其它", "其他"
替換常用錯別字 "按裝", "安裝"
替換常用錯別字 "按紐", "按鈕"
替換常用錯別字 "成份", "成分"
替換常用錯別字 "題綱", "提綱"
替換常用錯別字 "煤體", "媒體"
替換常用錯別字 "存貯", "存儲"
替換常用錯別字 "一楨", "一幀"
替換常用錯別字 "好象", "好像"
替換常用錯別字 "對像", "對象"


End Sub

Sub 替換常用錯別字(strWrong As String, strRight)

'此過程僅供程序調用,不要人手工使用
'
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = strWrong
.Replacement.Text = strRight
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub


Sub 段間加空行()

'在段落間加上空行,[list]列表之間不加空行

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p[*]"
.Replacement.Text = "[*]"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "[/list]^p^p"
.Replacement.Text = "[/list]^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


End Sub

Sub 字體紅色()

With Selection
.InsertBefore "[color=red]"
End With

With Selection
.InsertAfter "[/color]"
End With


End Sub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章