Tìm và thay thế Symbol trong winword
Người dịch và phát triển: Nguyễn Phương Thảo. Địa chỉ: 525 - Điện Biên - TX.Yên Bái fthaoabc@yahoo.com
Khi sử dụng Word, ta có thể tìm một ký hiệu, thay bằng một Symbol bằng cách sử dụng chức năng tìm thay thế thông qua mã Clipboard. Ví dụ, để thay thế một ký hiệu F& đánh trong văn bản bằng ký hiệu F Symbol, Trung tâm phổ cập Tin học miền núi ABC hướng dẫn học sinh sử dụng cách chèn ký hiệu Symbol này ra, rồi đưa Symbol này vào ClipBoard bằng nút Cut hoặc Copy. Thực hiện chức năng tìm và thay thế (Ctrl+H). Tìm gì: F&. Thay thế bằng: ^c. (Đó mã của những gì trong Clipboard)
Khi muốn tìm Symbol và thay thế bằng Symbol hoặc thay bằng một đoạn văn bản thì tôi xin giới thiệu cách thiết lập thủ tục bằng Visual Basic For Application.
Bạn khởi động Visual Basic For Application trong Winword bằng nhiều cách. Tôi chọn Alt+F11. Rồi gõ Ctrl+R để hiện Project Explore. Click vào tệp Normal.dot rồi Click chọn Insert Form.
Tại Form Cần có các nút lệnh, hộp nhãn, nút tròn sau (Ta không phải đặt thuộc tính Caption vì khi thi hành chương trình sẽ tự điền các thuộc tính cần thiết)
Tên Form: FindSymbol97
cmdCancelButton - Nút thôi
cmdFindNextButton - Nút tìm tiếp
cmdReplaceButton - Nút thay
cmdReplaceAllButton - Nút thay tất cả
cmdFindSymbolDialogButton - Nút tìm hiện hộp thoại
cmdReplaceSymbolDialogButton - Nút thay thế để hiện hôp thoại
lblFindCharacter - Nhãn thông báo ký tự tìm
lblFindFont - Nhãn thông báo Font chữ tìm
lblReplaceCharacter - Nhãn thông báo ký tự thay
lblReplaceFont Nhãn thông báo Font thay
optSearchDoc Nút lựa chọn tìm
optSearchSel - Nút lựa chọn tìm trong phần chọn
optReplaceWithText - Nút chọn thay thế bằng đoạn văn bản
lblFW,lblRW,lblSD - Nhãn hướng
Sau đó DClick vào Form để tiến
hành viết Code cho Form như sau:
Dim NRMText As String
Dim SearchingNormal As String
Dim SearchingSpecChar As String
Dim RepSpecChar As String
Dim FSSel As String
Dim FSDoc As String
Dim SNF As String
Dim TheFORMName As String
Dim FW As String
Dim RW As String
Dim SYM As String
Dim aSYM As String
Dim RSYM As String
Dim aRSYM As String
Dim FNT As String
Dim CHRTR As String
Dim DOC As String
Dim aDOC As String
Dim SEL As String
Dim aSEL As String
Dim oRWT As String
Dim aoRWT As String
Dim REP As String
Dim aREP As String
Dim REPA As String
Dim aREPA As String
Dim CANC As String
Dim aCANC As String
Dim FINDN As String
Dim aFINDN As String
Dim SRCH As String
Dim WholeDoc As Integer
Dim XVar As String
Dim Done As Integer
Dim GoodFind As Integer
Dim NotFoundMsg As Integer
Dim SinglePHolder As Variant
Dim ToggleReplaceFind As Variant
Dim vlblFindFontText As Variant
Dim vlblFindCharacterText As Variant
Dim vlblReplaceFontText As Variant
Dim vlblReplaceCharacterText As Variant
Sub Localization()
'***Bắt đầu LOCALIZATION***
'***ATTN LOCALIZATION!!!: Đoạn "(normal text)" sẽ hiện dưới
'***(Xin chọn hộp lựa chọn font đầu tiên trong hộp thoại
'*** xoá InsertSymbol trong hộp thoại cùng Version
'*** 4 dòng)
'Định nghĩa các lời nhắc
NRMText = "(normal text)"
SearchingNormal = "Dùng Edit/Find Replace (hoặc Ctrl + H) để tìm các ký tự trong
tìm và thay thế bình thường."
SearchingSpecChar = "Dùng Edit/Find Replace để tìm thay những ký tự đặc biệt."
RepSpecChar = "Sử dụng hộp Normal Text font để thay thế bằng những ký tự đặc
biệt."
FSSel = "Đã tìm xong trong phần lựa chọn (Bôi đen)."
FSDoc = "Đã tìm xong trong văn bản."
SNF = "Symbol không tìm thấy."
'Định nghĩa các nhãn và các phím nóng trong Form
TheFORMName = "Tin học ABC - Tìm thay thế Symbol"
FW = "Tìm gì:"
RW = "Thay thế bằng:"
SRCH = "Chỗ tìm:"
FNT = "Font:"
CHRTR = "Ký tự:"
SYM = "Symbol"
aSYM = "s"
RSYM = "Symbol"
aRSYM = "y"
DOC = "Toàn bộ văn bản"
aDOC = "b"
SEL = "Phần chọn"
aSEL = "c"
REP = "Thay thế"
aREP = "T"
REPA = "Thay tất cả"
aREPA = "a"
CANC = "Thôi"
aCANC = "i"
FINDN = "Tìm tiếp"
aFINDN = "p"
oRWT = " Đoạn:"
aoRWT = "n"
'***Xong LOCALIZATION***
End Sub
Private Sub UserForm_Activate()
'Đặt nhãn Form và phím nóng
FindSymbol97.Caption = TheFORMName
cmdCancelButton.Caption = CANC
cmdCancelButton.Accelerator = aCANC
cmdFindNextButton.Caption = FINDN
cmdFindNextButton.Accelerator = aFINDN
cmdReplaceButton.Caption = REP
cmdReplaceButton.Accelerator = aREP
cmdReplaceAllButton.Caption = REPA
cmdReplaceAllButton.Accelerator = aREPA
cmdFindSymbolDialogButton.Caption = SYM
cmdFindSymbolDialogButton.Accelerator = aSYM
cmdReplaceSymbolDialogButton.Caption = RSYM
cmdReplaceSymbolDialogButton.Accelerator = aRSYM
lblFindCharacter.Caption = CHRTR
lblFindFont.Caption = FNT
lblReplaceCharacter.Caption = CHRTR
lblReplaceFont.Caption = FNT
optSearchDoc.Caption = DOC
optSearchDoc.Accelerator = aDOC
optSearchSel.Caption = SEL
optSearchSel.Accelerator = aSEL
optReplaceWithText.Caption = oRWT
optReplaceWithText.Accelerator = aoRWT
lblFW.Caption = FW
lblRW.Caption = RW
lblSD.Caption = SRCH
End Sub
Private Sub UserForm_Initialize()
Localization
Set SinglePHolder = ActiveDocument.Range(0, 0)
'Chuẩn bị FORM
cmdReplaceSymbolDialogButton.Enabled = True
cmdFindNextButton.Enabled = False
cmdFindSymbolDialogButton.SetFocus
cmdReplaceButton.Enabled = False
cmdReplaceAllButton.Enabled = False
txtReplaceTextBox.Enabled = False
If Application.Selection.Start = Application.Selection.End Then
optSearchSel.Enabled = False
optSearchDoc.Value = 1
Else
optSearchSel.Value = 1
optSearchDoc.Value = 0
End If
End Sub
Private Sub cmdCancelButton_Click()
'Bỏ và xoá BOOKMARK nếu tồn tại
If ActiveDocument.Bookmarks.Exists("PHolder") = True Then
ActiveDocument.Bookmarks("PHolder").Delete
End If
Unload FindSymbol97
End Sub
Private Sub cmdFindSymbolDialogButton_Click()
'Hiện hộp thoại Symbol để phục vụ tìm
Done = 0
ToggleReplaceFind = 0
GoodFind = 0
XVar = 0
NotFoundMsg = 0
Set dlg = Dialogs(wdDialogInsertSymbol)
dlg.Tab = 0
If dlg.Display() <> -2 Then
lblFindFontText = dlg.Font
lblFindCharacterText = dlg.charnum
Else
lblFindFontText = ""
lblFindCharacterText = ""
End If
If lblFindFontText = NRMText Then
MsgBox SearchingNormal, vbInformation, "Tin học ABC - 029 854 134"
lblFindFontText = ""
lblFindFontText.Enabled = True
lblFindCharacterText = ""
lblFindCharacterText.Enabled = True
End If
If dlg.Tab = 1 Then
MsgBox SearchingSpecChar, vbInformation, "Tin học ABC - 029 854 134"
lblFindFontText = ""
lblFindFontText.Enabled = True
lblFindCharacterText = ""
lblFindCharacterText.Enabled = True
End If
vlblFindFontText = lblFindFontText
vlblFindCharacterText = lblFindCharacterText
If ActiveDocument.Bookmarks.Exists("PHolder") = True Then
ActiveDocument.Bookmarks("PHolder").Select
End If
If optSearchDoc.Value Then
ActiveDocument.Content.Select
End If
ButtonHandler
End Sub
Private Sub cmdReplaceSymbolDialogButton_Click()
''Hiện hộp thoại Symbol để phục vụ thay thế
optReplaceWithSymbol.Value = 1
Set dlg = Dialogs(wdDialogInsertSymbol)
dlg.Tab = 0
If dlg.Display() <> -2 Then
lblReplaceFontText = dlg.Font
lblReplaceCharacterText = dlg.charnum
Else
lblReplaceFontText = ""
lblReplaceCharacterText = ""
End If
If dlg.Tab = 1 Then
MsgBox RepSpecChar, vbInformation, "Tin học ABC - 029 854 134"
lblReplaceFontText = ""
lblReplaceFontText.Enabled = True
lblReplaceCharacterText = ""
lblReplaceCharacterText.Enabled = True
End If
ButtonHandler
End Sub
Private Sub cmdFindNextButton_Click()
'Tìm tiếp nếu chọn nút tìm tiếp
ToggleReplaceFind = 1
FindSymbolRoutine
End Sub
Private Sub cmdReplaceButton_Click()
If ToggleReplaceFind = 0 Then
ToggleReplaceFind = 1
ElseIf ToggleReplaceFind = 1 Then
ToggleReplaceFind = 0
End If
'Nếu tìm chưa ấn phím định nghĩa thì tìm
If ToggleReplaceFind = 1 Then FindSymbolRoutine
'Nếu tìm chưa ấn phím định nghĩa thì thay
vlblReplaceFontText = lblReplaceFontText
vlblReplaceCharacterText = lblReplaceCharacterText
If ToggleReplaceFind = 0 And NotFoundMsg <> 1 Then
ReplaceRoutine
ToggleReplaceFind = 1
FindSymbolRoutine
End If
End Sub
Private Sub cmdReplaceAllButton_Click()
'Quay lại màn hình, không cập nhật
Application.ScreenUpdating = False
vlblReplaceFontText = lblReplaceFontText
vlblReplaceCharacterText = lblReplaceCharacterText
'Thay thế 1 lần nếu chọn nút đã định nghĩa
If ToggleReplaceFind = 1 Then
ReplaceRoutine
End If
'So sánh tìm thay thế ...
NotFoundMsg = 0
While NotFoundMsg <> 1
FindSymbolRoutine
If NotFoundMsg <> 1 Then
ReplaceRoutine
End If
Wend
NotFoundMsg = 0
'Quay lại màn hình và cập nhật
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub
Private Sub optReplaceWithSymbol_Click()
'Thay đổi và thay thế bằng SYMBOL
lblReplaceFont.Enabled = True
lblReplaceCharacter.Enabled = True
lblReplaceFontText.Enabled = True
lblReplaceCharacterText.Enabled = True
txtReplaceTextBox.Enabled = False
ButtonHandler
End Sub
Private Sub optReplaceWithText_Click()
'Thay đổi và thay thế bằng đoạn văn
cmdReplaceButton.Enabled = True
cmdReplaceAllButton.Enabled = True
lblReplaceFont.Enabled = False
lblReplaceCharacter.Enabled = False
lblReplaceFontText.Enabled = False
lblReplaceCharacterText.Enabled = False
txtReplaceTextBox.Enabled = True
txtReplaceTextBox.SetFocus
ButtonHandler
End Sub
Private Sub optSearchDoc_Click()
' Thay đổi khi tìm toàn bộ văn bản
optSearchSel.Value = 0
optSearchDoc.Value = 1
If ActiveDocument.Bookmarks.Exists("PHolder") = True Then
ActiveDocument.Bookmarks("PHolder").Select
ActiveDocument.Bookmarks("PHolder").Delete
ToggleReplaceFind = 0
End If
WholeDoc = 1
End Sub
Private Sub optSearchSel_Click()
' Thay đổi khi tìm trong phần chọn
optSearchDoc.Value = 0
optSearchSel.Value = 1
WholeDoc = 0
End Sub
Private Function ButtonHandler()
'Hiện, ẩn các nút lệnh
If lblFindFontText <> "" Then
cmdFindNextButton.Enabled = True
If optReplaceWithSymbol.Value = True Then
If lblFindFontText <> "" Then
If lblReplaceFontText <> "" Then
cmdReplaceButton.Enabled = True
cmdReplaceAllButton.Enabled = True
Else
cmdReplaceButton.Enabled = False
cmdReplaceAllButton.Enabled = False
End If
End If
Else
If txtReplaceTextBox.Enabled = True Then
If lblFindFontText <> "" Then
cmdReplaceButton.Enabled = True
cmdReplaceAllButton.Enabled = True
Else
cmdReplaceButton.Enabled = False
cmdReplaceAllButton.Enabled = False
End If
End If
End If
Else
cmdFindNextButton.Enabled = False
cmdReplaceButton.Enabled = False
cmdReplaceAllButton.Enabled = False
End If
End Function
Private Function ReplaceRoutine()
'Thay thế với văn bản hoặc Symbol được chọn
If optReplaceWithSymbol Then
Selection.Collapse
Selection.InsertSymbol Font:=vlblReplaceFontText,
characternumber:=vlblReplaceCharacterText, _
unicode:=True
Selection.Delete Unit:=wdCharacter, Count:=1
Else
Selection.Collapse
Selection.InsertBefore txtReplaceTextBox
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Selection.Collapse
End Function
Private Function FoundFontName()
'Nhận tên hoặc Fon chữ nếu tìm thấy
If lblFindCharacterText > 0 Then
FoundFontName = Selection.Font.Name
Else
Set dlg = Dialogs(wdDialogInsertSymbol)
FoundFontName = dlg.Font
End If
End Function
Private Function FindSymbolRoutine()
'Tìm symbol kế tiếp
If WholeDoc = 1 Then
ActiveDocument.Content.Select
If ActiveDocument.Bookmarks.Exists("PHolder") Then
ActiveDocument.Bookmarks("PHolder").Delete
End If
Set PHolder = ActiveDocument.Bookmarks.Add("Pholder")
WholeDoc = 0
End If
If ActiveDocument.Bookmarks.Exists("PHolder") = False Then
If optSearchSel.Value Then
Set PHolder = Selection.Bookmarks.Add("PHolder")
Else
ActiveDocument.Content.Select
Set PHolder = Selection.Bookmarks.Add("PHolder")
End If
GoodFind = 0
End If
If optSearchDoc.Value = True Then
optSearchSel.Enabled = False
End If
If Done = 1 Then
NotFoundMsg = 0
If optSearchDoc.Value = True Then
ActiveDocument.Content.Select
Else
ActiveDocument.Bookmarks("PHolder").Select
End If
End If
Done = 0
XVar = 0
While XVar <> lblFindFontText And Done <> 1
If lblFindCharacterText < 0 Then
unichar = lblFindCharacterText + 65536
Else
unichar = lblFindCharacterText
End If
With Selection.Find
.ClearFormatting
.Text = ChrW(unichar)
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchWildcards = False
.Wrap = 0
.Execute
End With
XVar = FoundFontName()
If XVar = lblFindFontText And Selection.Find.Found = True Then GoodFind = 1
'Hiện hộp thoại nếu không tìm thấy
If Selection.Range.End > ActiveDocument.Bookmarks("PHolder").End Or _
Selection.Range.Start < ActiveDocument.Bookmarks("pholder").Start Or _
(Selection.Range.Start = SinglePHolder.Start And _
Selection.Range.End = SinglePHolder.End) Then
Selection.Collapse
If GoodFind = 1 Then
If optSearchSel.Value = True Then
MsgBox FSSel, vbInformation, "Tin học ABC - 029 854 134"
ActiveDocument.Bookmarks("PHolder").Select
Else
MsgBox FSDoc, vbInformation, "Tin học ABC - 029 854 134"
ActiveDocument.Content.Select
End If
NotFoundMsg = 1
Else
MsgBox SNF, vbInformation, "Tin học ABC - 029 854 134"
NotFoundMsg = 1
ActiveDocument.Bookmarks("PHolder").Select
End If
Done = 1
ToggleReplaceFind = 0
Else
If Selection.Find.Found = False And GoodFind = 1 And NotFoundMsg <> 1 Then
If optSearchSel.Value = True Then
MsgBox FSSel, vbInformation, "Tin học ABC - 029 854 134"
Else
MsgBox FSDoc, vbInformation, "Tin học ABC - 029 854 134"
End If
ActiveDocument.Bookmarks("PHolder").Select
NotFoundMsg = 1
Done = 1
ToggleReplaceFind = 0
End If
If Selection.Find.Found = False And GoodFind = 0 And NotFoundMsg <> 1 Then
MsgBox SNF, vbInformation, "Tin học ABC - 029 854 134"
NotFoundMsg = 1
ActiveDocument.Bookmarks("PHolder").Select
Done = 1
ToggleReplaceFind = 0
End If
End If
Set SinglePHolder = Selection.Range
Wend
End Function
Bạn phải viết một Thủ tục để gọi Tìm và thay thế Symbol, rồi gán thủ tục này vào
một tổ hợp phím hoặc menu, hoặc nút lệnh. Ví dụ:
Sub TimVaThayTheSymbol()
FindSymbol97.Show
End Sub
Chúc các bạn thành công.
--------------------------------------------
Trung tâm phổ cập Tin học miền núi ABC - Số 525 - Đường Điện Biên - TX. Yên Bái
Là trung tâm Tin học do một số bộ đội và giáo viên tiểu học ở Yên Bái thành lập.
Xa trung tâm, khó khăn rất nhiều, toàn bộ giáo án và tài liệu đào tạo đều bằng
tiếng Anh soạn lại nên rất khó khăn. Học sinh học chủ yếu là người vùng sâu,
vùng xa , 1/3 trong số đó là dân tộc ít người.
Trang bị đều do các cá nhân bỏ ra, không có sự giúp đỡ của các tổ chức và nhà
nước nên tiếp thu những kiến thức mới chủ yếu qua Internet và Thế giới Vi tính
rất hạn chế.
Nguyễn Phương Thảo
PcLeHoan
1996 - 2002
Mirror :
http://www.pclehoan.com
Mirror :
http://www.lehoanpc.net
Mirror :
http://www.ktlehoan.com