[hỏi] cách thay thế nhiều từ trong word cùng lúc

Liên hệ QC

chiquan91

Thành viên mới
Tham gia
31/10/11
Bài viết
1
Được thích
0
Em hay phải convert tài liệu pdf sang word, thường xuyên gặp phải trường hợp lỗi font mà không thể sửa bằng bất cứ công cụ nào, những lúc đấy em phải dùng công cụ replace trong word để thay từng từ một. Vậy em muốn hỏi các bạn/anh/chị có cách nào (như tool, lệnh trong excel) mà mình có thể add nhiều từ để find/replace cùng 1 lúc không? Em xin cảm ơn trước:-=
 
Tạm thời, bạn gửi file cần chuyển (xóa bớt chỉ để 1 trang) và tạo 1 bảng ở cuối tài liệu, có 2 cột, cột 1 chứa các từ đang có cần thay, cột 2 chứa các từ sẽ thay. Tôi sẽ gửi lại file này với macro chuyển đổi cho bạn.

Nếu làm nhiều thì bảng trên chỉ cần làm 1 lần. Bạn cứ nhập các từ cần thay và sẽ thay cho hết. Chú ý mỗi từ 1 ô của bảng.

Tôi cũng hơi bận, nếu gửi lên mà nhận lại thông tin muộn thì cũng thông cảm, vì còn có người khác giúp.
 
của bạn đây:
Phần 1:
Option Explicit
' Chon biên tên_file_chon(1 To 100) nham muc dich giai phóng bô nho cua KQ thôi
Public tên_file_chon(1 To 100) As String '100 files is the maximum applying this code
Public i As Integer

Sub ABC()
' ----------------------------------------------------- '
' Tao cac ban VietPharse Edit '
' Write by: duongthanh85 '
' Y!M & Skype: duongthanhhtd '
' Email: duongdinhthanh@gmail.com '
' Blog: www.360.yahoo.com/duongthanhhtd '
' ----------------------------------------------------- '

Dim CheckFile As Boolean
Dim k As Integer
Dim j As Integer
Dim Page_hiên_tai As Integer


'call a routine that removes all settings from the find dialog
'so future users of the dialog won't get strange results

' Call ClearFindAndReplaceParameters

Page_hiên_tai = 0

'get current page
Page_hiên_tai = Selection.Information(wdActiveEndAdjustedPageNumber)

Call FileDialogOpen_Multi


For j = 1 To i Step 1

CheckFile = VerifyFileABC(tên_file_chon(j))
If tên_file_chon(j) <> vbNullString And CheckFile = True Then
'MsgBox "ok xxxxxxxxxx: " & tên_file_chon(j)
Call trans1(tên_file_chon(j))

Else
' Châm dut
End
End If
Next

' vÊ lai trang hiên thi truoc dó
Selection.GoTo what:=wdGoToPage, Which:=wdGoToNext, Name:=Page_hiên_tai

End Sub
Function VerifyFileABC(FileName As String) As Boolean
' ----------------------------------------------------- '
' Kiem tra su ton tai cua mot file '
' Write by: duongthanh85 '
' Y!M & Skype: duongthanhhtd '
' Email: duongdinhthanh@gmail.com '
' Blog: www.360.yahoo.com/duongthanhhtd '
' ----------------------------------------------------- '
On Error Resume Next
Open FileName For Input As #1
If Err Then
MsgBox ("File: " & FileName & " Khong ton tai, kiem tra lai duong dan file data !! Hoac ban da huy chon file.")
VerifyFileABC = False
Else
VerifyFileABC = True
End If
Close #1
End Function

Sub FileDialogOpen_Multi()
Dim MyDialog As FileDialog
Dim Tâp_hop_cac_tên_file As Variant
Dim KQ As Variant


'On Error Resume Next
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
.Filters.Clear
.Filters.Add "All EXELL File ", "*.xls", 1
.AllowMultiSelect = True

i = 1
If .Show = -1 Then
For Each Tâp_hop_cac_tên_file In .SelectedItems

tên_file_chon(i) = Tâp_hop_cac_tên_file
i = i + 1
Next
i = i - 1
' ReDim tên_file_chon(Tâp_hop_cac_tên_file)
Else
MsgBox "Ban chua chon file."
End
End If

End With
End Sub
 
phần 2



Sub trans1(txtstr As String)

' ----------------------------------------------------- '
' Thuc hien replace tu dong theo File data '
' Write by: duongthanh85 '
' Y!M & Skype: duongthanhhtd '
' Email: duongdinhthanh@gmail.com '
' Blog: www.360.yahoo.com/duongthanhhtd '
' ----------------------------------------------------- '
Dim bstartApp As Boolean
Dim xlapp As Object
Dim xlbook As Object
Dim xlsheet As Object
Dim xlrange1 As Object

Dim iCount As Integer
Dim tongsotu As Integer


Dim myarray As Variant
Dim Findarray As Variant
Dim Replarray As Variant
Dim string_Findarray() As String
' Thu nghiem tam thoi
Dim KQ As String
KQ = " "



Dim txt_vao1 As String
Dim txt_vao2 As String




Dim txt_vao As String





Dim i, j As Integer
'Khai bao Variant
Dim Arr As Variant



' Dêm tu

' Dim NumCharsBefore As Long, NumCharsAfter As Long
' Dim CountWord As Long


' Them 1 khoang trang sau cau can xuong dong
' Thaymaytudongiankhongdungwildcads MÂU_TÌM:="([! ])(^13)", MÂU_THAY:="\1 \2", mau_me:=4



Selection.Find.ClearFormatting

' Application.ScreenUpdating = False
' Application.ScreenUpdating = True

'Get the number of chars in the doc BEFORE doing Find & Replace

'CountWord = 0
'NumCharsBefore = ActiveDocument.Characters.Count


' Replace theo file txt_vaostr









On Error Resume Next
Set xlapp = GetObject(, "Excel.Application")
If Err Then
bstartApp = True
Set xlapp = CreateObject("Excel.Application")
End If
'On Error GoTo 0
With xlapp
' On Error GoTo Err_Handler
Set xlbook = .Workbooks.Open(txtstr)
Set xlsheet = xlbook.Worksheets(1)


With xlsheet

' só 4 này tuong duong voi 1 trong 4 hang sô
' nó tìm den dòng du lieu cuôi cùng
Set xlrange1 = .Range("A1", .Range("A1").End(4))
' Set xlrange2 = .Range("B1", .Range("B1").End(4))
Findarray = xlrange1.Value




' Replarray = xlrange2.Value
End With

End With
If bstartApp = True Then
xlapp.Quit
End If
Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
Set xlrange1 = Nothing


For i = 1 To UBound(Findarray)


j = InStr(Findarray(i, 1), "=")

If j <> 0 Then
Arr = Split(Findarray(i, 1), "=", 2)
txt_vao1 = Trim(Arr(0))
txt_vao2 = Arr(1)
If txt_vao2 = vbNullString Then
txt_vao2 = " "

End
Else
txt_vao2 = Trim(txt_vao2)
End If
Else
' gán các giá tri này = Null se bao loi; cho chuoi bat ky
txt_vao1 = "bbb111bbbccc dd dxxxyyyxzxx"
txt_vao2 = "mâmmamama mmamma mamammamam"


End If




' Selection.HomeKey Unit:=wdStory
'iCount = 0
'Application.ScreenUpdating = False

With Selection.Find
.ClearFormatting
.MatchCase = False
.Text = LÂY_MÂU_TÌM(txt_vao1, txt_vao2)

''==================================
'' Kiêm tra nêu nhu có dâu * hay [ ] ( ) không thì thêm dâu \ o truoc
''==================================
If InStr(txt_vao1, "*") <> 0 Then
txt_vao1 = Replace(txt_vao1, "*", "\*")
End If

If InStr(txt_vao1, "[") <> 0 Then
txt_vao1 = Replace(txt_vao1, "[", "\[")
End If

If InStr(txt_vao1, "]") <> 0 Then
txt_vao1 = Replace(txt_vao1, "]", "\]")
End If

If InStr(txt_vao1, "(") <> 0 Then
txt_vao1 = Replace(txt_vao1, "(", "\(")
End If
If InStr(txt_vao1, ")") <> 0 Then
txt_vao1 = Replace(txt_vao1, ")", "\)")
End If
If InStr(txt_vao1, "{") <> 0 Then
txt_vao1 = Replace(txt_vao1, "{", "\{")
End If
If InStr(txt_vao1, "}") <> 0 Then
txt_vao1 = Replace(txt_vao1, "}", "\}")
End If
' Hàm txt_vao1 nêu có dâu * se duoc bien doi
' Luu ý là LÂY_MÂU_THAY(txt_vao1, txt_vao2) phai o tren khong thi bao loi


.Text = LÂY_MÂU_TÌM(txt_vao1, txt_vao2)
.Replacement.Text = LÂY_MÂU_THAY(txt_vao1, txt_vao2)

If ((Left(Trim(txt_vao1), 1) = "|") And (Right(Trim(txt_vao1), 1) = "|")) Then

.Text = "(^13)([ ]{1,2})(" & LÂY_MÂU_TÌM(txt_vao1, txt_vao2) & ")([ ]{1,2})(^13)"
.Replacement.Text = "\1\2" & LÂY_MÂU_THAY(txt_vao1, txt_vao2) & "\4\5"

End If






.Replacement.HIGHLIGHT = True
.MatchWholeWord = True
.MatchWildcards = True
''==================================
'' Kiêm tra nêu hai ve thay = nhau
''==================================
If txt_vao1 = txt_vao2 Then
.MatchWildcards = False
.Text = "bbb111bbbccc dd dxxxyyyxzxx"
.Replacement.Text = "mâmmamama mmamma mamammamam"
End If


.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindContinue


' Loop until Word can no longer
' find the search string and
' count each instance


Do While .Execute
''==================================
'' Neu can dem tu thi dung icount
'iCount = iCount + 1
''==================================
' Thay 1 lân
.Execute Replace:=wdReplaceOne
' Chuyen den tu tiep theo
Selection.MoveRight
''==================================
'' Neu can dem tu thi dung icount
'iCount = iCount + 1
''==================================
'tongsotu = iCount
Loop

End With

'===============Xu ly lan thu 2 cua truong hop co 2 dau "|" o dau va cuoi tu

If ((Left(Trim(txt_vao1), 1) = "|") And (Right(Trim(txt_vao1), 1) = "|")) Then

With Selection.Find
.ClearFormatting
.MatchCase = False
' mâutìm = "(^13)([ ]{1,2})(" & A_txt1_mautim & ")(^13)"
' mâutìm = "(^13)([ ]{1,2})(" & A_txt1_mautim & ")([ ]{1,2})(^13)"
' mâuthay = "\1\2" & txt2_mauthay & "\4"
' mâuthay = "\1\2" & txt2_mauthay & "\4\5"

.Text = "(^13)([ ]{1,2})(" & LÂY_MÂU_TÌM(txt_vao1, txt_vao2) & ")(^13)"

''==================================
'' Kiêm tra nêu nhu có dâu * hay [ ] ( ) không thì thêm dâu \ o truoc
''==================================
If InStr(txt_vao1, "*") <> 0 Then
txt_vao1 = Replace(txt_vao1, "*", "\*")
End If

If InStr(txt_vao1, "[") <> 0 Then
txt_vao1 = Replace(txt_vao1, "[", "\[")
End If

If InStr(txt_vao1, "]") <> 0 Then
txt_vao1 = Replace(txt_vao1, "]", "\]")
End If

If InStr(txt_vao1, "(") <> 0 Then
txt_vao1 = Replace(txt_vao1, "(", "\(")
End If
If InStr(txt_vao1, ")") <> 0 Then
txt_vao1 = Replace(txt_vao1, ")", "\)")
End If
If InStr(txt_vao1, "{") <> 0 Then
txt_vao1 = Replace(txt_vao1, "{", "\{")
End If
If InStr(txt_vao1, "}") <> 0 Then
txt_vao1 = Replace(txt_vao1, "}", "\}")
End If
' Hàm txt_vao1 nêu có dâu * se duoc bien doi
' Luu ý là LÂY_MÂU_THAY(txt_vao1, txt_vao2) phai o tren khong thi bao loi


.Text = "(^13)([ ]{1,2})(" & LÂY_MÂU_TÌM(txt_vao1, txt_vao2) & ")(^13)"
.Replacement.Text = "\1\2" & LÂY_MÂU_THAY(txt_vao1, txt_vao2) & "\4"


.Replacement.HIGHLIGHT = True
.MatchWholeWord = True
.MatchWildcards = True
''==================================
'' Kiêm tra nêu hai ve thay = nhau
''==================================
If txt_vao1 = txt_vao2 Then
.MatchWildcards = False
.Text = "bbb111bbbccc dd dxxxyyyxzxx"
.Replacement.Text = "mâmmamama mmamma mamammamam"
End If


.MatchSoundsLike = False
.MatchAllWordForms = False
.Wrap = wdFindContinue
'.Execute Replace:=wdReplaceAll
' Loop until Word can no longer
' find the search string and
' count each instance


Do While .Execute
''==================================
'' Neu can dem tu thi dung icount
'iCount = iCount + 1
''==================================
' Thay 1 lân
.Execute Replace:=wdReplaceOne
' Chuyen den tu tiep theo
Selection.MoveRight
''==================================
'' Neu can dem tu thi dung icount
'iCount = iCount + 1
''==================================
'tongsotu = iCount
Loop


End With
End If

' Neu truong hop co "|" o phia sau thi thêm 1 truong hop nua



' Call ClearClipboard

' Application.ScreenUpdating = True
' ActiveDocument.UndoClear
' Selection.Find.ClearFormatting
Next i

' MsgBox j

' MsgBox KQ


'Get the number of chars AFTER doing Find & Replace
'NumCharsAfter = ActiveDocument.Characters.Count

'Calculate of the number of replacements,
'and put the result into the function name variable
'CountWord = NumCharsAfter - NumCharsBefore




'Hien thi
' MsgBox "Da thay the: " & CountWord & _
" ky tu trong lan nay ", vbInformation
'call a routine that removes all settings from the find dialog
'so future users of the dialog won't get strange results

'Thaymaytudongiankhongdungwildcads MÂU_TÌM:=" ", MÂU_THAY:=" ", mau_me:=5
Call Xoa_hai_khoangtrang
Call ClearFindAndReplaceParameters

'Err_Handler:
' Exit Sub

End Sub
 
Web KT

Group

DIỄN ĐÀN GIẢI PHÁP EXCEL Group 1
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 2
Back
Top Bottom