페코히로의 연구하고 성장하는 일상

긴 워드 파일을 3페이지씩 나누어 저장하는 법

에코히로 2023. 1. 5. 23:34

워드 파일이 길 경우에 나누어서 저장하고 싶은 경우가 있다. 

이때를 위해 간단한 VBA code를 작성하였다.
물론 페이지를 3페이지가 아니게 하고 싶으면 조금만 수정하면 된다.

Sub SplitPagesIntoNewDocs()

'Declare variables
Dim doc As Document
Dim page As Range
Dim pageNumber As Integer
Dim pageCount As Integer
Dim docName As String

'Set the document and page count
Set doc = ActiveDocument
pageCount = doc.ComputeStatistics(wdStatisticPages)

'Loop through each group of three pages in the document
For pageNumber = 1 To pageCount Step 3
    Set page = doc.Range(Start:=doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageNumber).Start, _
                         End:=doc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageNumber + 3).Start)
    page.Copy
    
    'Create a new document and paste the page group
    Documents.Add
    Selection.Paste
    
    'Save the new document to the TEMP folder on the C drive
    docName = "Page " & pageNumber & " to " & pageNumber + 2 & ".docx"
    ActiveDocument.SaveAs FileName:="C:\TEMP\" & docName
    ActiveDocument.Close
Next pageNumber

End Sub

이 수정된 버전의 코드는 세 페이지 그룹의 문서를 반복하며 각 그룹을 새 문서에 복사하고 새 문서를 C 드라이브의 TEMP 폴더에 저장한 다음 문서를 닫습니다.



1. 이 코드를 사용하려면 별도의 페이지로 나누려는 Word 문서를 엽니다.

2. "Alt+F11"을 눌러 VBA 편집기를 엽니다.

3. VBA 편집기에서 "삽입"을 클릭한 다음 "모듈"을 클릭하여 새 모듈을 만듭니다.

4. 코드를 모듈에 붙여넣은 다음 VBA 편집기를 닫습니다.

5. Word 문서에서 "Alt+F8"을 눌러 매크로 대화 상자를 엽니다.

6. "SplitPagesIntoNewDocs" 매크로를 선택하고 "실행"을 클릭합니다.

도움이 되길 바래요!