VBA Macro/VBA PowerPoint

파워포인트 슬라이드 번호 삽입 매크로

루아흐뉴마 2017. 10. 16. 23:17
반응형





  • 슬라이드 바닥글을 활용하여 페이지번호를 삽입하는 기능이 있지만,
  • 첫째 슬라이드가 아닌 임의의 슬라이드에서 임의의 번호로 시작하는 기능은 없다.
  • 더욱이 슬라이드 마스터가 익숙하지 않은 사람이라면 헷갈리기 쉽다.
  • 이 매크로는 슬라이드 페이지 번호를 자동으로 삽입해주는 매크로이다.
  • 번호의 서식은 자유로이 변경할 수 있고, 빈 슬라이드를 간지로 쓰는 경우에는 레이아웃 인덱스를 지정해서 번호가 붙는것을 방지할 수 있다.


VBA Code


Option Explicit
Dim i As Long, cnt As Long
Dim sld_height As Long, sld_width As Long
Dim mySlide As Slide, myShp As Shape
Dim page_style As String
Sub pptx_page_numbering()

For i = 1 To ActivePresentation.Slides.Count 'i의 시작점을 다른 숫자로 바꾸면 해당 슬라이드부터 번호 시작
    cnt = cnt + 1 '실제 삽입되는 번호 카운팅
    
    If ActivePresentation.Slides(i).CustomLayout.Index = 300 Or _
       ActivePresentation.Slides(i).CustomLayout.Name = "레이아웃 이름" Then
    Else
    
    '위 코드는 PT 보고서 중 빈슬라이드(간지)에는 번호를 삽입하지 않는 코드이다.
    '레이아웃 인덱스를 찾아서 숫자를 넣어주거나, 레이아웃 이름을 지정해서 빈장에는 번호를 삽입하지 않도록 할 수 있다.
    '간지가 몇 장 안된다면 직접 지워주어도 무방하다.
    
        For Each myShp In ActivePresentation.Slides(i).Shapes
            If myShp.Name Like "*" & "myShp" & "*" Then
                myShp.Delete
            End If
        Next myShp
        
        '매크로를 통해 삽입한 임의의 개체임을 나타내기 위해 모든 페이지번호 개체에 "myShp" 이름을 부여 -> 한꺼번에 지울 때 활용 

        ActivePresentation.Slides(i).Select
        
        With ActivePresentation.Slides(i).Shapes.AddShape(Type:=msoShapeRectangle, Left:=500, Top:=515, Width:=40, Height:=20)
            .Name = "myShp" & i
            .TextFrame.HorizontalAnchor = msoAnchorCenter
            .TextFrame.TextRange.Text = cnt  '번호 삽입
            .TextEffect.Alignment = msoTextEffectAlignmentCentered
            .TextFrame.TextRange.Font.Color.RGB = RGB(145, 145, 145)
            .TextFrame.TextRange.Font.Size = 13
            .TextFrame.TextRange.Font.Name = "휴먼고딕"
            .Line.Transparency = 1
            .Fill.Transparency = 1
        End With
        
        ActivePresentation.Slides(i).Shapes("myShp" & i).Select       '도형 위치를 슬라이드의 중앙으로 지정하기 위한 방편
        ActiveWindow.Selection.ShapeRange.Align aligncmd:=msoAlignCenters, relativeto:=msoTrue

        ' 번호 삽입 위치를 오른쪽으로 하고 싶은 경우 msoAlignCenters => msoAlignRights로 변경 (2020. 3. 5. Update)

                
    End If
Next i
cnt = 0
End Sub


매크로 실행 동영상





페이지번호 2개 삽입하기


  • 하나의 슬라이드 안에 2개 페이지 번호를 삽입하는 방법이다.
  • 몇 가지 방법이 있는데, 그냥 무식하게 페이지 번호를 붙이는 코드를 반복해서 쓸 수 있겠다.
  • VBA 코드에서 각 페이지 번호가 생성되는 위치(Left, Top)를 지정해주면 된다.
  • 디버그 창에서 슬라이드 너비와 높이를 알려주는 코드를 상단에 적어놓았으니 슬라이드 사이즈를 확인하고 번호를 달면 된다.
  • 아래 예시 코드를 실행하면 다음 그림과 같이 2개의 페이지 번호가 생성된다.





VBA Code


Option Explicit
Sub pptx_page_numbering()
Dim i As Long, cnt As Long
Dim sld_height As Long, sld_width As Long
Dim mySlide As Slide, myShp As Shape
Dim page_style As String
        
Debug.Print ActivePresentation.SlideMaster.Width  '슬라이드의 너비(가로px)
Debug.Print ActivePresentation.SlideMaster.Height '슬라이드의 높이(세로px)
        
For i = 1 To ActivePresentation.Slides.Count
  For Each myShp In ActivePresentation.Slides(i).Shapes
    If myShp.Name Like "*" & "myShp" & "*" Then
      myShp.Delete
    End If
Next myShp
                
        
'2개의 페이지 번호를 넣기 위해 아래 절취선 부분을 반복
'동일한 페이지 번호를 만들어주되 Left, Top 좌표만 변경
          
'--------------------------------------------------------------
        
cnt = cnt + 1 '실제 삽입되는 번호 카운팅
        
ActivePresentation.Slides(i).Select
  With ActivePresentation.Slides(i).Shapes.AddShape _
    (Type:=msoShapeRectangle, Left:=50, Top:=500, Width:=40, Height:=20)
              
    .Name = "myShp" & cnt '번호 개체에 식별할 수 있는 ID 부여
    '매크로를 통해 삽입한 임의의 개체임을 나타내기 위해
    '모든 페이지번호 개체에 "myShp" 이름을 부여 -> 한꺼번에 지울 때 활용
              
    .TextFrame.TextRange.Text = cnt '번호 삽입
                    
    '아래는 번호 서식
                    
    .TextFrame.HorizontalAnchor = msoAnchorCenter
    .TextEffect.Alignment = msoTextEffectAlignmentCentered
    .TextFrame.TextRange.Font.Color.RGB = RGB(145, 145, 145)
    .TextFrame.TextRange.Font.Size = 13
    .TextFrame.TextRange.Font.Name = "휴먼고딕"
    .Line.Transparency = 1
    .Fill.Transparency = 1
            
  End With
          
'----------------------------------------------------------------
          
          
'--------------------------------------------------------------
        
cnt = cnt + 1 '두 번째 페이지 번호 카운팅
ActivePresentation.Slides(i).Select
  With ActivePresentation.Slides(i).Shapes.AddShape _
        (Type:=msoShapeRectangle, Left:=880, Top:=500, Width:=40, Height:=20)
              
    .Name = "myShp" & cnt
    .TextFrame.TextRange.Text = cnt
                  
    .TextFrame.HorizontalAnchor = msoAnchorCenter
    .TextEffect.Alignment = msoTextEffectAlignmentCentered
    .TextFrame.TextRange.Font.Color.RGB = RGB(145, 145, 145)
    .TextFrame.TextRange.Font.Size = 13
    .TextFrame.TextRange.Font.Name = "휴먼고딕"
    .Line.Transparency = 1
    .Fill.Transparency = 1
  End With
          
'----------------------------------------------------------------
            
Next i
       
cnt = 0

End Sub

 Copyright (2018) Ruahneuma. All Rights Reserved.


반응형