2017-12-11 4 views
-1

학교에서 나는 "경험"과 같은 "flashcard"를 생성하도록 배열 된 그림 (수천에서 수천)이 포함 된 수많은 PowerPoint 프레젠테이션을 만듭니다. 디렉토리에서 사진을 가져오고 텍스트 상자의 슬라이드 맨 아래에 그림의 이름과 함께 연속적으로 그림을 삽입하는 코드를 작성했습니다. 지금까지 아무런 문제가 없었습니다. 그러나 모든 파일 이름을 순서대로 나열하는 인덱스 페이지를 만들려고하지만 프레젠테이션 시작 부분의 텍스트 상자에 별도의 줄이 필요합니다.디렉토리에서 텍스트 상자에 파일 이름 추출 Powerpoint VBA

나는 내 코드의 관련 부분만을 포함했습니다.

' (2a)Adds Index Page, compiles file names into index page as a list 
' Creates slide 
Set oSld = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutBlank) 

With Application.ActivePresentation.PageSetup 
    .SlideHeight = 612 
    .SlideWidth = 1087 
End With 

' Create Title Box with Specified Dimensions and Slide Position 
Set oPic = oSld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
      Left:=40, Top:=36, Width:=1007, Height:=540) 


' FORMAT TEXTBOX SHAPE 

' Shape Name 
oPic.Name = "Index" 

' No Shape Border 
oPic.Line.Visible = msoFalse 

' Shape Fill Color 
oPic.Fill.ForeColor.RGB = RGB(255, 255, 255) 

' Shape Text Color 
oPic.TextFrame.TextRange.Font.Color.RGB = RGB(1, 0, 0) 

' Left Align Text 
oPic.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = ppAlignLeft 

' Vertically Align Text to Top 
oPic.TextFrame2.VerticalAnchor = msoAnchorTop 

' Adjust Font Size 
oPic.TextFrame2.TextRange.Font.Size = 11 

' Adjust Font Style 
oPic.TextFrame2.TextRange.Font.Name = "Arial" 

' Change file path to desired directory and add "\" TO THE END: 
strPath = "C:\Users\josephandrews\Desktop\Test\" '*****N.B. note the last "\" at end of line 
strFileSpec = "*.jpg" 'you can change the selected file format, (e.g. "*.png") but only one file type can be used 

strTemp = Dir(strPath & strFileSpec) 

' Text inside Shape. Important to note that strTemp is the pic file name 
oPic.TextFrame.TextRange.Characters.Text = strTemp & vbNewLine 

' Required paramater for Loop through pictures 
Do While strTemp <> "" 
    ' Causes search for next picture in directory 
    strTemp = Dir 
Loop 

텍스트 상자에 모든 파일 이름 목록을 만들 것으로 예상했습니다.

나중에 새 줄이있는 첫 번째 파일의 이름 만 표시합니다.

+0

왜 그림을 가져올 때 목록을 만들지 않았습니까? ... 코드가 잘못 들여 쓰기되었습니다. 들여 쓰기를 수정했는데, 검토가 끝나고 받아 들여지면 볼 수 있어야합니다. ... 첫 번째 위치에서 코드가 제대로 들여 쓰기 되었다면 반복 루프가 코드 끝에 있고 do do nothing 루프라는 것을 알았을 것입니다. – jsotola

+0

알아 냈습니다. 큰 소리 치다 : [link] (http://www.pptfaq.com/FAQ00464_Use_DIR_to_get_a_list_of_files.htm) –

답변

0

답을 찾았습니다. 여기에 내 예가있다.

Sub Test_2() 

' Demonstrates the use of DIR to return a file at a time 

Dim strPath As String 
Dim strFile As String 
Dim strFileSpec As String 
Dim strFilesFound As String 
Dim oSld As Slide 
Dim oTbox As Shape 

strPath = "C:\Users\BobComputer\Desktop\Test\" ' or wherever you want to look for files 
strFileSpec = "*.jpg" ' or whatever type of files you want to list 

' get the first file that meets our specification 
strFile = Dir$(strPath & strFileSpec) 

' if we got at least one file, continue: 
While strFile <> "" 
    strFilesFound = strFilesFound & strFile & vbCrLf 
    ' get the next file and loop 
    strFile = Dir 
Wend 

' let's see what we've got 
'MsgBox strFilesFound 
Set oSld = ActivePresentation.Slides(2) 
Set oTbox = oSld.Shapes(1) 
    oTbox.TextFrame.TextRange.Characters.Text = strFilesFound 


End Sub