PDA

View Full Version : Solved: Automating a presentation



kroz
09-07-2010, 06:03 AM
Hi All,

I'm trying to automate the creation of a PowerPoint presentation - i have a template that i'm using and need to load x images in several tables.
I've managed to work out a MACRO to load, crop and position the files in the right position but i still don't know how to create a window in which i can modify the path to my images.

Right now i wrote the entire path in the MACRO and i'm trying to replace it with a variable. The only problem is - how do i read this variable and more then that, how do i hide this window once i've read it (I wouldn't want it to show up when i'm showing the presentation later on).

Thanks

kroz
09-10-2010, 02:35 AM
I've decided to change things a little since I was going nowhere with the way I was thinking before. So, new plan. Put the results file in the folder i have the ppt.
Now comes a second challenge. Each file in my OUTPUT folder (that's the folder that contains the results i'm trying to put into the presentation) has a certain name blah
_[code1]_moreblah.jpeg. How do i read that? Each file is unique

[code]ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:=Application.A ctivePresentation.Path & "\OUTPUT\blah" & "*" &"moreblah.jpeg", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=23, Top:=-59, Width:=675, Height:=660).Select

John Wilson
09-11-2010, 10:27 PM
Maybe add a file picker dialogue? Here's the code

Sub OpenDialog()
Dim Fdlg As FileDialog
Dim strFileName As String
Set Fdlg = Application.FileDialog(msoFileDialogFilePicker)
With Fdlg
.InitialFileName = ActivePresentation.Path & "\OUTPUT\"
.Title = "Browse for a file"
.Filters.Clear
.AllowMultiSelect = False
'change to suit
.Filters.Add "Pictures , *.jpg,", "*.gif,*.png"
If .Show = True Then
strFileName = .SelectedItems(1)
End If
End With
If strFileName = "" Then Exit Sub
'Add code to do something with strFileName eg add pic to slide
Set Fdlg = Nothing
End Sub

kroz
09-13-2010, 12:05 AM
Thank you John, that would work for my initial request.
However, as I stated in my second post, I don't need to read all the files manually (i have 18 pictures to add), i want to automate the reading process for all the files.
To do this I've created a macro that crops and positions all the 18 files and all i need to do now is read them.
All the files have basically the same name structure (i create the images with a script in another program) so i'm trying to use that in my favor.
The images have names like [CommonPartOfTheName]_[UniqueEnding].jpeg.
I will add the folder picker code to my macros, that would make the macro more flexible.
I might declare a variable to read the common part thingy after reading the folder. I'll let you know if I manage to have a piece of code that is running

John Wilson
09-13-2010, 12:24 AM
I'm finding it hard to visualize what you need but maybe something based on this:

Sub loadpics()
Const strCommon As String = "C:\Users\John\Desktop\Pics\start_"
Dim strVar As String
Dim i As Integer
Dim opic As Shape
For i = 1 To 18
Select Case i
Case Is = 1
strVar = "Pic1.jpg"
Case Is = 2
strVar = "MyPic2.jpg"
'etc to 18
End Select
Set opic = ActivePresentation.Slides(i).Shapes.AddPicture(strCommon & strVar, msoFalse, msoTrue, 10, 10, 100, 60)
With opic
'insert code to crop, move etc
End With
wNext
End Sub

kroz
09-21-2010, 06:16 AM
Thank you John, i think it might work.
I'll get back to you with the final version once i have time to compile one.

kroz
09-22-2010, 05:06 AM
ok, i have advanced a bit, both in thinking on how to create a structure for my problem and code too. Here's where i am now:



'Variable declaration not included in the code below
l = 4
'Ciclying the tables/slides

For i = 1 To 4
ActivePresentation.Slides(l).Select
l = l + 1
For k = 1 To 2
For j = 1 To 3
Var5 = Var1(k) & Var2 & Var3(i) & Var4(j)
Set opic(k, j) = ActivePresentation.Slides(i).Shapes.AddPicture(strCommon & Var5, msoFalse, msoTrue, 23, -59, 675, 660)
With opic(1, 1)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 73.12
.Top = 59.38
End With
With opic(1, 2)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 273.75
.Top = 59.38
End With
With opic(1, 3)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 473.75
.Top = 59.38
End With
With opic(2, 1)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 73.12
.Top = 281.5
End With
With opic(2, 2)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 273.12
.Top = 281.5
End With
With opic(2, 3)
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 46.68
.Left = 473.12
.Top = 281.5
End With
Next
Next
Next


I'm a newbie when it comes to scripting in VBA so i don't really know how to call the function that inserts and modifies the images. It gave me a error on the second with block...
Should I use "Case" ? :think:

kroz
09-22-2010, 11:04 PM
got it working and tuned it to perfection:)

Thank you John for your invaluable help!


'Initializing the variables

m = 0

'Ciclying the tables/slides

For i = 1 To 4
For k = 1 To 2
For j = 1 To 3

'This is where the name for the current image is created
Var5 = Var1(k) & Var2 & Var3(i) & Var4(j)
l = i + 3
'Loading the image in clipboard
Set opic = ActivePresentation.Slides(l).Shapes.AddPicture(strCommon & Var5, msoFalse, msoTrue, 23, -59, 675, 660)
m = m + 1

Select Case m
Case Is = 1

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 67.12
.Top = 61.38
.PictureFormat.CropBottom = 42.11
End With

Case Is = 2

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 267.75
.Top = 61.38
.PictureFormat.CropBottom = 42.11
End With

Case Is = 3

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 467.75
.Top = 61.38
.PictureFormat.CropBottom = 42.11
End With

Case Is = 4

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 67.12
.Top = 283.5
.PictureFormat.CropBottom = 42.11
End With

Case Is = 5

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 267.12
.Top = 283.5
.PictureFormat.CropBottom = 42.11
End With

Case Is = 6

With opic
.Fill.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 218.5
.Width = 191.88
.PictureFormat.CropRight = 30.88
.Left = 467.12
.Top = 283.5
.PictureFormat.CropBottom = 42.11
m = 0
End With
End Select
Next
Next
Next