Gửi bởi
ntn1197612
File này mình mún sửa code thành tách 2 hoặc 3 trang thành 1 file thì sửa như thế nào vậy bạn?
Thú thật là đọc code ko hiểu cho lắm.
Mã:
Sub newSplitFile()
On Error Resume Next
Application.ScreenUpdating = False
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Pages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
ChangeFileOpenDirectory ActiveDocument.Path
j = 0
For i = 0 To Pages – 1
‘ Pages là tổng số trang, vậy bạn đổi (pages -1) cho phù hợp
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
'câu này chọn đến trang bạn muốn ngắt, => đổi Count cho phù hợp
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Cut
Selection.TypeBackspace
Selection.HomeKey Unit:=wdStory
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Count:=1, Name:=""
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Stt = Mid(Selection.Text, InStr(1, Selection.Text, ": ") + 1, Len(Selection.Text))
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Count:=1, Name:=""
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
tennv = Mid(Selection.Text, InStr(1, Selection.Text, ": ") + 1, Len(Selection.Text))
Fname = "File_" & Stt & "_" & tennv & ".docx"
If Fname <> "File__.docx" Then j = j + 1
ActiveDocument.SaveAs2 FileName:=Fname, FileFormat:=wdFormatXMLDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=True, WritePassword:="", _
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, CompatibilityMode:=14
Selection.WholeStory
Selection.Paste
Next i
Application.ScreenUpdating = True
MsgBox "Done!" & Chr(13) & "There's " & j & " page(s) were saved !"
End Sub
bạn làm thử nhé,
for .. next ... khó hiểu thì dùng
Do While (ActiveDocument.BuiltInDocumentProperties(wdProper tyPages) lớn hơn số pages muốn ngắt trang, nhớ kiểm tra số lẻ còn lại để bẫy lỗi không đú số trang để goto)
...
Loop
Thường mình tìm hiểu code bằng cách run từng dòng lệnh (nhấn F8) xem code nó làm gì, hiểu tổng quan của code rồi tìm đúng chỗ mình cần sửa, sửa lại, hì hì.