2017-12-11 9 views
-1

学校向けに、私は経験のような "フラッシュカード"を作成するように配置された写真(数百から数千)で多数の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

それを考え出してください。大きな叫び声:[リンク](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 
関連する問題