Nội dung này mới chỉ là sơ khai về VBA và chỉ là bản nháp, hiện đã sửa đổi nhiều. Để có kiến thức thành thạo về VBA, bạn cần phải học thêm nhiều nữa...
Ad giải thích giúp code này với ạ
Sub DoReplace()
Const Find1 = "CAR-060"
Const Replace1 = "CAR-034"
Const Find2 = " second find string "
Const Replace2 = " second replacement "
Dim FilePick As FileDialog
Dim FileSelected As FileDialogSelectedItems
Dim WordFile As Variant ' FileName placeholder in selected files loop
Dim FileJob As String ' Filename for processing
Dim WorkDoc As Object
Dim WholeDoc As Range
Dim FooterDoc As Range
' On Error GoTo CreateReports_Error
Set FilePick = Application.FileDialog(msoFileDialogFilePicker)
With FilePick
.Title = "Choose Report Template"
.Filters.Clear
.Filters.Add "Word Documents & Templates", "*.do*"
.Filters.Add "Word 2003 Document", "*.doc"
.Filters.Add "Word 2003 Template", "*.dot"
.Filters.Add "Word 2007 Document", "*.docx"
.Filters.Add "Word 2007 Template", "*.dotx"
.Show
End With
Set FileSelected = FilePick.SelectedItems
If FileSelected.Count <> 0 Then
For Each WordFile In FileSelected
FileJob = WordFile
Set WorkDoc = Application.Documents.Open(FileJob, , , , , , , , , , , False)
Set WholeDoc = WorkDoc.Content
Set FooterDoc = WorkDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
With FooterDoc
.Find.Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
.Find.Execute Find2, True, True, , , , True, , , Replace2, wdReplaceAll
End With
With WholeDoc.Find
.Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
End With
WorkDoc.Save
WorkDoc.Close
Next
End If
MsgBox "Completed"
DoReplace_Exit:
Set WholeDoc = Nothing
Set FilePick = Nothing
Set WorkDoc = Nothing
Set FooterDoc = Nothing
Exit Sub
DoReplace_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DoReplace of VBA Document ReplaceMulti"
Resume DoReplace_Exit
End Sub
Private Sub Document_New()
End Sub