babymyl3
Thành viên mới

- Tham gia
- 25/1/19
- Bài viết
- 3
- Được thích
- 0
Tạo 1 Folder chứa File phu luc, Mở File phu luc rồi gán code sau vào Module, khi chạy code nó sẽ tạo File mới là Phuluc_NV_1 (theo thứ tự trang), code hơi chậm nhưng có thể đáp ứng cái đang cần.Chào mọi người ạ, tình hình e nhận được file phụ lục công ty và yêu cầu phải tách nhỏ ra từng file theo tên nhân viên. Em có xem thử các bài đăng dùng code VBA nhưng lại không biết làm lên mò mãi không được, có ai hỗ trợ tách file hộ e với ạ
Sub TachFile_Word()
Dim NhieuTrang As Document
Dim TaoFileMoi As Document
Dim SaoChep As Range
Dim TrangHienTai As Integer
Dim DemSoTrang As Integer
Dim DatTenFileMoi As String
Application.ScreenUpdating = False
Set NhieuTrang = ActiveDocument
Set SaoChep = NhieuTrang.Range
TrangHienTai = 1
'Dém só trang
DemSoTrang = NhieuTrang.Content.ComputeStatistics(wdStatisticPages)
Do Until TrangHienTai > DemSoTrang
If TrangHienTai = DemSoTrang Then
SaoChep.End = ActiveDocument.Range.End 'Sao chep và dùng tai trang cuói
Else
'Chon trang bat dàu
Selection.GoTo wdGoToPage, wdGoToAbsolute, TrangHienTai + 1
SaoChep.End = Selection.Start
End If
SaoChep.Copy 'copy 1 trang
Set TaoFileMoi = Documents.Add 'Tao mói File Word
TaoFileMoi.Range.Paste 'paste vào File mói
'Xóa ngát trang
TaoFileMoi.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
'Dat ten File mói theo thú tu trang
DatTenFileMoi = Replace(NhieuTrang.FullName, ".doc", "_" & Right$("NV_" & TrangHienTai, 8) & ".doc")
TaoFileMoi.SaveAs DatTenFileMoi 'save File mói tao
TrangHienTai = TrangHienTai + 1 'Chuyen trang tiép theo
TaoFileMoi.Close 'close File mói tao
SaoChep.Collapse wdCollapseEnd
Loop
Application.ScreenUpdating = True
'Thoát bo nhó.
Set NhieuTrang = Nothing
Set TaoFileMoi = Nothing
Set SaoChep = Nothing
End Sub
Tạo 1 Folder chứa File phu luc, Mở File phu luc rồi gán code sau vào Module, khi chạy code nó sẽ tạo File mới là Phuluc_NV_1 (theo thứ tự trang), code hơi chậm nhưng có thể đáp ứng cái đang cần.
Mã:Sub TachFile_Word() Dim NhieuTrang As Document Dim TaoFileMoi As Document Dim SaoChep As Range Dim TrangHienTai As Integer Dim DemSoTrang As Integer Dim DatTenFileMoi As String Application.ScreenUpdating = False Set NhieuTrang = ActiveDocument Set SaoChep = NhieuTrang.Range TrangHienTai = 1 'Dém só trang DemSoTrang = NhieuTrang.Content.ComputeStatistics(wdStatisticPages) Do Until TrangHienTai > DemSoTrang If TrangHienTai = DemSoTrang Then SaoChep.End = ActiveDocument.Range.End 'Sao chep và dùng tai trang cuói Else 'Chon trang bat dàu Selection.GoTo wdGoToPage, wdGoToAbsolute, TrangHienTai + 1 SaoChep.End = Selection.Start End If SaoChep.Copy 'copy 1 trang Set TaoFileMoi = Documents.Add 'Tao mói File Word TaoFileMoi.Range.Paste 'paste vào File mói 'Xóa ngát trang TaoFileMoi.Range.Find.Execute Findtext:="^m", ReplaceWith:="" 'Dat ten File mói theo thú tu trang DatTenFileMoi = Replace(NhieuTrang.FullName, ".doc", "_" & Right$("NV_" & TrangHienTai, 8) & ".doc") TaoFileMoi.SaveAs DatTenFileMoi 'save File mói tao TrangHienTai = TrangHienTai + 1 'Chuyen trang tiép theo TaoFileMoi.Close 'close File mói tao SaoChep.Collapse wdCollapseEnd Loop Application.ScreenUpdating = True 'Thoát bo nhó. Set NhieuTrang = Nothing Set TaoFileMoi = Nothing Set SaoChep = Nothing End Sub